简体   繁体   English

冬眠。 同一对一映射在不同项目中的不同行为

[英]Hibernate. Different behaviour of same one to one mapping in different projects

I have mapped entities: 我已经映射了实体:

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;
    }
}

I have 2 projects where I use hibernate.hbm2ddl.auto set to create . 我有两个项目使用hibernate.hbm2ddl.auto设置来create I confused but this code creates database different in my differernt projects. 我很困惑,但是这段代码在我不同的项目中创建了不同的数据库。

First: 第一:

在此处输入图片说明

It is expected and desired behaviour. 这是预期和期望的行为。

Second: 第二:
在此处输入图片说明

As you can see realtion is missing. 如您所见,Realture缺失。

I don't understand where can I search a cause of this problem. 我不知道在哪里可以找到导致此问题的原因。

Pleasr help to find. 请帮助找到。

I wasted a lot of time and eventually I noticed that second varian doesnt work because following dependencies were added to pom.xml 我浪费了很多时间,最终我发现第二个varian不起作用,因为以下依赖项已添加到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>

After I removed their - relation became a create. 在我删除他们的-关系后,就创建了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM