I have the 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;
My next action
public enum Action { PHONE, EMAIL, POSTAL,PHONE_OR_EMAIL, SMS; }
I want to get a list of rules with a specific set of actions I'm 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 Therefore, after the tutorial from jbrookover, I tried to switch to the wrapper class for Action named RuleAction and was able to establish the oneToMany relationship. I also changed 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 that returns me the whole rule that has EMAIL or POSTAL, but I want the whole rule with EMAIL and POSTAL and . Help me change the request.
hibernate hibernate-criteria
Anupam Gupta Oct 07 2018-11-12T00: 00Z
source share