简体   繁体   English

持久化对象时分离的实体异常

[英]Detached entity exception when persisting object

I have a problem with detached entity passed to persistence. 我对传递给持久性的分离实体有疑问。 I thought I knew what detached entity is, but apparently this is too much for me... So Anyway I am trying to create a Person object that has an Interviewer object that has an Office object and every time I try to persist Person I get the exception (Office is the detached entity in this case). 我以为我知道分离的实体是什么,但是显然这对我来说太多了……所以无论如何,我试图创建一个Person对象,该对象的Interviewer对象具有一个Office对象,并且每次我尝试持久化Person时,我都会得到例外(在这种情况下,Office是独立实体)。 Relevant code looks like this: 相关代码如下:

@Before
public void setup() {

    Person person = PersontHelper.createPerson();
    person.getInterviewer().setOffice(officeDao.find(1));
    personDao.create(person);
    interviewers = service.getAllInterviewers();

}

the createPerson() is a static method of a helper class: createPerson()是帮助程序类的静态方法:

public static Person createPerson(){

    Interviewer interviewer = Interviewer.createInterviewer();
    Collection<Subject> skills = new HashSet<SubjectO>();   
    Person person = new Person();

    person.setInterviewer(interviewer);
    person.setSkills(skills);

    return person;
}

and finally createInterviewer() : 最后createInterviewer()

public static Interviewer createInterviewer(){
    Office office = new Office(1, 1, "Bristol");
    Interviewer interviewer = new Interviewer();

    interviewer.setFirstname("John");
    interviewer.setSurname("Bloggs");
    interviewer.setEmail("john.example.com");
    interviewer.setJobTitle(30);
    interviewer.setOffice(office);
    interviewer.setInterviews(new HashSet<Interview>());

    return interviewer;
}

You might notice that helper method that creates Interviewer already sets its office, but this is used for other tests, and since the office with the same properties is stored in test database I use officeDao to get it and set it as Interviewer's office. 您可能会注意到,创建Interviewer的助手方法已经设置了它的办公室,但是它用于其他测试,并且由于具有相同属性的办公室存储在测试数据库中,因此我使用officeDao来获取它并将其设置为Interviewer的办公室。 I tried multiple combinations of what I posted here but I get no positive results so far. 我尝试过将此处发布的内容进行多种组合,但到目前为止我没有得到任何积极的结果。 I don't understand why it complains about Office being detached, because it did even when I wasn't getting it from db - in other words all three object were new and they were not associated with persistence context. 我不明白为什么它抱怨Office分离,因为即使我不是从db那里获得它,它也这样做-换句话说,所有三个对象都是新对象,并且它们与持久性上下文无关。 I've read http://java.boot.by/scbcd5-guide/ch06.html but still cannot work it out. 我已经阅读了http://java.boot.by/scbcd5-guide/ch06.html,但仍然无法解决 What am I doing wrong here? 我在这里做错了什么?

The basic reason behind this exception is the entity which you want pass to persist is not associated with the EntityManager object. 发生此异常的基本原因是,您要传递的持久化实体未与EntityManager对象关联。 Anyways if you are using entityManager.persist(entity) then try to use entityManager.merge(entity) . 无论如何,如果您正在使用entityManager.persist(entity),请尝试使用entityManager.merge(entity)

One more thing is you should check your entity have updated or not for that you can use flush() method of EntityManager class. 还有一件事是,您应该检查您的实体是否已更新,因为可以使用EntityManager类的flush()方法。

Ex. 例如 entityManager.flush(); entityManager.flush();

Where do you open the transaction ? 您在哪里开立交易?

If your transaction management is at DAO level, this means your Office object has been loaded in a transaction opened by OfficeDao and is considered as detached in the next transaction opened by personDao. 如果您的事务管理处于DAO级别,则意味着您的Office对象已加载到OfficeDao打开的事务中,并被视为与personDao打开的下一个事务分离。

Ideally, your transaction management should be handled by your helper method or a service layer. 理想情况下,您的事务管理应由您的助手方法或服务层处理。

Otherwise, to fix this you must either load your Office object or merge it inside your personDao.create() method. 否则,要解决此问题,您必须加载Office对象或将其合并到personDao.create()方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM