2014-11-23 141 views
0

我已經映射實體在不同的項目相同的一對一映射不同的行爲:休眠。

Entity 
@Table(name="user_content") 
public class UserContent { 
    @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "content")) 
    @Id 
    @GeneratedValue(generator = "generator") 
    @Column(name = "content_id", unique = true, nullable = false) 
    private Long contentId; 

    @Column(name = "name") 
    String name; 

    @Column(name = "moderate_comment") 
    String moderateComment; 

    @OneToOne(fetch = FetchType.LAZY) 
    @PrimaryKeyJoinColumn 
    Content content; 


    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getModerateComment() { 
     return moderateComment; 
    } 

    public void setModerateComment(String moderateComment) { 
     this.moderateComment = moderateComment; 
    } 

    public Content getContent() { 
     return content; 
    } 

    public void setContent(Content content) { 
     this.content = content; 
    } 
} 

@Entity 
@Table(name = "content") 
public class Content { 
    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "content_id", unique = true, nullable = false) 
    private Long contentId; 


    @OneToOne(fetch = FetchType.LAZY, mappedBy = "content", cascade = CascadeType.ALL) 
    UserContent userContent; 


    public UserContent getUserContent() { 
     return userContent; 
    } 

    public void setUserContent(UserContent userContent) { 
     this.userContent = userContent; 
    } 

    public Long getContentId() { 
     return this.contentId; 
    } 

    public void setContentId(Long id) { 
     this.contentId = id; 
    } 
} 

我有2個項目,我用hibernate.hbm2ddl.auto設置爲create。我很困惑,但是這段代碼在我的不同項目中創建了不同的數據庫。

第一:

enter image description here

預計和期望的行爲。

二:
enter image description here

正如你可以看到維吾爾丟失。

我不明白在哪裏可以搜索此問題的原因。

Pleasr幫助找到。

回答

0

我浪費了很多時間,最終我注意到,第二瓦里安行不通的,因爲下面的依賴關係添加到pom.xml

 <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-annotations</artifactId> 
      <version>3.6.3.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-commons-annotations</artifactId> 
      <version>3.3.0.ga</version> 
     </dependency> 

後,我消除了他們的 - 關係成爲一個創造。