Hibernate Criteria查询多对多Enum

时间:2013-02-27 15:39:13

标签: java hibernate hibernate-criteria

我有一个课程评论:

@Entity
@Table(name = Constants.COMMENTS_TABLE)
@Audited
public class Comment {


@Column(name = "comment", nullable = false)
private String comment;

@ElementCollection(targetClass = CommentTopic.class)
@Enumerated(EnumType.STRING)
@Fetch(value = FetchMode.JOIN)
@CollectionTable(name = Constants.COMMENTS_TOPIC_JOIN_TABLE, joinColumns = @JoinColumn(name = "comment_id"))
@Column(name = "topic")
private Set<CommentTopic> commentTopics;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "comment_id", nullable = false)
private Long commentId;
}

保留评论类有效,但以下标准查询:

Criteria criteria = session.createCriteria(Comment.class)
         .add(Restrictions.eq("commentTopics", topic));

List<Comment> entries = criteria.list();

throws org.hibernate.exception.DataException:没有为参数1指定值。

这是构建的查询:

选择this_.comment_id为comment1_0_0_,this_.comment为comment0_0_,commenttop2_.comment_id为comment1_0_2_,commenttop2_.topic为topic2_,来自评论this_ left outer join comments_topic commenttop2_ on this_.comment_id = commenttop2_.comment_id where this_.comment_id =?< / p>

我使用了错误的注释吗?

标准查询是否未正确构建?

1 个答案:

答案 0 :(得分:0)

我将CommentTopic枚举放在CommentTopicWrapper类中。

我将commentTopicsSet的注释更新为:

@OneToMany(targetEntity = CommentTopicWrapper.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = Constants.COMMENTS_TOPIC_JOIN_TABLE, joinColumns = @JoinColumn(name = "comment_id"), inverseJoinColumns = @JoinColumn(name = "topic"))
private Set<CommentTopicWrapper> commentTopics;