[英]Hibernate: how to delete entities in many-to-many relationships with extra attributes
[英]How to query for entities with many-to-many relationships?
我有3个有关联的实体。 以下是我的课程的简化示例:
@Entity
public class Action {}
@Entity
public class Instance {
@ManyToMany
private Set<Action> actions;
@ManyToMany
private Set<CadSystem> cadSystems;
}
@Entity
public class CadSystem {}
如何查询属于特定Action
和CadSystem
所有Instance
? 例如,我想在JpaRepository
执行以下JpaRepository
:
public interface InstanceRepository extends JpaRepository<Instance, Long> {
List<Instance> findByActionAndCadSystem(Action action, CadSystem cadSystem);
}
但这是不可能的,因为Instance
没有名为action
和cadSystem
字段。 我认为以下方法会起作用:
public interface InstanceRepository extends JpaRepository<Instance, Long> {
List<Instance> findByActionsAndCadSystems(Set<Action> actions, Set<CadSystem> cadSystems);
}
但是在这种情况下,我将总是不得不创建一个只有一个元素的新Set
。
使用查询。 另外,请注意,由于关联是多对多的,因此此查询可能返回几个操作:
select i from Instance i
join i.actions action
join i.cadSystems cadSystem
where action = :action
and cadSystem = :cadSystem
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.