Play フレームワーク 2.1 で作成したアプリケーションでエンティティのクローン作成に問題があります。以下のメソッドを実行すると、RollbackException: Error while committing the transaction が発生します。
私の質問は: 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 が提案したように、現在は clone メソッドを使用していません。また、別のエラーが発生しました。
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: 実行例外 [[PersistenceException: org.hibernate.HibernateException: 指定された識別子を持つ複数の行が見つかりました: 2、クラス: models.Address]] at 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$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:132) [play_2.10.jar:2.1.0] at play.core.server.netty.PlayDefaultUpstreamHandler$$ anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:128) [play_2.10.jar:2.1.0] at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala) :113) [play_2.10.jar:2.1.0] play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(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] 原因: 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] org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2209) で ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final] org.hibernate.type.EntityType.loadByUniqueKey( EntityType.java:661) ~[hibernate-core-4.1.1.Final.jar:4.1.1.Final] 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) ~ [hibernate-core-4.1.1.Final.jar:4.1.1.Final]
重要なのは、2 つの要素リストを操作したくないということです。Client クラスに 2 つのフィールド (アドレス、corrAddress) が必要です。