【发布时间】:2015-03-27 19:54:29
【问题描述】:
我有三个表和两个类。两个表/类之间存在多对多关系。
OYS_USER -->带有“oys_lesson”的多对多
OYS_LESSON-->使用“oys_user”的多对多
OYS_LESSON_STUDENT-->关系
在 User.class
@ManyToMany(cascade= CascadeType.ALL,fetch=FetchType.LAZY)
@JoinTable(name="oys_lesson_student",
joinColumns = { @JoinColumn(name="student_id",referencedColumnName="id")},
inverseJoinColumns = {@JoinColumn(name="lesson_id",referencedColumnName="id") })
@Fetch(FetchMode.SUBSELECT)
private Set<Lesson> studentLessons;
在 Lesson.class
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinTable(name = "oys_lesson_student", joinColumns = { @JoinColumn(name = "lesson_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "student_id", referencedColumnName = "id") })
@Fetch(FetchMode.SUBSELECT)
private Set<User> lessonStudents;
我用以下代码更新了集合:
@Override
public void addLessonToStudent(Lesson lesson) {
// TODO Auto-generated method stub
String username = SecurityContextHolder.getContext()
.getAuthentication().getName();
Criteria criteria = openSession().createCriteria(User.class)
.add(Restrictions.eq("username", username));
User user=(User) criteria.uniqueResult();
log.info("'"+lesson.getLessonName()+"' lesson added to '"+user.getUsername()+"'");
/*user.getStudentLessons().add(lesson);
updateUser(user);
*/
lesson.getLessonStudents().add(user);
updateLesson(lesson);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void updateLesson(Lesson lesson) {
log.info(lesson.getLessonName()+" updated.");
openSession().update(lesson);
}
当我将当前用户添加到课程收藏(或课程添加到当前用户收藏)时。
更新/插入查询不会出现在休眠统计信息中(控制台日志)。
因此不能将记录添加到任何集合中。
基本上,我想向多对多集合添加新对象。我做错了什么?
- 我使用hibernate 4.3.5.Final(摆脱延迟加载异常)
- 我使用 org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 来摆脱延迟加载异常)。
- 我用 FetchType.JOIN 尝试了 CascadeType.EAGER。但没有改变结果。更新/插入查询没有出现在休眠统计信息(控制台日志)中。
-
我在 application-context.xml(spring-hibernate-configuration) 中使用以下属性进行休眠:
<property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory </prop> <prop key="hibernate.enable_lazy_load_no_trans">true</prop> <prop key="use_sql_comments">true</prop> <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop> <prop key="hibernate.connection.autocommit">true</prop> <prop key="net.sf.ehcache.configurationResourceName">/myehcache.xml</prop> </props> </property> -
和弹簧 txManager:
<!-- Enable the configuration of transactional behavior based on annotations --> <tx:annotation-driven transaction-manager="txManager" /> <!-- Transaction Manager is defined --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="SessionFactory" /> </bean>我使用 spring/security、hibernate 4.3.5.Final、jpa2.1、jsf2.2。提前致谢。
【问题讨论】:
-
这个jsf有什么关系?
-
不,你也没有使用 JPA API
-
你到底是什么意思?我使用 jpa 2.1 for hibernate 4.3.5.Final。你有什么建议来解决这个问题吗?谢谢@Neil-Stockton
-
您使用的是 Hibernate 的 API,而不是 JPA API。 JPA 中没有“会话”。 JPA 中没有任何限制,而且还在继续。
-
我该怎么做?
标签: spring hibernate orm hibernate-mapping