I'm struggle with equals with my hibernate entities...
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?!
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
------------------------------------------------