0

我在使用 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 建议的那样,我现在不使用方法克隆。我还遇到了另一个错误。

我在客户端类中创建了方法级联:

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]] 在 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] 在玩.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:132) [play_2.10.jar:2.1.0] 在 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、对于类:model.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] 在 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] Caused 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] 在 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 元素列表进行操作。我想在我的客户端类中有 2 个文件(地址,corrAddress)。

4

0 回答 0