Bad*_*ngh 43 spring hibernate jpa spring-data-jpa
我试图通过Invoice对象获取用户时收到javax.persistence.EntityNotFoundException错误
invoice.getUser().的getId()
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)
Run Code Online (Sandbox Code Playgroud)
@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;
}
Run Code Online (Sandbox Code Playgroud)
oze*_*ray 85
我有同样的问题,而且
@NotFound(action = NotFoundAction.IGNORE)
Run Code Online (Sandbox Code Playgroud)
解决了我的问题.
sha*_*lee 30
如果使用@ManyToOne,则引用的实体必须存在.唯一的另一种选择是将该字段指定为long并通过单独的查询检索引用的实体.
如果找不到请求的实体,则抛出异常(javax.persistence.EntityNotFoundException)而不是返回null.
如果您是延迟加载而不是手动处理此异常,请使用@NotFound批注来解决此异常.
@ManyToOne(
fetch = FetchType.LAZY)
@NotFound(
action = NotFoundAction.IGNORE)
@JoinColumn(
name = COLUMN,
referencedColumnName = COLUMN,
insertable = false,
updatable = false)
private Table table;
Run Code Online (Sandbox Code Playgroud)
raj*_*wat 22
问题可能是直接实体不存在,但也可能是来自该实体的引用实体,通常用于EAGER提取类型,或者可选= false.
试试这个:
//bi-directional many-to-one association to User
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="Users_id")
private User user = new User();
Run Code Online (Sandbox Code Playgroud)
小智 5
不知道这是否适用于您的情况。
但是我有一个类似的问题,我直接在数据库表X中进行了更新,并将字段设置为null,但是在java类中,该字段为@NotNull。
因此,当另一个类Y的ManyToOne对象具有X对象时,由于该实体中的值为null,因此无法加载该实体。
小智 5
请尝试以下操作
@OneToMany(mappedBy="yourMappingattributeName",cascade=CascadeType.ALL)
或者
@OneToMany(mappedBy="yourMappingattributeName",cascade=CascadeType.MERGE)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
70523 次 |
最近记录: |