我有以下实体
@Entity
@Table(name = "rule")
public class Rule implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "rule_id")
private Long id;
@ElementCollection(targetClass = Action.class)
@CollectionTable(name = "rule_action", joinColumns = @JoinColumn(name = "rule_id"))
@Enumerated(value = EnumType.STRING)
@Column(name = "action")
private Set<Action> actions;
//After editing as per jbrookover's suggestion adding a new mapping
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "rule_id")
private Set<RuleAction> ruleActions;
}
以下是我的行动
public enum Action {
PHONE, EMAIL, POSTAL,PHONE_OR_EMAIL, SMS;
}
我想获取具有特定操作集的规则列表我正在尝试这个
DetachedCriteria criteria = DetachedCriteria.forClass(Rule.class,"rule");
criteria = criteria.createAlias("rule.actions", "action");
criteria.add(Restrictions.in("action.name",actionSet));
return getHibernateTemplate().findByCriteria(criteria);
但是得到 org.hibernate.MappingException: collection was not an association: exception..
编辑 因此,在 jbrookover 的指导下,我尝试使用名为 RuleAction 的 Action 包装类,并且能够建立 oneToMany 关系,我还修改了查询,如下所示
Set<Action> act = new HashSet<Action>();
act.add(Action.EMAIL);
act.add(Action.POSTAL);
DetachedCriteria criteria = DetachedCriteria.forClass(Rule.class);
criteria.add(Restrictions.eq(SUPPORT_LANG, Language.valueOf("EN")))
.createCriteria("ruleActions").add(Restrictions.in("action",act));
return getHibernateTemplate().findByCriteria(criteria);
但这将返回给我所有具有 EMAIL 或 POSTAL 的规则,但我想要的是所有具有 EMAIL 和 POSTAL 的规则请帮助我修改查询。