【问题标题】:Duplicate of Entity实体副本
【发布时间】:2013-08-27 07:15:37
【问题描述】:

我无法让这个工作,我找不到我的特殊情况的例子。简而言之:我有一个有关系的主要实体。现在我想复制一个子实体并将这个新的子实体链接到主实体。最好我试着举一个简短的例子。我跳过了 getter 和 setter 方法以使其更短:

实体条目:

@Entity
public class Entry implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(mappedBy = "entry", cascade=CascadeType.ALL)
    private List<EntryData> entriesDataList = new LinkedList<>();

    @OneToOne(cascade=CascadeType.ALL)
    private EntryData entryData;

}

实体入口数据:

@Entity
public class EntryData implements Serializable{

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String subject;

    @ManyToOne(optional = false)
    private Entry entry;    

    @ManyToMany(mappedBy = "entries", cascade = {CascadeType.ALL} )
    private List<EntryTag> tags = new LinkedList<>();

}

实体入口标签:

@Entity
public class EntryTag  implements Serializable{

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique=true)
    private String name;

    @ManyToMany() 
    private List<EntryData> entries = new LinkedList<>();        

}

现在我想做以下事情:

@Stateless
public class NewEntryData {

    @PersistenceContext
    private EntityManager em;

    public void makeNewEntryData(){
        Entry e = em.find(Entry.class, 10);
        em.detach(e);
        EntryData ed = e.getEntryData();
        ed.setSubject("New Subject");
        ed.setId(null);
        for (Iterator<EntryTag> it = ed.getTags().iterator(); it.hasNext();) {
            it.next().addEntryData(ed);            
        }
        em.merge(e);
    }   
}

我的预期:

生成了一个新实体EntryData,它的内容与存储在Entry.entryData 中的旧实体相同,但新主题除外。在Entry.entryData 中生成一个指向新EntryData 的链接。旧的EntryData 原样包含在数据库中。

我在em.merge(e) 得到的信息:

org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [test.EntryData] is mapped to a primary key column in the database. Updates are not allowed.

谁能帮我解决这个问题?

【问题讨论】:

  • 我不是 Java 开发人员,但在阅读帖子后,我浏览并看到 CascadeDetach 分离了所有导航对象。这与您使用的框架有关吗?
  • 因为CascadeType.ALL 我认为我已经做了级联分离。

标签: java entity-framework jpa entity jpa-2.0


【解决方案1】:

test.EntryData 类的 id 列被映射为主键,因此 id column 中不允许更新

我认为您在 id 列中插入重复值

【讨论】:

  • 那么做我想做的事情的解决方案是什么?计划我将主键更改为null。通过这种方式,我认为我可以生成一个新的 EntryData 实体,而不是覆盖旧实体。
  • 将自动增量添加到该列
  • 我以为GenerationType.AUTO 会创建一个id,如果它是null
  • 我已经使用了自动增量...GenerationType.AUTO对应于MYSQL中的自动增量
  • kk 在你的 mysql 表中创建一个序列并修改你的代码,例如 public class ContactMaster 实现 Serializable{ @TableGenerator(name="CONT_SEQGEN", table="SEQUENCE_GENERATOR", pkColumnName = "SEQ_ID", valueColumnName = "SEQ_ID_VAL", pkColumnValue = "CONTACT_ID", allocationSize = 1) @Id @GeneratedValue(strategy=GenerationType.TABLE,generator="CONT_SEQGEN") @Column(name="CONTACT_ID") public String contactId;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多