Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityNotFoundException in Hibernate Many To One mapping however data exist

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; } 
like image 488
Badal Singh Avatar asked Nov 24 '12 06:11

Badal Singh


People also ask

How do I fix EntityNotFoundException?

If this is not the case, there is a workaround to ignore the missing entity. Combining @NotFound(action = NotFoundAction. IGNORE) with @ManyToOne annotation will stop the persistence provider from throwing the EntityNotFoundException, but we'll have to handle missing entity by hand to avoid NullPointerException.

How do you map a one to many relationship in hibernate?

The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. The <many-to-one> element is used to set the relationship between EMPLOYEE and ADDRESS entities. The name attribute is set to the defined variable in the parent class, in our case it is address.

What is @OneToMany in Java?

A OneToMany relationship in Java is where the source object has an attribute that stores a collection of target objects and if those target objects had the inverse relationship back to the source object it would be a ManyToOne relationship.


2 Answers

I had the same problem, and

@NotFound(action = NotFoundAction.IGNORE) 

solved my problem.

like image 50
ozeray Avatar answered Sep 22 '22 22:09

ozeray


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; 
like image 20
shane lee Avatar answered Sep 22 '22 22:09

shane lee