我在使用play framework 2.1创建的应用程序中克隆实体时遇到问题。 当我运行下面的方法时,我得到RollbackException:提交事务时出错。
我的问题是:在playframework中使用jpa克隆实体的正确方法是什么?
@play.db.jpa.Transactional
public static Result edit(final Long id) {
final Client client = Client.findById(id);
if (client == null) {
return notFound(String.format("Client %s does not exist.", id));
}
if(client.address.id == client.corrAddress.id){
client.corrAddress = client.address.clone();
}
Form<Client> filledForm = CLIENT_FORM.fill(client);
return ok(form.render(filledForm, "Edycja danych klienta"));
}
地址克隆方法:
public Address clone(){
return new Address(this.street, this.postCode, this.city);
}
更新:
我在代码中做了一些更改。我现在不使用方法克隆,正如@Christopher Hunt建议的那样。 我还有另一个错误。
我在Client类中创建了方法级联:
public void cascade(){
if(address.equals(corrAddress)){
if(address.id != null && corrAddress.id != null
&& address.id.equals(corrAddress.id)){
address.client = this;
address.corrClient = this;
address.saveOrUpdate();
corrAddress = null;
} else {
address.client = this;
address.corrClient = this;
address.saveOrUpdate();
corrAddress = null;
}
} else {
if(address.id != null && corrAddress.id != null
&& address.id.equals(corrAddress.id)){
System.out.println("TEST");
address.client = this;
address.corrClient = null;
address.saveOrUpdate();
String street = this.corrAddress.street;
String postCode = this.corrAddress.postCode;
String city = this.corrAddress.city;
corrAddress = null;
/** CODE BELOW CAUSES ERROR: **/
corrAddress = new Address(street, postCode, city);
corrAddress.client = null;
corrAddress.corrClient = this;
corrAddress.saveOrUpdate();
/** CODE ABOVE CAUSES ERROR: **/
} else {
address.client = this;
address.saveOrUpdate();
corrAddress.corrClient = this;
corrAddress.saveOrUpdate();
}
}
}
在Client类中定义地址引用:
@Valid
@OneToOne(mappedBy="client", orphanRemoval=true, fetch = FetchType.EAGER)
public Address address;
@Valid
@OneToOne(mappedBy="corrClient", orphanRemoval=true, fetch = FetchType.EAGER)
public Address corrAddress;
现在我收到了错误:
play.api.Application $$ anon $ 1:执行 exception [[PersistenceException:org.hibernate.HibernateException: 找到了具有给定标识符的多个行:2,对于类: models.Address]] play.api.Application $ class.handleError(Application.scala:289) 〜[play_2.10.jar:2.1.0] at play.api.DefaultApplication.handleError(Application.scala:383) [play_2.10.jar:2.1.0] at play.core.server.netty.PlayDefaultUpstreamHandler $$不久$ 2 $$ anonfun $处理$ 1.适用(PlayDefaultUpstreamHandler.scala:132) [play_2.10.jar:2.1.0] at play.core.server.netty.PlayDefaultUpstreamHandler $$匿名$ $$ 2 $ anonfun手柄$ 1.适用(PlayDefaultUpstreamHandler.scala:128) [play_2.10.jar:2.1.0] at play.api.libs.concurrent.PlayPromise $$ anonfun $ extend1 $ 1.适用(Promise.scala:113) [play_2.10.jar:2.1.0] at play.api.libs.concurrent.PlayPromise $$ anonfun $ extend1 $ 1.适用(Promise.scala:113) [play_2.10.jar:2.1.0] javax.persistence.PersistenceException: org.hibernate.HibernateException:具有给定的多行 找到了标识符:2,用于类:models.Address at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1360) 〜[hibernate-entitymanager-4.1.1.Final.jar:4.1.1.Final] at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1288) 〜[hibernate-entitymanager-4.1.1.Final.jar:4.1.1.Final] at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1294) 〜[hibernate-entitymanager-4.1.1.Final.jar:4.1.1.Final] at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:877) 〜[hibernate-entitymanager-4.1.1.Final.jar:4.1.1.Final] at models.clients.Client.update(Client.java:58)〜[na:na] at models.clients.Client.saveOrUpdate(Client.java:110)〜[na:na]导致 by:org.hibernate.HibernateException:给定的多行 找到了标识符:2,用于类:models.Address at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:104) 〜[hibernate-core-4.1.1.Final.jar:4.1.1.Final] at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:161) 〜[hibernate-core-4.1.1.Final.jar:4.1.1.Final] at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2209) 〜[hibernate-core-4.1.1.Final.jar:4.1.1.Final] at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:661) 〜[hibernate-core-4.1.1.Final.jar:4.1.1.Final] at org.hibernate.type.EntityType.resolve(EntityType.java:441) 〜[hibernate-core-4.1.1.Final.jar:4.1.1.Final] at org.hibernate.type.EntityType.replace(EntityType.java:298) 〜[冬眠核-4.1.1.Final.jar:4.1.1.Final]
重要的是我不想使用2个元素列表。我想在我的Client类中使用2个fileds(address,corrAddress)。