简体   繁体   English

在父实体被持久化并被选中之后,子实体为空

[英]Child entity is null after parent entity is persisted and selected

Please help! 请帮忙! It has already taken me a day, and I am no closer to resolving the issue. 它已经花了我一天的时间,而且我没有更接近解决这个问题。 My set up is as follows: 我的设置如下:

  1. Eclipse Indigo Eclipse Indigo
  2. Eclipselink 2.3 Eclipselink 2.3
  3. Apache Tomcat 6 Apache Tomcat 6

What I am doing is I am persisting an entity that has an @OneToOne mapping with a child entity. 我正在做的是我持有一个具有子实体的@OneToOne映射的实体。 Once the record is in the database, I am running a select and successfully selecting the newly inserted record. 一旦记录在数据库中,我正在运行一个选择并成功选择新插入的记录。 However, the child entity is null. 但是,子实体为null。

My goal is to only insert the parent record, because the child record is already in the DB. 我的目标是只插入父记录,因为子记录已经在DB中。

Question: what do I need to change to cause the child record to be populated? 问题:我需要更改什么才能填充子记录?

I have an entity that I persist: 我有一个我坚持的实体:

EntityManager em = getEntityManager();
try {
    em.getTransaction().begin();
    em.persist(timeSheet);
    em.getTransaction().commit();
}

The entity contains @OneToOne: 该实体包含@OneToOne:

@Entity
@Cache(expiry=-1)
@Table(name="TIME_SHEET")
    public class TimeSheet implements Serializable {
        private static final long serialVersionUID = 1L;

        @OneToOne(fetch=FetchType.EAGER)
        @JoinColumns({
            @JoinColumn(name="PROJECT_ID", referencedColumnName="PROJECT_ID", nullable = false, insertable=false, updatable=false),
            @JoinColumn(name="COMPANY_ID", referencedColumnName="COMPANY_ID", nullable = false, insertable=false, updatable=false)
        })
        private Project project;
        public Project getProject() {
            return project;
        }

The child entity is: 子实体是:

@Entity
@Cache(expiry=-1)
@Table(name="PROJECT")
public class Project implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private ProjectPK id;

After "TimeSheet" is persisted and selected, "Project" is null. 保持并选择“TimeSheet”后,“Project”为空。 What should I do to ensure that "Project" is populated? 我该怎么做才能确保填充“项目”?

Thank you! 谢谢!

Well, in the code shown, you persist a TimeSheet without first setting a Project in it, so why would you expect it to have a Project when you load it? 好吧,在显示的代码中,你坚持使用TimeSheet而不先在其中设置一个Project,那么为什么你在加载它时会期望它有一个Project? You should never expect your object model to look different after you load it than it did before you saved it. 加载它后,你永远不应该期望你的对象模型看起来与你保存它之前的不同。 If it does look different, then you have a problem. 如果它确实看起来不同,那么你就有问题了。

You should set the cascade policy correctly. 您应该正确设置级联策略。

@OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-hibspec-cascade http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#entity-hibspec-cascade

Cascade policy dictates what to do with associated entities when you perform a persistence action on the parent entity. 级联策略规定了在父实体上执行持久性操作时如何处理关联实体。

As Ryan points out, you need to maintain the reference in TimeSheet to the Project. 正如Ryan指出的那样,您需要将TimeSheet中的引用保持为Project。 This is because the TimeSheet is cached. 这是因为时间表是缓存的。 So you can either set the relationship by reading in the existing project when creating the TimeSheet, or by refreshing the TimeSheet after the transaction commits so that it gets built. 因此,您可以通过在创建TimeSheet时读取现有项目来设置关系,也可以在事务提交后刷新TimeSheet以便构建它。

The code you have shown sets the TimeSheet->Project relationship fields are read only and doesn't show how the foriegn keys are populated. 您显示的代码设置TimeSheet-> Project关系字段是只读的,并不显示如何填充foriegn键。 Make sure that these are actually set when inserting the TimeSheet through other mappings or it maybe that the TimeSheet legitimately doesn't have a Project - though the fields seem to have a not-null constraint. 确保在通过其他映射插入TimeSheet时实际设置了这些映射,或者可能是TimeSheet合法地没有项目 - 尽管字段似乎具有非空约束。

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

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