Shiyou
Shiyou

Reputation: 121

How to map multiple One-to-Many association in Hibernate

I have these 3 tables in my database

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`)
)

Then I want to use Hibernate to connect them.

These are my java classes

User:

// ... 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
}

Project:

// ... 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
}

Work:

// ... 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
}

Now it can work.

However, after the relation of Project and Work (One to Many / Many to One) mapped, It gives me error message.

What I do for mapping is :

add these code into Project class:

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

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

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

And these code into Work class:

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

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

and remove @Column(name = "project_id") private int projectId;

Then it gives me error message:

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)

So I want to know how I can map the relation of Project-Work.

Upvotes: 0

Views: 4302

Answers (1)

anish
anish

Reputation: 492

In the 'Project' class, the code is:

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

And, in the 'Work' class, the code is:

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

The names in ('mappedBy="project_id") and the variable name in the other class (private Project project) both should match.

Upvotes: 3

Related Questions