OneToMany介于2个Hibernate类之间

时间:2014-10-30 10:33:16

标签: java hibernate relation

我试图了解Hibernate。 在让我的项目进行编译之后,我已经开始将我的类转换为"启用了Hibernate"

目前,我有2个班级 1)人(id,name,firstname,...) 2)任务(Id,名称,描述,idOwner)

我想在Person(id)和Task(idOwner)之间建立OneToMany关系 因此,当用户从Person类获取List时,他们将获得与该链接相关的所有任务。

经过一番尝试和失败之后,这是我现在的代码:

Person.java

@Entity
@Table(name = "people", uniqueConstraints = {
@UniqueConstraint(columnNames = "PERSON_ID")
})
public class Person implements Serializable {

private int id;
private String firstName;
private String name;
private String function;
private String email;
private String password;
private RoleEnum role;
private List<Task> lstTasks;

public Person(String firstName, String name, String function) {
    this.firstName = firstName;
    this.name = name;
    this.function = function;
    this.email = "";
    this.password = "";
    this.setRole(RoleEnum.User);
}

// <editor-fold defaultstate="collapsed" desc="Getters">

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "PERSON_ID", unique = true, nullable = false)
public int getId() {
    return id;
}

@Column(name = "PERSON_FIRSTNAME", unique = false, nullable = false, length = 30)
public String getFirstName() {
    return firstName;
}

@Column(name = "PERSON_NAME", unique = false, nullable = false, length = 30)
public String getName() {
    return name;
}

@Column(name = "PERSON_FUNCTION", unique = false, nullable = false, length = 30)
public String getFunction() {
    return function;
}

@Column(name = "PERSON_EMAIL", unique = false, nullable = false, length = 30)
public String getEmail() {
    return email;
}

@Column(name = "PERSON_PASSWORD", unique = false, nullable = false, length = 30)
public String getPassword() {
    return password;
}

@Column(name = "PERSON_ROLE", unique = false, nullable = false, length = 30)
public RoleEnum getRole() {
    return role;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "idOwner")
public List<Task> getLstTasks() {
    return lstTasks;
}

//Setters

}

Task.java

@Entity
@Table(name="tasks", uniqueConstraints = 
@UniqueConstraint(columnNames = "TASK_ID"))
public class Task implements Serializable {

private int id;
private String name;
private String description;
private int idOwner;

public Task(int id, String name, int idOwner) {
    this.id = id;
    this.name = name;
    this.idOwner = idOwner;
}

// <editor-fold defaultstate="collapsed" desc="Getters">

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "TASK_ID", unique = true, nullable = false)
public int getId() {
    return id;
}

@Column(name = "TASK_NAME")
public String getName() {
    return name;
}

@Column(name = "TASK_DESCRIPTION")
public String getDescription() {
    return description;
}

@Column(name = "TASK_ID_OWNER")
public int getIdOwner() {
    return idOwner;
}

// </editor-fold>


//Setters

}

有人可以告诉/显示/解释我必须做些什么,让这项工作成功吗?

2 个答案:

答案 0 :(得分:0)

目前,您的映射应该链接ownerId而不是Task类。

将您的Task类更改为此

private Person person;


@ManyToOne
@JoinColumn(name = "TASK_ID_OWNER")
public Person getPerson() {
    return person;
}

答案 1 :(得分:0)

在您的Person实体中,您已声明与Task实体的一对多关系,如下所示:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "idOwner")
public List<Task> getLstTasks() {
    return lstTasks;
}

这里你给了idOwnermappedBy属性,这意味着你告诉hibernate Task类中有idOwner类的属性,类型为{{1} }}

因此,您必须像这样修改Person类(将变量类型从Tasks更改为int):

Person

如果你删除private Person idOwner; @ManyToOne @JoinColumn(name = "TASK_ID_OWNER") public Person getIdOwner() { return idOwner; } public void setIdOwner(Person idOwner) { this.idOwner=idOwner; } 注释,那么hibernate将为你的关系创建一个Join表,否则它只会在@JoinColumn表中创建一个外键,外键列名为Task