如何在Hibernate

时间:2015-05-21 01:08:48

标签: java hibernate annotations one-to-many

我的数据库中有这3个表

CREATE TABLE IF NOT EXISTS `User`(
    `id`        INT             NOT NULL    AUTO_INCREMENT,
    `name`      VARCHAR(255)    NOT NULL    DEFAULT '',
    PRIMARY KEY (`id`)
)

CREATE TABLE IF NOT EXISTS `Project` (
    `id`        INT             NOT NULL    AUTO_INCREMENT,
    `manager`   INT             NOT NULL,   
    `name`      VARCHAR(255)    NOT NULL    DEFAULT '', 
    PRIMARY KEY (`id`),
    FOREIGN KEY (`manager`) REFERENCES `User`(`id`)
)

CREATE TABLE IF NOT EXISTS `Work`(
    `id`            INT             NOT NULL    AUTO_INCREMENT,
    `project_id`    INT             NOT NULL    DEFAULT -1,
    `name`          VARCHAR(255)    NOT NULL    DEFAULT '', 
    PRIMARY KEY (`id`),
    FOREIGN KEY (`project_id`)  REFERENCES `Project`(`id`),
    FOREIGN KEY (`creator`)     REFERENCES `User`(`id`)
)

然后我想用Hibernate连接它们。

这些是我的java类

用户:

// ... import code
@Entity
@Table
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column
    private String name;

    @OneToMany(mappedBy = "manager")
    private List<Project> projects;

    public User() {
    }
    //  ... Getter & Setter code
}

项目:

// ... import code
@Entity
@Table
public class Project {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column
    private String name;

    @ManyToOne
    @JoinColumn(name = "manager")
    private User manager;

    public Project () {
    }
    //  ... Getter & Setter code
}

工作:

// ... import code
public class Work {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column
    private String name;
    @Column(name = "project_id")
    private int projectId;

    @ManyToOne
    @JoinColumn(name = "creator")
    private User creator;

    public Work() {
    }
    //  ... Getter & Setter code
}

现在它可以工作。

然而,在项目与工作(一对多/多对一)的关系映射后,它给出了错误信息。

我为绘图做的是:

将这些代码添加到Project类中:

@OneToMany(mappedBy = "project_id")
private List<Work> works;

public List<Work> getWorks() {
    return works;
}

public void setWorks(List<Work> works) {
    this.works = works;
}

这些代码进入Work class:

@ManyToOne
@JoinColumn(name = "project_id")
private Project project;
public Project getProject() {
    return project;
}

public void setProject(Project project) {
    this.project = project;
}

并删除@Column(name = "project_id") private int projectId;

然后它给我错误信息:

Exception in thread "main" org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: database.tables.Work.project_id in database.tables.Project.works
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:768)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)

所以我想知道如何映射Project-Work的关系。

1 个答案:

答案 0 :(得分:3)

在&#39;项目&#39;类,代码是:

@OneToMany(mappedBy = "project_id")
private List<Work> works;

而且,在&#39;工作&#39;类,代码是:

@ManyToOne
@JoinColumn(name = "project_id")
private Project project;   
//var name should match with mappedBy name in other class

(&#39; mappedBy =&#34; project_id&#34;)中的名称和其他类(私有项目项目)中的变量名称都应匹配。