Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hi,
im new with Hibernate and i'm facing the following problem.
I retrieve somes Data and wan to store the result in my DB.
I have following relations:
AttributeGroupDef(attGroupDefID, parentAttDefID, deleted, groupPosition) with self reference[Field: parentAttDefID] and
AttributeDef(attDefID, attGroupDefID, active, .... attPosition). The field attGroupDefID should be autoincrement and set by the DB.
The attGroupDefID field is referenced in many other tables.
So when trying to insert my retrieved objects i become the Error listed above.
I read on the hibernate-forum it could come from the autoincrement field attGroupDefID of attributeGroupDef. I changed the mapping file and set unique="true" to say the field AttGroupDefID references a PK and insert="true" because in the past i got Errors like
"org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: "
So can some one help me hier with some tips howto solve this problem.
Thanks in advance.
Bertrand.
Hibernate version:
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.db.common.model">
<class name="AttributeGroupDef" table="AttributeGroupDef">
<id name="attGroupDefID" type="java.lang.Integer" >
<column name="attGroupDefID" sql-type="int" not-null="true"/>
<generator class="increment"/>
</id>
<many-to-one
name="parentAttDef"
class="AttributeGroupDef"
column="parentAttDefID"
cascade="all"
lazy="false"
unique="true"
insert="true"
/>
<property name="deleted" type="java.lang.Integer" >
<column name="deleted"/>
</property>
<property name="groupPosition" type="java.lang.Integer" >
<column name="groupPosition"/>
</property>
<set
name="subAttGroups"
table="AttributeGroupDef"
cascade="save-update"
inverse="true"
lazy="false">
<key column="parentAttDefID"/>
<one-to-many class="AttributeGroupDef"/>
</set>
<set
name="childAttributes"
table="AttributeDef"
cascade="save-update"
inverse="true"
lazy="false">
<key column="attGroupDefID"/>
<one-to-many class="AttributeDef"/>
</set>
<list name="translationList" table="Translation">
<key column="attGroupDefID"/>
<index column="translationID"/>
<one-to-many class="com.db.common.model.Translation"/>
</list>
</class>
<sql-query name="getSpecificAttGroupDef">
<return alias="agd" class="AttributeGroupDef"/>
SELECT {agd.*}
FROM AttributeGroupDef agd WHERE agd.attGroupDefID = :agdID
</sql-query>
<sql-query name="getAllFromAttGroupDef">
<return alias="agd" class="AttributeGroupDef"/>
SELECT {agd.*}
FROM AttributeGroupDef agd
</sql-query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public class StoreAttGroup {
Session session;
Transaction transaction;
public StoreAttGroup(){
session = HibernateUtil.currentSession();
transaction = session.beginTransaction();
AttributeGroupDef attGroupDef = new AttributeGroupDef();
}
public void storeTODataBase(List attGroupDefList){
try
{
AttributeGroupDef agd;
for (int i=0; i< attGroupDefList.size(); i++){
agd = (AttributeGroupDef)attGroupDefList.get(i);
if(agd != null) {
session.save(agd);
transaction.commit();
System.out.println("I have saved the " + i+ " AttibuteGroupDef in the DB");
}
}
//transaction.commit();
}
catch (Exception e) {
if (transaction != null) transaction.rollback();
e.printStackTrace();
}
finally{HibernateUtil.closeSession(); }
}
Full stack trace of any exception that occurs:
ERROR [main] (JDBCExceptionReporter.java:72) - [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Ein expliziter Wert für die Identitätsspalte kann in der AttributeGroupDef-Tabelle nicht eingefügt werden, wenn IDENTITY_INSERT auf OFF festgelegt ist.
ERROR [main] (AbstractFlushingEventListener.java:277) - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [com.db.common.model.AttributeGroupDef]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1859)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2190)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:726)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:320)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at com.admin.upload.xml.StoreAttGroup.storeTODataBase(StoreAttGroup.java:32)
at com.admin.upload.xml.TreeWalkAttGroupTest.setUp(TreeWalkAttGroupTest.java:29)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Ein expliziter Wert für die Identitätsspalte kann in der AttributeGroupDef-Tabelle nicht eingefügt werden, wenn IDENTITY_INSERT auf OFF festgelegt ist.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1843)
... 22 more
org.hibernate.exception.ConstraintViolationException: could not insert: [com.db.common.model.AttributeGroupDef]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1859)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2190)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:726)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:320)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at com.admin.upload.xml.StoreAttGroup.storeTODataBase(StoreAttGroup.java:32)
at com.admin.upload.xml.TreeWalkAttGroupTest.setUp(TreeWalkAttGroupTest.java:29)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Ein expliziter Wert für die Identitätsspalte kann in der AttributeGroupDef-Tabelle nicht eingefügt werden, wenn IDENTITY_INSERT auf OFF festgelegt ist.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1843)
... 22 more
Name and version of the database you are using:
The generated SQL (show_sql=true):
I don't know exactly where can i find the generated SQL (show_sql=true) ?
Debug level Hibernate log excerpt: