【问题标题】:AppFuse Save two new entities at onceAppFuse 一次保存两个新实体
【发布时间】:2012-07-16 08:09:33
【问题描述】:

我正在尝试一次在 AppFuse(Struts2、Hibernate 和 Spring)中保存 2 个实体, 这是一个例子(地址和人是新对象):

person.setAddress(address);
personManager.save(person);

但这不起作用,我得到了这个异常:

object references an unsaved transient instance - save the transient
instance before merge

我必须做的:

addressManager.save(address);
person.setAddress(address);
personManager.save(person);

在个人模型中,我已经声明了这样的地址:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "person", cascade= CascadeType.ALL)
public Address getAddress() {
    return this.address
}

有没有办法一次性保存这些新实体?

提前谢谢..!

【问题讨论】:

    标签: spring hibernate jpa appfuse


    【解决方案1】:

    以下内容可能对您有所帮助

    您是否按照docs_oracle_javax_persistence_OneToMany.html 中给出的方式闲置了

    示例 1: 使用泛型的一对多关联

    在客户类中:

    @OneToMany(cascade=ALL, mappedBy="customer")
    public Set getOrders() { return orders; }
    

    在订单类中:

    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { return customer; }
    


    示例 2: 不使用泛型的一对多关联

    在客户类中:

    @OneToMany(targetEntity=com.acme.Order.class, cascade=ALL,
            mappedBy="customer")
    public Set getOrders() { return orders; }
    

    在订单类中:

    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { return customer; }
    

    你可以按照这个例子OneToManyTargetEntity中给出的做。

    看看这些线程:
    stackoverflow_4011472
    stackoverflow_9032998

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      相关资源
      最近更新 更多