I've implemented equals()/hashCode() methods as suggested in an older topic on stackoverflow. The problem of that approach is the exception
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:160) [hibernate-core-4.1.6.Final.jar:4.1.6.Final]
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:259) [hibernate-core-4.1.6.Final.jar:4.1.6.Final]
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) [hibernate-core-4.1.6.Final.jar:4.1.6.Final]
at pkg.db.TblChain_$$_javassist_815.equals(TblChain_$$_javassist_815.java) [classes:]
[...]
which is thrown BEFORE equals() is invoked. It only happens, when the entity has e.g. foreign keys to other entities. Hibernate tries to fetch those entities and triggers the exception because of the closed session (equals() is invoked in a new JSF request, EntityManager is request scoped).
The guy of the solution never hit that problem that's why I'm asking you for help.
Edit 14.04.2015 14:50: Both equals() are implemented using the ID of the entity. But for this example I've replaced its implementations with a simple return, as it doesn't matter. I've found out that it doesn't matter if I have another foreign entity (e.g. tblChainType) in the foreign entity (tblChain). It will always fail, if the first one was not used while the entity manager, that was used for loading, was active.
// Entity classes
public class TblChainInstance {
private TblChain tblChain; // Foreign entity
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "intChainId", nullable = false)
public TblChain getTblChain() {
return this.tblChain;
}
public boolean equals(Object other) {
return false; // doesn't matter, what I have here.
// It always works if this is the selected Entity.
}
}
public class TblChain {
public boolean equals(Object other) {
return false; // doesn't matter, what I have here.
// It always throws a LazyInitializationException
}
}
// Testcode
@Named @ViewScoped
public class MyBean implements Serializable {
private TblChainInstance _tblChainInstance;
@PostConstruct
public void _init() {
_tblChainInstance = new JPAQuery(_entityManager).from(qtChainInstance)
.limit(1).singleResult(qtChainInstance);
}
public void actionListener() {
System.out.println(_tblChainInstance.equals(1)); // outputs false
System.out.println(_tblChainInstance.getTblChain().equals(1)); // throws
}
}