Hi!
I´m having a problem when calling Session.merge() on a transient object whicha has an embeddable component, and that component also references the transient object via the @Parent annotation.
Apparently, the problem occurs when hibernate is trying to obtain the proxy for the transient object, which doesn´t exist because the object has just been attached to the session.
Here is the example code:
Code:
@Entity()
@Table(name="parent")
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Embedded
private Child child;
public int getId() {
return id;
}
public void setId(final int id) {
this.id = id;
}
public Child getChild() {
return child;
}
public void setChild(final Child child) {
this.child = child;
}
}
Code:
@Embeddable
public class Child {
@Basic
private int value;
@org.hibernate.annotations.Parent
private Parent parent;
public int getValue() {
return value;
}
public void setValue(final int value) {
this.value = value;
}
public Parent getParent() {
return parent;
}
public void setParent(final Parent parent) {
this.parent = parent;
}
}
Code:
public class Merge {
public static void main(final String[] args) {
final SessionFactory sessFactory = initSessionFactory();
final Session session = sessFactory.openSession();
final Parent parent = new Parent();
final Child child = new Child();
parent.setChild(child);
child.setValue(1);
child.setParent(parent);
session.merge(parent);
}
private static SessionFactory initSessionFactory() {
final MyHibnerateConfiguration cfg = new MyHibnerateConfiguration();
final SessionFactory sessFactory = cfg.buildSessionFactory();
return sessFactory;
}
}
Here is the stacktrace of the exception:
Code:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.engine.StatefulPersistenceContext.proxyFor(StatefulPersistenceContext.java:670)
at org.hibernate.type.ComponentType.instantiate(ComponentType.java:492)
at org.hibernate.type.ComponentType.replace(ComponentType.java:459)
at org.hibernate.type.TypeFactory.replace(TypeFactory.java:515)
at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:377)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:179)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
at ar.com.mindset.hibernate.Merge.main(Merge.java:18)
Any light on this issue will be appreciated...
Thanks!!