2014-02-19 66 views
0

我有以下多重性: Order3d 1 ----- 1 .. * DrawFile 1 ----- 1 .. * Order3dLineJPA插入失敗,因爲在零值,我的外鍵

而且我當我想保存Order3d對象我得到一個約束衝突,因爲DrawFile.id放在我Order3dLine領域,我在國外鍵得到了零值

Hibernate: insert into order3d (delivery, demand_date, estimate_date, order_date, person_id, state) values (?, ?, ?, ?, ?, ?) //OK 
Hibernate: insert into draw (dimension, filename, format, hashname, hole, order3d_id, readable, size, slope, wall) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) //OK 
Hibernate: insert into order3dline (duration, produced, quantity, color, draw_id, material) values (?, ?, ?, ?, ?, ?) 
09:33:08,779 TRACE BasicBinder:84 - binding parameter [1] as [SMALLINT] - 0 
09:33:08,780 TRACE BasicBinder:84 - binding parameter [2] as [SMALLINT] - 0 
09:33:08,780 TRACE BasicBinder:84 - binding parameter [3] as [SMALLINT] - 2 
09:33:08,781 TRACE BasicBinder:84 - binding parameter [4] as [INTEGER] - 0 
09:33:08,781 TRACE BasicBinder:84 - binding parameter [5] as [INTEGER] - 0 //NOT OK 
09:33:08,782 TRACE BasicBinder:84 - binding parameter [6] as [VARCHAR] - Something 

我的Java類是像這(我沒有把所有的屬性和方法,因爲我猜這是不相關的):

@Entity 
@Table (name="draw") 
public class DrawFile implements Serializable { 
    @OneToMany(mappedBy="draw", fetch = FetchType.EAGER, cascade=CascadeType.ALL) 
    private Set<Order3dLine> line3ds=new HashSet<Order3dLine>(3); 
} 

@Entity 
@Table (name="order3dline") 
public class Order3dLine implements Serializable { 
    private static final long serialVersionUID = 3993578603382571145L; 

    @EmbeddedId 
    private Order3dLine3dPK id=new Order3dLine3dPK(); 
    @ManyToOne(fetch=FetchType.EAGER) 
    @JoinColumn(name = "draw_id", referencedColumnName="id", insertable = false, updatable = false) 
    private DrawFile draw; 
} 

@Embeddable 
public class Order3dLine3dPK implements Serializable { 
    @Column(name="draw_id", columnDefinition="id", insertable=false, updatable=false) 
    private int drawId; 
    private String material=""; 
    private int color; 
} 

註釋是否缺失?

+0

您必須填寫您的FK並輸入有效的編號 –

回答

0

對此的一個常見錯誤來源是持久化一個order3d對象,但設置非管理order3d對象作爲order3dLine對象的引用,並且由於所引用的order3d對象未被持久化,order3dLine對象獲得一個NULL關鍵字 - 嘗試持久化order3d對象,並使用託管的order3d對象重新設置order3dLine對象上的引用,然後持久保存order3dLine對象,使order3dLine對象獲得對JPA實體的引用,而不僅僅是POJO。

+0

我的Order3d已完全填充並存儲在我的Web應用程序的會話中,因此myOrder爲TANSCIENT,然後我調用entityManager.persist(myOrder);將myOrder狀態更改爲MANAGED。但是我得到了我第一篇文章中描述的問題。 – Athanor

+0

並且order3Line對象具有在持續調用時引用的託管order3d對象? – Smutje