简体   繁体   中英

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

Hibernate is unable to determine the type for Set at the table Company. I am trying to create a foreign key of table Company through one-to-many relationship. One Company can have multiple users.

Company.java is given below:

@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 is as below:

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

And below is my database definition for 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;

you have to mention this relation at class level also in Company

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

in User

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM