Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

Java Spring how to check if two objects are equal and one of them is LAZY loaded

I try to compare two objects - one of them is LAZY loaded:

@Entity
public class Path{
 public void removePoint(Point point) {
if (point.getPath() != null && point.getPath() != this) {
            throw new IllegalArgumentException(String.format("Point %s does not belong to path%s", point, this));
        }
        point.setPath(null);
        this.getPoints().remove(point);
    }
}

Check:

window.getPath().removePoint(point);

The problem is that these two objects don't have the same reference: point.getPath() and this

The question is if the reason why these 2 obejcts have different references is that one of them is LAZY loaded (sth like Path$HibernateProxy$Di4Siuwn@32825)?

question from:https://stackoverflow.com/questions/66066158/java-spring-how-to-check-if-two-objects-are-equal-and-one-of-them-is-lazy-loaded

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If objects are lazily loaded then they will not be the actual entity class that you defined but rather a proxy implementation as you have now found Path$HibernateProxy$Di4Siuwn@32825 In general in java you shou use equals method to compare objects. Hibernate entities may require some additional treatment to help with equality comparison. Please review this article in detail https://howtodoinjava.com/hibernate/hibernate-entities-equality-and-identity/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...