org.hibernate.MappingException:无法确定:java.util.Set的类型,在表:Company,对于列:[org.hibernate.mapping.Column(users)]

时间:2016-06-04 13:31:36

标签: java mysql spring hibernate hibernate-onetomany

Hibernate无法确定表公司的Set类型。我正在尝试通过一对多关系创建表公司的外键。一家公司可以拥有多个用户。

Company.java如下:

@Entity
@Table(name = "Company")
@SuppressWarnings("serial")
public class Company implements Serializable {


    @Id
    @GeneratedValue
    @Column(name = "companyId", length = 32 )
    private int companyId;

    private Set<User> users;

    @Column(name = "companyName")
    String companyName;

    @Column(name = "typeOfBusiness")
    String typeOfBusiness;


    public int getCompanyId() {
        return companyId;
    }

    public void setCompanyId(int id) {
        this.companyId = id;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getTypeOfBusiness() {
        return typeOfBusiness;
    }

    public void setTypeOfBusiness(String typeOfBusiness) {
        this.typeOfBusiness = typeOfBusiness;
    }


    public Set<User> getUsers() {
        return this.users;
    }

    @OneToMany(mappedBy="company", cascade=CascadeType.ALL, fetch=FetchType.LAZY, orphanRemoval=true)
    public void setUsers(Set<User> users) {
        this.users = users;
    }

    public Company() {
    }

    public Company(int companyId, Set<User> users, String companyName, String typeOfBusiness) {
        this.companyId = companyId;
        this.users = users;
        this.companyName = companyName;
        this.typeOfBusiness = typeOfBusiness;
    }
}

User.java如下:

@Entity
@Table(name = "User")
@SuppressWarnings("serial")
public class User implements Serializable {

    @Id
    @GeneratedValue
    @Column(name = "id", length = 11 )
    private Long id;

    private Company company;

    @Column(name = "userName")
    String userName;

    @Column(name = "userPassword")
    String userPassword;

    @Column(name = "userEmail")
    String userEmail;


    public Long getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public Company getCompany() {
        return this.company;
    }

    @ManyToOne
    @JoinColumn( name="companyId",insertable=false, updatable=false, nullable = false)
    public void setCompany(Company company) {
        this.company = company;
    }

    public User(){}

    public User(Long id, Company company, String userName, String userPassword, String userEmail) {
        super();
        this.id = id;
        this.company = company;
        this.userName = userName;
        this.userPassword = userPassword;
        this.userEmail = userEmail;
    }
}

以下是我对mysql的数据库定义:

CREATE TABLE `Company` (                   
          `companyId` bigint(32) NOT NULL AUTO_INCREMENT,  
          `companyName` varchar(50) NOT NULL,
          `typeOfBusiness` varchar(50),
          PRIMARY KEY (`companyId`)                     
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `User` (                   
          `id` int(11) NOT NULL AUTO_INCREMENT,  
          `userName` varchar(50) NOT NULL,      
          `userPassword` varchar(50) NOT NULL,
          `userEmail` varchar(50),
          `phoneNumber` bigint(32),
          `companyId` bigint(32),
          PRIMARY KEY (`id`),
          FOREIGN KEY (companyId) REFERENCES Company(companyId) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

1 个答案:

答案 0 :(得分:1)

你也必须在课堂上提到这种关系 在@OneToMany(fetch = FetchType.LAZY, mappedBy = "company") private Set<User> users;

User
@ManyToOne @JoinColumn(name="company_id") private Company company;

中的

case