[英]Hibernate Criteria Query for many to many Enum
I have a class Comment: 我有一堂课评论:
@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;
}
Persisting the comment class works but the following criteria query: 保留注释类有效,但可以查询以下条件:
Criteria criteria = session.createCriteria(Comment.class)
.add(Restrictions.eq("commentTopics", topic));
List<Comment> entries = criteria.list();
throws org.hibernate.exception.DataException: No value specified for parameter 1. 抛出org.hibernate.exception.DataException:没有为参数1指定值。
This is the query built: 这是建立的查询:
select this_.comment_id as comment1_0_0_, this_.comment as comment0_0_, commenttop2_.comment_id as comment1_0_2_, commenttop2_.topic as topic2_ from comments this_ left outer join comments_topic commenttop2_ on this_.comment_id=commenttop2_.comment_id where this_.comment_id=? 从注释this_中,从this_.comment_id = commenttop2_.comment_id上的外部加入comment_topic commenttop2_的注释中选择this_.comment_id作为注释1_0_0_,this_.comment作为注释0_0_,commenttop2_.comment_id作为注释1_0_2_,commenttop2_.topic作为topic2_。
Am I using incorrect annotations? 我使用的注释不正确吗?
Is the criteria query not being constructed properly? 条件查询是否构建正确?
I placed the CommentTopic enum in a CommentTopicWrapper class. 我将CommentTopic枚举放在CommentTopicWrapper类中。
I updated the annotation for the commentTopicsSet to: 我将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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.