我有三个实体:EntityA,EntityB和EntityC。我需要使用Spring Data JPA从这些实体中通过连接查询获取值,并将其放入对象列表中。 查询如下:
select x.id,x.formNo,x.name, z.testScore, y.semester
from EntityA as x left join EntityB as z on x.id = z.a_id
inner join EntityC as y on x.c_id = y.id where x.id=1
实体包括:
实体A:
@Entity
public class EntityA {
@Id
@GeneratedValue
private Integer id;
private String name;
private String formNo;
@OneToOne(mappedBy = "a",fetch=FetchType.LAZY, cascade = CascadeType.REMOVE)
private EntityB b;
@ManyToOne
@JoinColumn(name = "EntityC_id")
private EntityC c;
}
实体B:
@Entity
public class EntityB {
@Id
@GeneratedValue
private Integer id;
private double testScore;
@OneToOne
@JoinColumn(name = "EntityA_id")
private EntityA a;
}
实体C:
@Entity
public class EntityC {
@Id
@GeneratedValue
private Integer id;
private String semester;
@OneToMany(mappedBy = "c",fetch=FetchType.LAZY, cascade = CascadeType.REMOVE)
private List<EntityA> a;
}
我尝试了这样做。
@Repository
public interface SomeObjectRepository extends JpaRepository<Object, Integer>{
public final static String FIND_WITH_QUERY
= "select x.id,x.formNo,x.name, z.testScore, y.semester
from EntityA as x left join EntityB as z on x.id = z.a_id
inner join EntityC as y on x.c_id = y.id where x.id=:id";
@Query(FIND_WITH_QUERY)
public List<Object> getObjects(@Param("id") String id);
}