相关疑难解决方法(0)

OneToOne + No Foreginkey + Unidirectional + Datamissing => EntityNotFoundException

在遗留系统中,我们有1个表(实体)和1个视图(UserDetail),没有约束但有关系.

Entity
| Id | Desc | Created_By |...

UserDetail
| UserId | F_Name | L_Name |...
Run Code Online (Sandbox Code Playgroud)

CreatedBy具有创建该实体的用户的用户标识.我们在Entity.java中有一个单向映射,如下所示.

@OneToOne
@JoinColumn(name = "CREATED_BY", referencedColumnName = "USER_ID")
private UserDetail createdBy;
Run Code Online (Sandbox Code Playgroud)

问题

但由于它是一个遗留系统,我们无法控制它并且用户被硬删除.

调用entityReposity.findAll时,将引发以下错误

org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com...UserDetail with id 92237; nested exception is javax.persistence.EntityNotFoundException: Unable to find com...UserDetail with id 92237
Run Code Online (Sandbox Code Playgroud)

如何让JPA认为这种关联是可选的,或者在JPA中有什么方法可以忽略它(比如Hibernate中的@NotFound)?

试着

  1. @NotFound有效,但我们需要将@OneToOne破解为@ManyToOne.我们还想要一个独立于实现的解决方 注意到这个错误,https://hibernate.atlassian.net/browse/ANN-725

  2. optional = true - 如果这可以按照它应该如何表现的语法含义工作,我就不会写这个.

  3. 还尝试了UserDetails.java中的postLoad().

  4. @PostLoad解决方案我们有一个异常的异常处理程序,并且在调用Entity类中的@PostLoad之前会捕获异常.

在Entity.java中

    @PostLoad
    public void postLoad(){
        try {
            System.out.println(this.getCreatedBy()); //Before control comes here, it goes to aop …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa one-to-one oracle11g

9
推荐指数
1
解决办法
2869
查看次数

如果找不到实体,则为空对象

我正在使用Hibernate和JPA.我有一个叫做Customer引用的实体ParentCustomer:

public class Customer {
    @Id
    @GeneratedValue
    @Column(name = "CustomerID")
    private int id;

    @ManyToOne
    @JoinColumn(name = "ParentCustomerID")
    private Customer parent;

    // ...
}
Run Code Online (Sandbox Code Playgroud)

但在我的数据库中有一些客户没有父母所以ParentCustomerID设置为0.我测试我的课时得到的例外是:

javax.persistence.EntityNotFoundException: Unable to find it.keyforup.pat.data.entities.Customer with id 0

有没有一种方法来设置ParentCustomer,以null当id为0

java hibernate jpa

6
推荐指数
1
解决办法
8614
查看次数

标签 统计

hibernate ×2

java ×2

jpa ×2

one-to-one ×1

oracle11g ×1