I am getting javax.persistence.EntityNotFoundException error when I am trying to get User through Invoice object
invoice.getUser().getId()
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();
It should work when you add referencedColumnName = COLUMN
in @JoinColumns
of @ManyToOne
annotation
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;