EntityNotFoundException in Hibernate Many To One mapping however data exist
I had the same problem, and
@NotFound(action = NotFoundAction.IGNORE)
solved my problem.
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.
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;
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();