未知栏' categories1_.category'在'字段列表'

时间:2015-05-10 11:46:34

标签: spring hibernate jpa-2.0 spring-data spring-data-jpa

我有以下型号。

Employee.Java

refs/remotes

类别是Enum,如下所示。

@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)

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

1 个答案:

答案 0 :(得分:1)

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

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