Hibernate Entity equals var null and not null in "same scope"

Viewed 149

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
------------------------------------------------
2 Answers

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())));

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.

Related