2013-08-25 79 views
0

我正在用ManyToOne關係創建JPA實體。爲什麼有child.setParent(parent);原因如下期間flush()失敗:爲什麼設置ManyToOne父項會導致刷新失敗?

org.apache.openjpa.persistence.ArgumentException: Missing field for property "parent_id" in type "class test.ChildPrimaryKey". 

測試代碼失敗:

Parent parent = new Parent(); 
    Child child = new Child(); 
    ChildPrimaryKey childPrimaryKey = new ChildPrimaryKey(); 
    childPrimaryKey.setLookupId(1); 
    child.setId(childPrimaryKey); 
    child.setParent(parent); // <-- FAIL because of this 

    // Begin transaction 
    entityManager.clear(); 
    entityManager.getTransaction().begin(); 

    LOGGER.info("Persisting parent without child."); 
    entityManager.persist(parent); 

    LOGGER.info("Updating parent with child."); 
    childPrimaryKey.setParentId(parent.getId()); 
    parent.getChildren().add(child); 
    entityManager.merge(parent); 

    // Fail happens at flush 
    entityManager.flush(); 

實體:

@Embeddable 
public class ChildPrimaryKey { 
    @Column(name = "lookup_id") 
    private int lookupId; 

    @Column(name = "parent_id") 
    private long parentId; 
} 

@Entity 
@Table(name = "child") 
public class Child { 

    @EmbeddedId 
    private ChildPrimaryKey id; 

    @MapsId("parent_id") 
    @ManyToOne 
    private Parent parent; 
} 

@Entity 
@Table(name = "parent") 
public class Parent { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "id") 
    private long id; 

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
    private List<Child> children; 
} 

如果我刪除child.setParent(parent);語句,那麼我的代碼通過。使用OpenJPA 2.2.2

回答

1

我相信你的MapsId應該是@MapsId("parentId")根據你所呈現的類結構。 MapsId的值是一個屬性,而不是列名稱。

相關問題