EntityNotFoundException in Hibernate Many To One mapping however data exist

前端 未结 9 1470
庸人自扰
庸人自扰 2020-12-13 12:39

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

invoice.getUser().getId()

Error is as follo

相关标签:
9条回答
  • 2020-12-13 12:57

    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();
    
    0 讨论(0)
  • 2020-12-13 13:00

    It should work when you add referencedColumnName = COLUMN in @JoinColumns of @ManyToOne annotation

    0 讨论(0)
  • 2020-12-13 13:07

    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;
    
    0 讨论(0)
提交回复
热议问题