-2

I'm trying to get the hang of Hibernate. After getting my project to compile, I've started to convert my classes to be "Hibernate-enabled"

Currently, I'm having 2 classes 1) Person (id, name, firstname, ...) 2) Task (Id, name, description, idOwner)

I would like to have a OneToMany relationship between Person(id) and Task (idOwner) So when the users gets the List from the Person class, they would get all the tasks linked to that.

After some trying and failing, here's my current code:

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

}

Can somebody tell/show/explain me what I have to do exactly, to make this work?

Matt
  • 1,893
  • 11
  • 33
  • 57

2 Answers2

0

Currently your mapping should linking ownerId instead of Task class.

Change your Task class to this

private Person person;


@ManyToOne
@JoinColumn(name = "TASK_ID_OWNER")
public Person getPerson() {
    return person;
}
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
0

In your Person entity you have declared one-to-many relationship with Task entity like this:

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

Here you have given idOwner to mappedBy attribute, it means you are telling hibernate that there is property in Task class called idOwner which is of type Person.

So you have to modify your Tasks class like this (Changing the variable type from int to Person):

private Person idOwner;

@ManyToOne
@JoinColumn(name = "TASK_ID_OWNER")
public Person getIdOwner() {
    return idOwner;
}

public void setIdOwner(Person idOwner) {
    this.idOwner=idOwner;
}

If you remove the @JoinColumn annotation then hibernate will create a Join table for your relationship, else it will just create a foreign key in Task table with foreign key column name as TASK_ID_OWNER.

Chaitanya
  • 15,403
  • 35
  • 96
  • 137