Hi,
I would like to make a simple bidirectionnal association between my users and their categories.
- I have 2 classes: User and Category. User has a Set of Category and Category has a Set of User.
- In my DB, I have 3 classes: T_USER, T_GROUP and T_GROUP_N_USER.
T_GROUP_N_USER has only 2 foreign keys of type varchar, which reference to the primary key of T_USER and T_GROUP
Hibernate seems to generate the right SQL request:
Code:
insert into T_GROUP (Name, Code) values (?, ?)
insert into T_USER (User_Name, User_Login) values (?, ?)
insert into T_GROUP_N_USER(Group_Code, User_Login) values (?, ?)
But I can't figure out why oracle can't find the parent key of the foreign key in T_GROUP_N_USER. (as far as I understand the stack trace..)
I spent a _long_ time searching for this problem, without success. I would very very much appreciate any help of any kind.
- Are my mapping files correct?
- Is my main code correct too?
- Do you know where I could look to find the solution to this problem? (I spent a long time in the documentation and the FAQ in vain...)
Thanks very very much !
Hibernate version:
3.0
Mapping documents:
I have 2 mapping files:
category.hbm.xml: Code:
<class name="business.Category" table="T_GROUP">
<id name="code" column="Code">
<generator class="assigned"/>
</id>
<property column="Name" name="name"/>
<set name="users" table="T_GROUP_N_USER" inverse="false" lazy="true" outer-join="true">
<key column="Group_Code"/>
<many-to-many class="business.User" column="User_Login"/>
</set>
</class>
user.hbm.xml: Code:
<class name="business.User" table="T_USER">
<id column="User_Login" name="login">
<generator class="assigned"/>
</id>
<property column="User_Name" name="name"/>
<set name="categories" table="T_GROUP_N_USER" inverse="true" lazy="true" outer-join="true">
<key column="User_Login"/>
<many-to-many class="business.Category" column="Group_Code"/>
</set>
</class>
Code between sessionFactory.openSession() and session.close():
Code:
Configuration cf = new Configuration();
SessionFactory sessions = cf.configure().buildSessionFactory();
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
Category cat = new Category();
User usr = new User();
/***I put fake values here to have something to add in the DB ***/
cat.getUsers().add(usr);
usr.getCategories().add(cat);
cat.setName("catName1");
cat.setCode("catCode1");
usr.setLogin("usrLogin1");
usr.setName("usrName1");
cat.setUsers(usr.getCategories());
usr.setCategories(cat.getUsers());
session.save(cat);
session.save(usr);
tx.commit();
session.close();
Full stack trace of any exception that occurs:
[5/3/05 13:47:29:707 EDT] 24539f91 SystemOut O Hibernate: insert into T_GROUP (Name, Code) values (?, ?)
[5/3/05 13:47:29:754 EDT] 24539f91 SystemOut O Hibernate: insert into T_USER (User_Name, User_Login) values (?, ?)
[5/3/05 13:47:29:754 EDT] 24539f91 SystemOut O Hibernate: insert into T_SMART_GROUP_N_USER_TEMP (Group_Code, User_Login) values (?, ?)
[5/3/05 13:47:29:786 EDT] 24539f91 JDBCException W org.hibernate.util.JDBCExceptionReporter SQL Error: 2291, SQLState: 23000
[5/3/05 13:47:29:786 EDT] 24539f91 JDBCException E org.hibernate.util.JDBCExceptionReporter ORA-02291:
integrity constraint (DWADMIN.SMART_GROUP_N_USER_TEMP_FK2) violated - parent key not found
[5/3/05 13:47:29:786 EDT] 24539f91 JDBCException W org.hibernate.util.JDBCExceptionReporter SQL Error: 2291, SQLState: 23000
[5/3/05 13:47:29:786 EDT] 24539f91 JDBCException E org.hibernate.util.JDBCExceptionReporter ORA-02291: integrity constraint (DWADMIN.SMART_GROUP_N_USER_TEMP_FK2) violated - parent key not found
[5/3/05 13:47:29:801 EDT] 24539f91 AbstractFlush E org.hibernate.event.def.AbstractFlushingEventListener Could not synchronize database state with session
[5/3/05 13:47:29:801 EDT] 24539f91 AbstractFlush E org.hibernate.event.def.AbstractFlushingEventListener TRAS0014I: The following exception was logged org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:179)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
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:675)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at application.HibernateTest.init(HibernateTest.java:74)
at javax.servlet.GenericServlet.init(GenericServlet.java:258)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doInit(StrictServletInstance.java:82)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._init(StrictLifecycleServlet.java:147)
at com.ibm.ws.webcontainer.servlet.PreInitializedServletState.init(StrictLifecycleServlet.java:270)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.init(StrictLifecycleServlet.java:113)
at com.ibm.ws.webcontainer.servlet.ServletInstance.init(ServletInstance.java:189)
at javax.servlet.GenericServlet.init(GenericServlet.java:258)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.addServlet(WebAppServletManager.java:870)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.loadServlet(WebAppServletManager.java:224)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.loadAutoLoadServlets(WebAppServletManager.java:542)
at com.ibm.ws.webcontainer.webapp.WebApp.loadServletManager(WebApp.java:1277)
at com.ibm.ws.webcontainer.webapp.WebApp.init(WebApp.java:283)
at com.ibm.ws.webcontainer.srt.WebGroup.loadWebApp(WebGroup.java:387)
at com.ibm.ws.webcontainer.srt.WebGroup.init(WebGroup.java:209)
at com.ibm.ws.webcontainer.srt.WebGroup.reload(WebGroup.java:1200)
at com.ibm.ws.webcontainer.WebContainer.reload(WebContainer.java:1072)
at com.ibm.ws.runtime.component.WebContainerImpl.reload(WebContainerImpl.java:380)
at com.ibm.ws.runtime.component.ApplicationMgrImpl$ApplicationNotifier.classChanged(ApplicationMgrImpl.java:637)
at com.ibm.ws.classloader.ClassLoaderManager.checkAndNotify(ClassLoaderManager.java:399)
at com.ibm.ws.classloader.ClassLoaderManager.access$100(ClassLoaderManager.java:43)
at com.ibm.ws.classloader.ClassLoaderManager$ReloadTimerTask.run(ClassLoaderManager.java:435)
at java.util.TimerThread.mainLoop(Timer.java:446)
at java.util.TimerThread.run(Timer.java:396)
Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (DWADMIN.GROUP_N_USER_FK2) violated - parent key not found
Name and version of the database you are using:
oracle 9i
The generated SQL (show_sql=true):
[5/3/05 13:47:29:707 EDT] 24539f91 SystemOut O Hibernate: insert into T_GROUP (Name, Code) values (?, ?)
[5/3/05 13:47:29:754 EDT] 24539f91 SystemOut O Hibernate: insert into T_USER(User_Name, User_Login) values (?, ?)
[5/3/05 13:47:29:754 EDT] 24539f91 SystemOut O Hibernate: insert into T_GROUP_N_USER(Group_Code, User_Login) values (?, ?)
Debug level Hibernate log excerpt: