dimanche 29 mars 2015

how to update many-to-many collection (lazy-loading) in hibernate/spring?

I have three tables and two classes.there are many-to-many relationship between the two tables/classes.



OYS_USER -->Many-to-Many with 'oys_lesson'

OYS_LESSON-->Many-to-Many with 'oys_user'

OYS_LESSON_STUDENT-->relationship


in 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;


in 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;


I updated collection with below codes:



@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);
}


When I added current user to lesson's collection (or lesson to current user's collection).

update/insert query does not appear in hibernate statistics(console-log).

So a record can not be added to any collection.

Basically,I want to add new object to many to many collection.What am I doing wrong?



  • I use hibernate 4.3.5.Final (to get rid of lazy loading exception)

  • I use org.springframework.orm.hibernate4.support.OpenSessionInViewFilter for rid of lazy loading exception).

  • I tried CascadeType.EAGER with FetchType.JOIN.But did not change the results.update/insert query does not appear in hibernate statistics(console-log).


  • I use below properties for hibernate in 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>



  • and spring 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>


    I use spring/security,hibernate 4.3.5.Final,jpa2.1,jsf2.2. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire