2016-06-04 2 views
0

Hibernate не может определить тип Set за столом компании. Я пытаюсь создать внешний ключ таблицы компании через отношения «один ко многим». Одна компания может иметь несколько пользователей.org.hibernate.MappingException: Не удалось определить тип для: java.util.Set, at table: Company, для столбцов: [org.hibernate.mapping.Column (users)]

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

у вас есть упомянуть это отношение на уровне класса также в Company

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

в User

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

вы можете удалить 'выборки = FetchType.LAZY,' – bphilipnyc