保留并选择父实体后,子实体为空

发布于 2024-12-11 02:34:43 字数 1424 浏览 0 评论 0原文

请帮忙!我已经花了一天的时间,而且我还没有更接近解决这个问题。我的设置如下:

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

我正在做的是保留一个与子实体具有 @OneToOne 映射的实体。一旦记录进入数据库,我就运行选择并成功选择新插入的记录。但是,子实体为空。

我的目标是仅插入父记录,因为子记录已经在数据库中。

问题:我需要更改什么才能填充子记录?

我有一个持久化的实体:

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

该实体包含@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;
        }

子实体是:

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

    @EmbeddedId
    private ProjectPK id;

持久化并选择“TimeSheet”后,“Project”为空。我应该怎么做才能确保“项目”已填充?

谢谢你!

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
  2. Eclipselink 2.3
  3. Apache Tomcat 6

What I am doing is I am persisting an entity that has an @OneToOne mapping with a child entity. 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.

My goal is to only insert the parent record, because the child record is already in the 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:

@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. What should I do to ensure that "Project" is populated?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裸钻 2024-12-18 02:34:43

好吧,在所示的代码中,您保留了一个 TimeSheet,而没有首先在其中设置一个项目,那么为什么您希望在加载它时它有一个项目呢?您永远不应期望对象模型在加载后与保存前看起来有所不同。如果它看起来确实不同,那么就有问题了。

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? 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.

软甜啾 2024-12-18 02:34:43

应正确设置级联策略。

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

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

级联策略规定了当您执行对父实体的持久性操作。

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

Cascade policy dictates what to do with associated entities when you perform a persistence action on the parent entity.

别想她 2024-12-18 02:34:43

正如 Ryan 指出的那样,您需要维护时间表中对项目的引用。这是因为 TimeSheet 已被缓存。因此,您可以通过在创建 TimeSheet 时读取现有项目来设置关系,也可以在事务提交后刷新 TimeSheet 以便构建它。

您显示的代码将 TimeSheet->Project 关系字段设置为只读,并且不显示如何填充外键。确保在通过其他映射插入 TimeSheet 时实际设置了这些值,或者 TimeSheet 可能合法地没有项目 - 尽管这些字段似乎具有非空约束。

As Ryan points out, you need to maintain the reference in TimeSheet to the 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.

The code you have shown sets the TimeSheet->Project relationship fields are read only and doesn't show how the foriegn keys are populated. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文