如何用JPA解决javax.persistence.EntityNotFoundException(不是使用@NotFound) [英] How solve javax.persistence.EntityNotFoundException with JPA (not by using @NotFound)

查看:4408
本文介绍了如何用JPA解决javax.persistence.EntityNotFoundException(不是使用@NotFound)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用JPA从数据库加载一些东西。一些实体之间可能有可选的关系,例如

  @Entity 
public class First {
.. ..
@OneToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH,CascadeType.DETACH})
@JoinColumns(value = {
JoinColumn(name =A_ID ,referenceColumnName =A_ID,insertable = false,updatable = false),
JoinColumn(name =B_ID,referencedColumnName =B_ID,insertable = false,updatable = false)})
private Second第二;

当这个关联存在于数据库中时,一切正常。当它不是,我得到一个 javax.persistence.EntityNotFoundException

我想要的是,而不是例外,如果该字段为NULL协会不存在。


我尝试了几种不同的东西,例如在关系注释中使用optional = true(其中btw是默认选项),将其设置为Nullable等等。似乎没有什么可以做的,似乎所有这些选项都被忽略。

我发现很多提到这个问题的链接(和一些问题在这里在stackoverflow中),但是在所有这些问题中,建议使用 Hibernate的@NotFound 注释。但我们不希望对Hibernate有任何依赖(我们希望保留所有纯粹的JPA)。



非常感谢您的全力帮助!

解决方案

以下是此问题的替代解决方案。我不得不在一个旧的数据库之上构建关系有时是腐败的。这是我通过仅使用JPA来解决它的方式。

  @PostLoad 
public void postLoad(){
try {
if(getObject )!= null&& getObject()。getId()== 0){
setObject(null);


catch(EntityNotFoundException e){
setObject(null);
}
}


We are using JPA to load some stuff from a database. Some entities may have optional relationships between them, e.g.

@Entity
public class First {
    ....
    @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
    @JoinColumns(value = {
        JoinColumn(name = "A_ID", referencedColumnName = "A_ID", insertable = false, updatable = false), 
        JoinColumn(name = "B_ID", referencedColumnName = "B_ID", insertable = false, updatable = false)})
    private Second second;

When this association is present in the database, everything is working fine. When it's not, I'm getting a javax.persistence.EntityNotFoundException
What I want is instead of the exception to have this field as NULL if the association is not present.

I have tried several different things, e.g. using the optional=true in the relationship annotation (which btw is the default option), putting it as Nullable, etc. Nothing seems to do the trick, it seems like all these options are being ignored.

I found a lot of links mentioning this very same problem (and some questions here in stackoverflow) but in all of them the suggestion is to use the @NotFound annotation from Hibernate. But we do NOT want to have any dependencies to Hibernate (we want to keep everything pure JPA).

Does any of you guys know any other way to solve this?

Many thanks for all your help!

解决方案

Below is an alternative solution for this problem. I had to build on top of an old database where the relations sometimes were corrupt. This is how I solved it by only using JPA.

@PostLoad
public void postLoad(){
    try {
        if(getObject() != null && getObject().getId() == 0){
            setObject(null);
        }
    }
    catch (EntityNotFoundException e){
        setObject(null);
    }
} 

这篇关于如何用JPA解决javax.persistence.EntityNotFoundException(不是使用@NotFound)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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