Gustavo Gustavo - 4 months ago 23
Java Question

How to map a list of Objects into a table with hibernate in spring?

I am trying to map a list of Users to a location object but I get a Mapping Exception. This is because the List object is not recognized by database ? Or why do I get this exception ?

This is my user class :

@Entity
@Table(name = "users")
public class NewUser extends BaseEntity{
private String login;
private String fullName;

private Location location;
private Department department;
private Role role;
private Long days;
private String team;
private Long managerId;
private String hiredDate;

public String getLogin() {
return login;
}

public void setLogin(String login) {
this.login = login;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public Location getLocation() {
return location;
}

@ManyToOne(targetEntity = Location.class)
@JoinTable(name = "location")
public void setLocation(Location location) {
this.location = location;
}

public Department getDepartment() {
return department;
}

@ManyToOne(targetEntity = Department.class)
public void setDepartment(Department department) {
this.department = department;
}

public Role getRole() {
return role;
}

@ManyToOne(targetEntity = Role.class)
@JoinTable(name = "role")
public void setRole(Role role) {
this.role = role;
}


And location class:

@Entity
@Table(name = "location")
public class Location extends BaseEntity{
private List<NewUser> users;

public List<NewUser> getUsers() {
return users;
}

@OneToMany(targetEntity = NewUser.class, mappedBy = "location")
@JoinTable(name = "users")
public void setUsers(List<NewUser> users) {
this.users = users;
}
}


Base entity:

@MappedSuperclass
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}
}


The error is :

Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: location, for columns: [org.hibernate.mapping.Column(users)]


How to effectively make relational mapping using hibernate annotations ?

Answer

Your mappings are incorrect. Assuming you want a FK in users table pointing to corresponding location then use the following:

In User

@ManyToOne
@JoinColumn(name = "location_id")
public void setLocation(Location location) {
    this.location = location;
}

In Location

@OneToMany(mappedBy = "location")
public void setUsers(List<NewUser> users) {
    this.users = users;
}

If you did not want the foreign key to Location in the users table then you could use a join table 'user_locations' with columns user_id (FK to users) and location_id (FK to locations) and use the @JoinTable annotation to specify it accordingly. In your mappings this would look like:

@ManyToOne
@JoinTable(name = "user_locations", joinColumns = @JoinColumn(name="user_id"), inverseJoinColumns = @JoinColumn(name = "location_id"))
public void setLocation(Location location) {
    this.location = location;
}
Comments