2012-07-16 29 views
0

我想AppFuse中(Struts2的,Hibernate和Spring)同時節省2個實體, 下面是一個例子(地址和人是新的對象):AppFuse中保存了兩個新的實體一次

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 
} 

有什麼辦法一次保存這個新的實體嗎?

在此先感謝..!

回答

1

下面可能有助於您

你有休耕在docs_oracle_javax_persistence_OneToMany.html

例1給出:一對許多使用關聯泛型

在Customer類:

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

In Order class:

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


例2:一一對多關聯,而不使用泛型

在Customer類:

@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

+0

感謝錢德拉! – jzafrilla 2012-07-16 13:29:26

+0

歡迎@jzafrilla。 – 2012-07-17 02:22:02

相關問題