JPA:当类使用EmbeddedId时坚持自我关系

时间:2015-01-15 07:58:28

标签: java jpa

我有这种自我关系

public class Estoria implements Serializable {

  @EmbeddedId 
  protected EstoriaPK estoriaPK; 

  @ManyToOne
  @JoinColumns({@JoinColumn(name = "id_estoria", referencedColumnName = "id", updatable = false, insertable = false, nullable = true)
              ,@JoinColumn(name = "id_projeto" , referencedColumnName = "id_projeto" ,     insertable = false, updatable = false)})
  private Estoria subtask;

  @OneToMany(mappedBy = "subtask",cascade = CascadeType.ALL) 
  private Collection<Estoria> subtasks; 
}

和我的Ids

@Embeddable 
public class EstoriaPK implements Serializable {

 @Basic(optional = false)
  @Column(name = "id", unique = true, nullable = false)
  private int id;
  @Basic(optional = false)
  @NotNull
  @Column(name = "id_projeto", unique = true, nullable = false)
  private int idProjeto;

  // GETTRs and SETTERs
}

我尝试继续使用级联

    public void persistSubtask(int idEstoria,Estoria subtask) {
    try {
        estoria = this.entityManager.createNamedQuery("Estoria.findById",Estoria.class)
                            .setParameter("id", idEstoria)
                            .getSingleResult();

        subtask.setEstoriaPK(new EstoriaPK(0,estoria.getProjeto().getId())); 
        subtask.setSubtask(estoria);
        this.entityManager.persist(subtask);  


    } catch (Exception e) {
        throw new NoPersistException("falha ao persistir subtask");
    } 
}

这不起作用,因为我的foreingkey没有坚持,是否有一些注释错误?

如果我试图坚持一个集合不能正常工作,因为他们的父亲引用不存在,是否还有其他需要修复的注释问题?

1 个答案:

答案 0 :(得分:0)

问题似乎是

insertable = false, updatable = false