'field list' 中的未知列 'categories1_.category'

Unknown column 'categories1_.category' in 'field list'

我有以下型号。

Employee.Java

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name="firstName")
    private String firstName;

    @Column(name="lastName")
    private String lastName;

    @Column(name = "category")
    @Enumerated(EnumType.STRING)
    @ElementCollection(fetch = FetchType.LAZY)
    @CollectionTable(name = "myobject_categories", joinColumns = @JoinColumn(name = "myobject_id"))
    private Set<Category> categories;
}

类别是一个枚举,如下所示。

public enum Category {
CATEGORY(1), CATEGORY2(2);

private final Integer id;
Category(Integer id) { this.id=id; }
public Integer getId() { return id; }
}

我的仓库如下,

public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
    @Query("from org.arunm.Employee c WHERE :category MEMBER OF c.categories")
    List<Employee> findAll(@Param("category") Set<Category> categories);
}

以下检索员工列表的代码不起作用。

Set<Category> categories = new HashSet<Category>();
categories.add(Category.CATEGORY);
List<Employee> list = customerRepository.findAll(categories);
customer.setCategories(categories);
customerRepository.findAll(categories);

在日志中,我看到查询生成为

select employee0_.id as id1_1_, employee0_.first_name as first_na2_1_, employee0_.last_name as last_nam3_1_ from employee employee0_ where ? in 
(select categories1_.category from myobject_categories categories1_ where employee0_.id=categories1_.myobject_id)

以下是我遇到的错误,

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'categories1_.category' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 

我的问题是在我的映射中是什么迫使休眠认为在 myobject_categories[= 中有一个名为 category 的列36=] table.

您告诉休眠使用 myobject_categories 作为映射 table。这样的映射 table 需要引用:一个返回到映射实体(在本例中为 Employee),然后返回到构成集合的实体。可能基于枚举名称,Hibernate 假定该字段的名称为 category

您可能需要考虑 different way of mapping Collections of enums