| Assuming we have 2 entities:
@Entity(name = "Parent")
public static class Parent {
private Integer id;
private String name;
private Parent() {
name = "Empty";
}
public Parent(String s) {
this.name = s;
}
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity(name = "Child")
public static class Child {
private Integer id;
private Parent parent;
public Child() {
this.parent = new Parent( "Name" );
}
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
And we execute the following test case:
Child child = new Child();
EntityManager entityManager = null;
EntityTransaction txn = null;
try {
entityManager = createEntityManager();
txn = entityManager.getTransaction();
txn.begin();
entityManager.persist( child );
txn.commit();
entityManager.clear();
Integer childId = child.getId();
Child childReference = entityManager.getReference( Child.class, childId );
try {
assertEquals( child.getParent().getName(), childReference.getParent().getName() );
}
catch (Exception expected) {
assertEquals( NoSuchMethodException.class, ExceptionUtil.rootCause( expected ).getClass() );
}
}
catch (Throwable e) {
if ( txn != null && txn.isActive() ) {
txn.rollback();
}
throw e;
}
finally {
if ( entityManager != null ) {
entityManager.close();
}
}
The current log message looks like this:
java.lang.NoSuchMethodException: org.hibernate.jpa.test.callbacks.PrivateConstructorTest$Parent$HibernateProxy$JqCA43Dt.<init>()
We should provide a more meaningful exception message to indicate that the default private constructor interferes with the Proxy mechanism, and the user should use the public, protected or package-private visibility instead. |