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

org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Company, for columns: [org.hibernate.mapping.Column(users)]

Hibernate无法确定表公司设置的类型。我试图通过一对多关系创建表公司的外键。一个公司可以有多个用户。

company.java如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@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如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@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的数据库定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;

你也必须在课堂上提到这种关系在Company

1
2
@OneToMany(fetch = FetchType.LAZY, mappedBy ="company")
 private Set<User> users;

User

1
2
3
@ManyToOne
@JoinColumn(name="company_id")
private Company company;