Home > Back-end >  Hibernate: Entity xx with id xx not found, one to one relationship
Hibernate: Entity xx with id xx not found, one to one relationship

Time:04-10

I am trying to use Hibernate for the first time, so I am not very familiar with the syntax yet. I am trying to model a library system. The classes that are causing problem are "Borrow" and "CopyBook" which are as following:

import javax.persistence.*;

@Entity
@Table(name = "copybook", schema="project")
public class CopyBook {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="copy_id", unique = true, nullable = false)
    private int copyId;

    @Convert(converter = StatusAttributeConverter.class)
    @Column(name="status", columnDefinition = "TEXT DEFAULT 'AVAILABLE'")
    private Status status;


    @ManyToOne(optional = false)
    @JoinColumn(name = "book_id")
    private Book book;

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    @OneToOne(mappedBy = "copy")
    private Borrow borrow;

    public Borrow getBorrow() {
        return borrow;
    }

    public void setBorrow(Borrow borrow) {
        this.borrow = borrow;
    }

    @OneToOne(mappedBy = "copy", optional = false)
    private Order order;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
}
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "borrow", schema = "project")
public class Borrow {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="no_borrow", unique = true, nullable = false)
    private int noBorrow;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="date_borrow", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", insertable = false, updatable = false, nullable = false)
    private Date dateBorrow;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="date_return")
    private Date dateReturn;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="max_date_return", insertable = false, updatable = false)
    private Date maxDateReturn;

    @OneToOne(optional = false)
    @JoinColumn(name = "copy_id")
    @NotFound(action = NotFoundAction.IGNORE)
    private CopyBook copy;

    public CopyBook getCopy() {
        return copy;
    }

    public void setCopy(CopyBook copy) {
        this.copy = copy;
    }

    @ManyToOne(optional = false)
    @JoinColumn(name="mem_id")
    private Members member;

    public Members getMember() {
        return member;
    }

    public void setMember(Members member) {
        this.member = member;
    }
}

I am using IntelliJ, so when I try to run the query of the Borrow entity it showed me that it cannot find CopyBook with id 1 even though it actually exists in the database

enter image description here

This is the entries currently present in my database (In these two table)

CopyBook:

CopyBook

Borrow:

enter image description here

As we can see, there is indeed a CopyBook with id of 1. This is also proved when I run the query of CopyBook entity which successfully returned all the results

Result after running in JPA select entity from CopyBook entity

enter image description here

Just to see what results Borrow is actually giving, I have added @NotFound(action = NotFoundAction.IGNORE). And this is the result I get

enter image description here

So I found this is a very bizarre situation because all data exist, and CopyBook is able to find its correspondent Borrow. But Borrow is unable to find its correspondent CopyBook ?

Strangley, I have another entity class Order which has almost the same attributes (Also has a OneToOne relationship with CopyBook), and it works perfectly.

CodePudding user response:

Problem Solved.

Because I have multiple One-To-Many relationship, and the FetchType is by default Eager. Whenever I want to retrieve the entity, it will perform Left Outer Join for all the entities until the end.

However, under the scope of this context. A book does not have to be ordered to exist in the library, be performing "JOIN" it loses entries and therefore causes the aforementioned problem.

As a result, all I did was to set the attributesOrder and Borrow with optional = true and fetch = Fetchtype.Lazy so it will not perform join unless necessary

  • Related