2016-08-04 58 views
0

我想將用戶列表映射到位置對象,但我得到了映射異常。這是因爲List對象不被數據庫識別?或者爲什麼我會得到這個異常?如何在Spring中將對象列表映射到具有Hibernate的表中?

這是我的用戶等級:

@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; 
    } 

和位置類:

@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; 
    } 
} 

基礎機構:

@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; 
    } 
} 

的錯誤是:

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

如何使用hibernate註釋有效地進行關係映射?

回答

3

你的映射是不正確的。假設你想有一個FK在用戶表指向相應的位置,然後使用以下命令:

在用戶

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

在位置

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

如果你不想在國外關鍵位置用戶表然後可以使用具有列user_id(FK到用戶)和location_id(FK到位置)的連接表'user_locations'並使用@JoinTable註釋來相應地指定它。在你的映射中,這看起來像:

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

Hibernate已經知道每個實體的表格,因爲您使用@Table註釋來指定它。你建立了兩個獨立的關係,而不是雙方關係。

使用mappedBy,申報等entitiy的領域要依靠:

Location.java

@OneToMany(fetch=FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy="location") 
public List<NewUser> getUsers() { 
    return users; 
} 

NewUser.java

@ManyToOne(fetch=FetchType.LAZY) 
@JoinColumn(name="id_user") 
public Location getLocation() { 
    return location; 
} 

參見:

+0

謝謝@AlanHay,它實際上是擁有關係的用戶實體。 –

相關問題