EntityNotFoundException in Hibernate Many To One mapping however data exist

前端 未结 9 1499
庸人自扰
庸人自扰 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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;
    

提交回复
热议问题