2013-02-10 162 views
1

我的類看起來像JPA:如何創建與實體相同類型的字段?

@Entity 
public class Version extends MutableEntity { 
    @Column(nullable = false) 
    private String name; 
    @Column(nullable = false) 
    @Enumerated(EnumType.STRING) 
    private VersionType type; 
    @Column(nullable = false) 
    @Enumerated(EnumType.STRING) 
    private VersionStatus status; 
    @Column(nullable = true) 
    private DateTime publishedOn; 
    @Column(nullable = true) 
    private DateTime retiredOn; 
    @Column 
    private Version parentVersion; 

我想有相同類型的parentVersion作爲Version,但我的測試失敗

@Test 
public void testVersion() { 
    Version version = new Version("testVersion", VersionType.MAJOR); 
    version = crudService.create(version); 
    assertNotNull(version.getId()); 
} 

,我看到錯誤的

Caused by: org.hibernate.MappingException: Could not determine type for: com.myorg.project.versioning.entities.Version, at table: Version, for columns: [org.hibernate.mapping.Column(parentVersion)] 

哪有我解決了這個問題?

回答

0

您在parentVersion上缺少一些註釋。 Hibernate不知道如何映射數據庫中的這一列。

@JoinColumn添加到您的字段,hibernate將使用其@Id字段。

1

這不是基本屬性。它是關係,因爲價值是其他實體。因此@ManyToOne註釋應使用:

@ManyToOne 
private Version parentVersion; 

如果需要雙向關係(父知道兒童),即可以通過添加以下來完成:

@OneToMany (mappedBy = "parentVersion") 
private List<Version> childVersions; 
相關問題