Is it possible to have a one_to_one with sharing of the primary key, and the primary key of the "child" entity to be set automatically when saving the parent entity?
Code:
Parent parent = new Parent();
parent.oneChild = new OneChild();
parent.oneChild.parent = parent; // setting the reverse relation
// here fails with stack trace as described above
session.save(parent);
Code:
public class Parent {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
Long parentId;
@OneToOne(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
Child child;
}
public class Child {
@OneToOne(mappedBy="child")
Parent parent;
@Id
Long childId;
}
I have been able to "walk around" this problem by using some specific Hibernate id generators, but... this ties me to hibernate. How can I achieve the desired result without using hibernate specific stuff? (this is how I made it work)
Code:
@Id
@GeneratedValue(generator="property_generator")
@GenericGenerator(name="property_generator",strategy="foreign",
parameters = @Parameter(name="property",value="deviz"))
Long childId;
Thanks,
Alex
Hibernate version: 3.2
Full stack trace of any exception that occurs:
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.intelisoft.redsand.domain.TaxeDeviz
at org.hibernate.id.Assigned.generate(Assigned.java:33)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
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:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:437)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:326)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
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:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
Name and version of the database you are using: PostgreSQL 8