Unknown column 'categories1_.category' in 'field list' 在 'field list' 中的 'categories1_.category' 列未知

10 浏览
0 Comments

Unknown column 'categories1_.category' in 'field list' 在 'field list' 中的 'categories1_.category' 列未知

我有以下的模型。

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

Category是一个枚举,如下所示。

public enum Category {
CATEGORY(1), CATEGORY2(2);
private final Integer id;
Category(Integer id) { this.id=id; }
public Integer getId() { return id; }
}

我的Repository如下所示,

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

以下代码用于检索员工列表,但无法正常工作。

Set categories = new HashSet();
categories.add(Category.CATEGORY);
List 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) 

我的问题是,是我的映射中的什么导致Hibernate认为在myobject_categories表中有一个名为category的列。

0
0 Comments

你告诉Hibernate使用myobject_categories作为映射表,这样的映射表需要有两个引用:一个回引到被映射的实体(在这种情况下是Employee),另一个引用到构成集合的实体。可能基于枚举名称,Hibernate假设该字段的名称为category

你可能需要考虑另一种映射枚举集合的方式

0