英文:
Hibernate Entity equals var null and not null in "same scope"
问题
我在处理我的Hibernate实体中的equals
方法时遇到了困难...<p>
或者可以这么说:我不知道为什么Integer id
在toString
方法中被正确打印,但在相同的范围内System.out.println(id) -> null
为什么?<p>
我尝试了在实体配置中同时使用lazy
和eager
,但行为相同。
这是我的类:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
......
@Override
public boolean equals(Object o) {
System.out.println("------------------------------------------------");
System.out.println("THIS: " + getClass().toString());
System.out.println("O: " + o.getClass().toString());
System.out.println("THIS: " + getClass().getClassLoader().toString());
System.out.println("O: " + o.getClass().getClassLoader().toString());
if (this == o)
return true;
if (o == null)
return false;
if(!(o instanceof Workstation)){
return false;
}
Workstation that = (Workstation) o;
System.out.println("THIS TO STRING: " + this.toString());
System.out.println("THAT TO STRING: " + that.toString());
System.out.println("THIS ID: " + this.id);
System.out.println("THAT ID: " + that.id);
System.out.println("THIS ID: " + this.id + " T");
System.out.println("THAT ID: " + that.id + " T");
System.out.println("ERGEBNIS: " + (id.equals(that.id)));
System.out.println("------------------------------------------------");
return id.equals(that.id);
}
@Override
public String toString() {
if (name != null)
return id + " - " + name + " ";
return "FEHLER WORKSTATION";
}
@Override
public int hashCode() {
return Objects.hash(id);
}
这是输出结果:
------------------------------------------------
THIS: class com.wulf.system.mes.model.Workstation
O: class com.wulf.system.mes.model.Workstation$HibernateProxy$ycosLiwl
THIS: org.springframework.boot.devtools.restart.classloader.RestartClassLoader@66e4c786
O: org.springframework.boot.devtools.restart.classloader.RestartClassLoader@66e4c786
THIS TO STRING: 5 - Zuschnitt <- TO STRING
THAT TO STRING: 5 - Zuschnitt <- TO STRING -- ID is 5 BUT
THIS ID: 5
THAT ID: null <- if I want to print the id of Object "That" I get null WHY?!
THIS ID: 5 T
THAT ID: null T
ERGEBNIS: false
------------------------------------------------
英文:
I'm struggle with equals
with my hibernate entities...<p>
Or lets say: I dont know, why the Id Integer id
is printed correcty in the toString
but in the same scope System.out.println(id) -> null
WHY?!<p>
I tried bouth using lazy
and eager
in entityconfig, but same behavior.
Thats my class:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
.....
@Override
public boolean equals(Object o) {
System.out.println("------------------------------------------------");
System.out.println("THIS: " + getClass().toString());
System.out.println("O: " + o.getClass().toString());
System.out.println("THIS: " + getClass().getClassLoader().toString());
System.out.println("O: " + o.getClass().getClassLoader().toString());
if (this == o)
return true;
if (o == null)
return false;
if(!(o instanceof Workstation)){
return false;
}
Workstation that = (Workstation) o;
System.out.println("THIS TO STRING: " + this.toString());
System.out.println("THAT TO STRING: " + that.toString());
System.out.println("THIS ID: " + this.id);
System.out.println("THAT ID: " + that.id);
System.out.println("THIS ID: " + this.id + " T");
System.out.println("THAT ID: " + that.id + " T");
System.out.println("ERGEBNIS: " + (id.equals(that.id)));
System.out.println("------------------------------------------------");
return id.equals(that.id);
}
@Override
public String toString() {
if (name != null)
return id + " - " + name + " ";
return "FEHLER WORKSTATION";
}
@Override
public int hashCode() {
return Objects.hash(id);
}
Thats the sout:
------------------------------------------------
THIS: class com.wulf.system.mes.model.Workstation
O: class com.wulf.system.mes.model.Workstation$HibernateProxy$ycosLiwl
THIS: org.springframework.boot.devtools.restart.classloader.RestartClassLoader@66e4c786
O: org.springframework.boot.devtools.restart.classloader.RestartClassLoader@66e4c786
THIS TO STRING: 5 - Zuschnitt <- TO STRING
THAT TO STRING: 5 - Zuschnitt <- TO STRING -- ID is 5 BUT
THIS ID: 5
THAT ID: null <- if I want to print the id of Object "That" I get null WHY?!
THIS ID: 5 T
THAT ID: null T
ERGEBNIS: false
------------------------------------------------
答案1
得分: 4
注意日志中的Workstation类信息。第一个是您的Workstation类,另一个是由Hibernate生成的指向Workstation类的代理。
THIS:class com.wulf.system.mes.model.Workstation
O:class com.wulf.system.mes.model.Workstation$HibernateProxy$ycosLiwl
Hibernate生成的代理类将所有方法调用委托给目标对象。因此,当调用toString方法时,它会调用实际Workstation对象上的toString方法。真实的工作站对象确实具有其id和name值,并打印出来。
另一方面,当调用that.id时,它会尝试在代理类上获取id值。Hibernate代理类上的字段值始终为null。
解决方法
使用getter方法。
System.out.println("THAT ID:" + that.getId());
System.out.println("ERGEBNIS:" + (id.equals(that.getId())));
英文:
Notice the class info of Workstation in the logs. First one is your Workstation class and other one is a proxy to Workstation class which was generated by Hibernate.
THIS: class com.wulf.system.mes.model.Workstation
O: class com.wulf.system.mes.model.Workstation$HibernateProxy$ycosLiwl
The proxy class generated by Hibernate delegates all it's method calls to the target object. So, when toString method is invoked, it calls the toString method on the real Workstation object. The real workstation object does have id and name value with it and prints it.
On the other hand, when that.id is invoked, it tries and fetches the id value on the proxy class. The field values on the hibernate proxy class are always null.
Solution
Use the getter method.
System.out.println("THAT ID: " + that.getId());
System.out.println("ERGEBNIS: " + (id.equals(that.getId())));
答案2
得分: 0
所以答案非常简单 - 对象并未保存到数据库。在调用toString()时,ID在那时并未生成。
因此,首先尝试先保存实体,然后执行其findById操作,作为示例并调用toString。我相信在这种情况下,它不会为空。
英文:
So the answer is pretty simple - object is not saved into the database. Id is not generated at the time you call an ID in toString().
So try to save entity first and then perform its findById as an example and call toString. I am convinced that I will not be null in this case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论