I have following Entity
@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;
}
Following is my Action
public enum Action {
PHONE, EMAIL, POSTAL,PHONE_OR_EMAIL, SMS;
}
I want to fetch a List of rule having particular set of actions I am trying this
DetachedCriteria criteria = DetachedCriteria.forClass(Rule.class,"rule");
criteria = criteria.createAlias("rule.actions", "action");
criteria.add(Restrictions.in("action.name",actionSet));
return getHibernateTemplate().findByCriteria(criteria);
But getting org.hibernate.MappingException: collection was not an association: exception..
EDIT So after guidance from jbrookover I tried going for a wrapper class for Action named as RuleAction and was able to eshtablish the oneToMany relationship, Also I modified the query as follows
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);
But this is returning me all the rule which are having either EMAIL or POSTAL but what I want is all the rule having EMAIL and POSTAL both Please help me in modifying the query.