Hibernate criteria query for Collection Table?

后端 未结 3 1860
刺人心
刺人心 2020-12-10 06:13

I have following Entity

@Entity
@Table(name = \"rule\")
public class Rule implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AU         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-10 07:01

    The criteria query looks fine. Suppose we have tables:

    rule(id, name,...)
    action(id, name,..)
    rule_action(id, rule_id, action_id,...) -- (or rule_id+action_id as  a composite pkey)
    

    The mapping should be as follows:

    public class Rule {
       ...
       @ManyToMany(mappedBy = "rule")
       private Set actions;
       ...
    }
    
    public class Action {
       ...
       @JoinTable(
          name="rule_action",
          joinColumns={@JoinColumn(name="action_id")},
          inverseJoinColumns={@JoinColumn(name="rule_id")}
       )
       private Set rules;
       ...
    }
    

    This way your criteria query should work.

提交回复
热议问题