EntityNotFoundException in Hibernate Many To One m

2019-01-23 01:57发布

问题:

I am getting javax.persistence.EntityNotFoundException error when I am trying to get User through Invoice object

invoice.getUser().getId()

Error is as follows

javax.persistence.EntityNotFoundException: Unable to find com.indianretailshop.domain.User with id 5
    at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:137)
    at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:189)
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:178)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)

Entity classes are as follows(getters and setters are not included)

@Entity
@Table(name="users")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(unique=true, nullable=false)
    private int id;

    .
        .
        .

    //bi-directional many-to-one association to Invoice
    @OneToMany(mappedBy="user")
    private List<Invoice> invoices;
}

@Entity
@Table(name="invoice")
public class Invoice implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(unique=true, nullable=false)
    private int id;
        .
        .
        .

    //bi-directional many-to-one association to User
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="Users_id")
    private User user;
}

回答1:

The problem could be that the direct entity does not exist, but also it could be that the referenced entity from that entity, normally for a EAGER fetch type, or optional=false.

Try this:

     //bi-directional many-to-one association to User
     @ManyToOne(fetch=FetchType.LAZY)
     @JoinColumn(name="Users_id")
     private User user = new User();


回答2:

I had the same problem, and

@NotFound(action = NotFoundAction.IGNORE)

solved my problem.



回答3:

If you use @ManyToOne, the referenced entity must exist. The only other option is to specify that field as a long and retrieve the referenced entity by means of a separate query.

Throws an exception (javax.persistence.EntityNotFoundException) instead of returning null if it can't find the requested entity.

Use @NotFound annotation to resolve this exception if you are lazy loading and not handling this exception manually.

 @ManyToOne(
        fetch = FetchType.LAZY)
    @NotFound(
        action = NotFoundAction.IGNORE)
    @JoinColumn(
        name = COLUMN,
        referencedColumnName = COLUMN,
        insertable = false,
        updatable = false)
    private Table table;


回答4:

I had the similar issue recently, but the problem is that record is always existed in DB. So I did some investigation, found out that at some point that cached entity is marked as removal and reload failed before reloading from DB. It happened in Hibernate 4.3.5 Final for me Then I upgrade to Hibernate 4.3.11 Final which seems to fix the issue.



回答5:

Not sure if that applies to your case.

But I had a similar issue where I updated directly in the database table X and set a field null, but in java class the field was @NotNull.

So when another class Y had a X object with ManyToOne, it wasn't able to load the entity because of the null value in the entity.



回答6:

please try the following

@OneToMany(mappedBy="yourMappingattributeName",cascade=CascadeType.ALL) or

@OneToMany(mappedBy="yourMappingattributeName",cascade=CascadeType.MERGE)