Hibernate中的EntityNotFoundException多对一映射,但存在数据 [英] EntityNotFoundException in Hibernate Many To One mapping however data exist

查看:927
本文介绍了Hibernate中的EntityNotFoundException多对一映射,但存在数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



invoice.getUser()。getId()

我得到javax.persistence.EntityNotFoundException错误,当我试图通过发票对象获取用户时,

错误如下



  javax.persistence.EntityNotFoundException:无法找到com。 (org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java):
org.hibernate.ejb.Ejb3Configuration $ Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:137)
org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java: )
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)



实体类如下(不包括getter和setter)



 @Entity 
@Table(name =users)
public class User实现Serializable {
private s tatic final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(unique = true,nullable = false)
private int id;





//双向多对一关联到发票
@OneToMany(mappedBy =user)
private List< Invoice>发票;

$ b $实体
@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;




//与用户
的双向多对一关联
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =Users_id)
私人用户用户;


解决方案

问题可能在于直接实体不存在,但也可能是来自该实体的引用实体,通常为EAGER提取类型,或可选= false。尝试此操作: / p>

  //与用户
的双向多对一关联$ {$ b @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =Users_id)
private User user = new User();


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

invoice.getUser().getId()

Error is as follows

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 classes are as follows(getters and setters are not included)

@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;
}

解决方案

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();

这篇关于Hibernate中的EntityNotFoundException多对一映射,但存在数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆