I am getting javax.persistence.EntityNotFoundException error when I am trying to get User through Invoice object
invoice.getUser().getId()
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 @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; }
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.
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.
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.
I had the same problem, and
@NotFound(action = NotFoundAction.IGNORE)
solved my problem.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With