我有一项任务是将左外连接转换为内连接。我在使用hibernate。 我有实体:
@Entity
@Table("entity1")
public Entity1{
@Id
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
private Entity2 field;
}
@Entity
@Table("entity2")
public class Entity2{
@Id
private String name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinTable(name = "entity3_entity2",
joinColumns = @JoinColumn(name="entity2_name")
inverseJoinColumns = @JoinColumn(name = "entity3_name"))
private Entity3 field;
}
@Entity
@Table("entity3")
public class Entity3{
@Id
private String name;
@OneToMany
private Set<Entity2> field;
}
我有hql:
SELECT e1 FROM Entity1 e1 inner join fetch e1.field e2 inner join fetch e2.field where e1.id = ?1
结果SQL是:
select ... from entity1 inner join entity2 on ... left outer join entity3_entity2 on ...
但是我需要在没有任何sql查询的情况下使用内部连接获取entity3记录。有办法吗?
答案 0 :(得分:0)
要在hibernate中使用内部联接获取记录,可以使用以下查询。
<query name="findEntities">
<query-param name="id" type="long" /> //pass id to here
SELECT e1 FROM Entity1 e1
inner join fetch e1.field e2
inner join fetch e2.field e3
where e1 = :id
</query>
或者
@Query("SELECT e1 FROM Entity1 e1 inner join fetch e1.field e2 inner join fetch e2.field e3 where e1 = ?1")