【问题标题】:How to map a list of Objects into a table with hibernate in spring?如何在春季使用休眠将对象列表映射到表中?
【发布时间】:2016-12-10 16:49:12
【问题描述】:

我正在尝试将用户列表映射到位置对象,但出现映射异常。这是因为 List 对象不被数据库识别?或者为什么我会得到这个异常?

这是我的用户类:

@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注解有效地进行关系映射?

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    您的映射不正确。假设您希望用户表中的 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_locations”与列 user_id(FK 到用户)和 location_id(FK 到位置)并使用 @JoinTable 注释相应地指定它。在您的映射中,这看起来像:

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

    【讨论】:

      【解决方案2】:

      Hibernate 已经知道每个实体的表,因为您正在使用 @Table 注释指定它。您正在建立两个独立的关系,而不是双边关系。

      使用mappedBy,声明您要依赖的其他实体的字段:

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

      另请参阅:

      【讨论】:

      • 感谢@AlanHay,实际上是拥有关系的用户实体。
      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2015-08-20
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 2013-01-11
      • 1970-01-01
      相关资源
      最近更新 更多