When I try to save(persist) a many-to-one relationship with a foreign key as a primary key, I get the following exception listed below.
My goal is to have a one-to-one relationship between users and profiles and be able to use lazy loading. I have read the following
http://www.hibernate.org/162.html and I'm trying to accomplish the method given in the comments section by "Regis Pires Magalhaes"
Hibernate version:
3.0
Mapping documents:
Code:
<class name="com.familyoven.user.User" table="users">
<id name="id" column="ID">
<generator class="identity"/>
</id>
<many-to-one column="id" cascade="all" name="profile" class="com.familyoven.user.Profile"
unique="true" />
</class>
<class name="com.familyoven.user.Profile" table="users_profiles">
<id name="id" column="id">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<one-to-one name="user" class="com.familyoven.user.User" constrained="true" />
</class>
Code between sessionFactory.openSession() and session.close():Code:
User user = new User();
user.setName('George Bush');
Profile profile = new Profile();
profile.setUser( user );
user.setProfile( profile );
session.save( user );
Full stack trace of any exception that occurs:Code:
org.hibernate.id.IdentifierGenerationException: null id generated for:class com.familyoven.user.Profile
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:95)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:531)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:523)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:134)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:385)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:242)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:559)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:543)
at sun.reflect.GeneratedMethodAccessor162.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.save(Unknown Source)
Name and version of the database you are using:mysql 5.0
Code:
Code: