These problems occurs only when I'm using lazy collections.
Hibernate version: 3.1.3
Customer mapping:
<hibernate-mapping>
<class name="Customer" table="CUSTOMER">
<id name="id" column="CUSTOMER_ID">
<generator class="native"/>
</id>
<property name="firstName"/>
<property name="lastName"/>
<property name="company" not-null="true"/>
<bag name="notes" order-by="date desc" table="CUSTOMER_NOTES">
<key column="CUSTOMER_ID"/>
<many-to-many
column="NOTE_ID"
unique="true"
class="sk.akruks.kormoran.model.Note"/>
</bag>
<bag name="events" inverse="true">
<key column="CUSTOMER_ID"/>
<one-to-many class="Event"/>
</bag>
</class>
</hibernate-mapping>
Event mapping:
<hibernate-mapping>
<class name="sk.akruks.kormoran.model.Event" table="EVENT">
<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>
<many-to-one name="customer"
column="CUSTOMER_ID"
not-null="true"/>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="eventType"/>
<property name="eventTypeStr"/>
<property name="visitors"/>
<property name="note" type="text"/>
<property name="author"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Well I make my web application in Tomcat.
I used well knows HibernateUtil class, which does not work for my not lazy Bag.
When I try this:
Code:
getSession().beginTransaction();
p_event.setCustomer(customer);
customer.getEvents().add(p_event);
getSession().save(p_event);
getSession().getTransaction().commit();
I get exception
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
for this code:
Code:
getSession().beginTransaction();
Hibernate.initialize(customer.getEvents());
p_event.setCustomer(customer);
customer.getEvents().add(p_event);
getSession().save(p_event);
getSession().getTransaction().commit();
I get this exception
org.hibernate.HibernateException: disconnected session
org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:452)
org.hibernate.Hibernate.initialize(Hibernate.java:295)
I tried also filter as mentioned on
http://www.hibernate.org/43.html
In that case I get:
javax.servlet.ServletException: Transaction not successfully started
sk.akruks.kormoran.utils.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:62)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
root cause
org.hibernate.TransactionException: Transaction not successfully started
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
sk.akruks.kormoran.utils.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:39)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
Name and version of the database MySQL 5.0.21
JDBC: mysql-connector-java-3.1.12
Only way for me was to set lazy="false" :(
Could anyone help me?
How can I keep session open?
Also openNew session is not working for me.
Should I use JTA? I'm newbie, I'm affraid I cannot handle it..