Hibernate equals and proxy

后端 未结 4 1848
忘了有多久
忘了有多久 2020-12-29 09:12

I have one BaseEntity which abstracts id and version property. this class also implements hashcode and equals based on PK (id) property.

BaseEntity{

    Lo         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-29 10:10

    This is mostly an effect of standard Java inheritance.

    a1.getB().equals(b1) uses Object.equals() (except if you've overridden equals() in your class), which only returns true if a1.getB() and b1 are the same instance. I don't know what you've done exactly (your code formatting is broken), but it looks like you've loaded a again in a different session, so you get a new instance for a and a.getB(), and consequently Object.equals() returns false.

    a1.getB().getId().equals(b1.getId()) uses Long.equals(), which returns true if the long values are the same (even for different instances of the Long object), and these values are obviously the same.

提交回复
热议问题