Hi Friends,
I am a newbie to hibernate. Please consider the following info first:
Hibernate version:3.1
Mapping documents:
Code:
<hibernate-mapping>
<class name="com.raisonne.example.Honey" table="honey">
<id name="id" column="HONEY_ID" type="java.lang.Long">
<generator class="increment"/>
</id>
<property name="name" column="NAME" type="java.lang.String"/>
<property name="taste" column="TASTE" type="java.lang.String"/>
<idbag name="wives" table="wives" order-by="WIFE_INDEX asc" >
<collection-id type="long" column="WIFE_ID">
<generator class="increment">
</generator>
</collection-id>
<key column="HONEY_ID" not-null="true"></key>
<composite-element class="com.raisonne.example.Wives">
<parent name="Honey"/>
<property name="name" column="NAME"></property>
<property name="color" column="COLOR"></property>
<property name="wife_index" column="WIFE_INDEX"></property>
</composite-element>
</idbag>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public static void saveCollection(Collection<Honey> honeys){
Transaction tx =null;
try {
for (Iterator<Honey> iter =honeys.iterator();iter.hasNext();){
Honey singleHoney =(Honey)iter.next();
Session session =InitSessionFactory.getInstance().getCurrentSession();
tx =session.beginTransaction();
session.save(singleHoney);
tx.commit();
session.flush();
session.close();
}
}catch (HibernateException e){
e.printStackTrace();
if (tx !=null &&tx.isActive())
tx.rollback();
}finally{
InitSessionFactory.close();
}
}
Name and version of the database you are using:[MySql 5.0/b]
[b]The generated SQL (show_sql=true) and Debug level Hibernate log excerpt:Code:
19:21:41,156 INFO org.hibernate.impl.SessionFactoryObjectFactory {1}:82 -Not binding factory to JNDI, no JNDI name configured
19:21:41,671DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:1644 -trace com.mchange.v2.resourcepool.BasicResourcePool@15e2ccd [managed: 5, unused: 4, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1d381d2)
19:21:41,687DEBUG org.hibernate.SQL {1}:346 -select max(HONEY_ID) from honey
Hibernate: select max(HONEY_ID) from honey
19:21:41,765DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:457 -cxnStmtMgr.statementSet( com.mysql.jdbc.Connection@3c9c31 ).size(): 1
19:21:41,765DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:196 -checkoutStatement: com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 1; checked out: 1; num connections: 1; num keys: 1
19:21:41,875DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:271 -checkinStatement(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 1; checked out: 0; num connections: 1; num keys: 1
19:21:42,062DEBUG org.hibernate.SQL {1}:346 -/* insert com.raisonne.example.Honey */ insert into honey (NAME, TASTE, HONEY_ID) values (?, ?, ?)
Hibernate: /* insert com.raisonne.example.Honey */ insert into honey (NAME, TASTE, HONEY_ID) values (?, ?, ?)
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:457 -cxnStmtMgr.statementSet( com.mysql.jdbc.Connection@3c9c31 ).size(): 2
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:196 -checkoutStatement: com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 2; checked out: 1; num connections: 1; num keys: 2
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:271 -checkinStatement(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 2; checked out: 0; num connections: 1; num keys: 2
19:21:42,078DEBUG org.hibernate.SQL {1}:346 -select max(WIFE_ID) from wives
Hibernate: select max(WIFE_ID) from wives
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:457 -cxnStmtMgr.statementSet( com.mysql.jdbc.Connection@3c9c31 ).size(): 3
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:196 -checkoutStatement: com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 3; checked out: 1; num connections: 1; num keys: 3
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:271 -checkinStatement(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 3; checked out: 0; num connections: 1; num keys: 3
19:21:42,078DEBUG org.hibernate.SQL {1}:346 -/* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
Hibernate: /* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
19:21:42,093DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:457 -cxnStmtMgr.statementSet( com.mysql.jdbc.Connection@3c9c31 ).size(): 4
19:21:42,093DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:196 -checkoutStatement: com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 4; checked out: 1; num connections: 1; num keys: 4
19:21:42,093DEBUG org.hibernate.SQL {1}:346 -/* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
Hibernate: /* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
19:21:42,093DEBUG org.hibernate.SQL {1}:346 -/* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
Hibernate: /* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
19:21:42,093DEBUG org.hibernate.SQL {1}:346 -/* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
Hibernate: /* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
19:21:42,109DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:271 -checkinStatement(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 4; checked out: 0; num connections: 1; num keys: 4
19:21:42,125DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:297 -checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 4; checked out: 0; num connections: 1; num keys: 4
19:21:42,125DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:1644 -trace com.mchange.v2.resourcepool.BasicResourcePool@15e2ccd [managed: 5, unused: 4, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1d381d2)
19:21:42,125DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:297 -checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 4; checked out: 0; num connections: 1; num keys: 4
19:21:42,140 INFO org.hibernate.impl.SessionFactoryImpl {1}:729 -closing
19:21:42,156DEBUG com.mchange.v2.c3p0.management.ActiveManagementCoordinator {1}:133 -MBean: com.mchange.v2.c3p0:type=PooledDataSource[1hge13l7w1tj3nio1v6twje|1ff92f5] unregistered.
19:21:42,156DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner {1}:578 -Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main] interrupted. Shutting down.
19:21:42,156DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner {1}:578 -Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main] interrupted. Shutting down.
19:21:42,156DEBUG com.mchange.v2.c3p0.management.ActiveManagementCoordinator {1}:85 -C3P0Registry mbean unregistered.
[u]org.hibernate.SessionException: Session is closed![/u]
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:978)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.flush(Unknown Source)
at com.raisonne.example.TestExample.saveCollection(TestExample.java:192)
at com.raisonne.example.TestExample.main(TestExample.java:52)
19:21:42,171DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner {1}:578 -Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main] interrupted. Shutting down.
19:21:42,171DEBUG com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource {1}:417 -com.mchange.v2.c3p0.PoolBackedDataSource@1ff92f5 has been closed.
[u]java.lang.Exception: DEBUG STACK TRACE for [/u]PoolBackedDataSource.close().
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:417)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:429)
at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:359)
at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:335)
at org.hibernate.connection.C3P0ConnectionProvider.close(C3P0ConnectionProvider.java:118)
at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:759)
at com.raisonne.hibernate.InitSessionFactory.close(InitSessionFactory.java:108)
at com.raisonne.example.TestExample.saveCollection(TestExample.java:200)
at com.raisonne.example.TestExample.main(TestExample.java:52)
19:21:42,171DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:962 -Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
19:21:42,218DEBUG com.raisonne.example.TestExample {1}:65 -com.raisonne.example.Honey@15db13f
19:21:42,218DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:468 -Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
19:21:42,218DEBUG com.raisonne.example.TestExample {1}:66 -com.raisonne.example.Honey@1ab2b55
19:21:42,218DEBUG com.mchange.v2.c3p0.impl.NewPooledConnection {1}:566 -com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb closed by a client.
java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:566)
at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:234)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:470)
at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:964)
at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:989)
at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1174)
19:21:42,218DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:476 -Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
19:21:42,234DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:967 -Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
19:21:42,234DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:962 -Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@189c036
19:21:42,234DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:468 -Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@189c036
19:21:42,234DEBUG com.mchange.v2.c3p0.impl.NewPooledConnection {1}:566 -com.mchange.v2.c3p0.impl.NewPooledConnection@189c036 closed by a client.
I am try to store collection of data related to two entity classes i.e. Honey and Wives.
The common protocol is that: Honey can have no. of wives but a wife can only have one honey.
But in my application the wives table is not storing honey_id properly.
Please have a look into the log excerpt and any suggestions are most welcome.