如何通过使用弹簧数据jpa进行连接来从多个实体返回对象? [英] How to return objects from multiple entity by joining by using spring data jpa?

查看:95
本文介绍了如何通过使用弹簧数据jpa进行连接来从多个实体返回对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个实体:EntityA,EntityB和EntityC。
从这些实体中,我需要通过使用弹簧数据jpa从联接查询中获取值到对象列表中。
Query是:

 选择x.id,x.formNo,x.name,z.testScore,y。学期
来自EntityA x离开连接EntityB作为z on x.id = z.a_id
内部连接EntityC as y on x.c_id = y.id其中x.id = 1

实体为:



EntityA:

  @Entity 
public class EntityA {
@Id
@GeneratedValue
private Integer id;
私人字符串名称;
私人字符串formNo;

@OneToOne(mappedBy =a,fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
private EntityB b;

@ManyToOne
@JoinColumn(name =EntityC_id)
private EntityC c;

实体B:

 @GeneratedValue 
私人整数id; $实体
公共类EntityB {

@Id
@GeneratedValue
私人整数ID;
私人双测试分数;

@OneToOne
@JoinColumn(name =EntityA_id)
private EntityA a;

实体C:



<$
@GeneratedValue
私人整数id; $实体
公共类EntityC {
@Id
@GeneratedValue
私人整数ID;
私人字符串学期;

@OneToMany(mappedBy =c,fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
private List< EntityA>一个;
}

我已经尝试过这种方式了

  @Repository 
public interface SomeObjectRepository扩展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);


解决方案

您只需要认识到JPQL是与SQL不同的语言,并且可以学习它。 JPQL从不使用表名和列名。 JPQL加入依赖于实体之间的关联,而不是在 ON 子句中。



查询应该简单地为

 从EntityA中选择x.id,x.formNo,x.name,z.testScore,y.semester 
x
左连接xb z
内连接xc y
其中x.id =:id


I have three entities: EntityA, EntityB and EntityC. From those entities I need to get value from the Joining Query into a list of objects by using spring data jpa. Query is:

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

The entities are:

EntityA:

  @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;
}

EntityB:

@Entity
public class EntityB {

@Id
@GeneratedValue
private Integer id;     
private double testScore;

@OneToOne
@JoinColumn(name = "EntityA_id")
private EntityA a;  
}

EntityC:

@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;    
}

I have tried like this

@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);
  }

解决方案

You just need to realize that JPQL is a different language from SQL, and learn it. JPQL never uses table and column names. JPQL joins rely on associations between entities, and not on an ON clause.

The query should thus simply be

select x.id,x.formNo,x.name, z.testScore, y.semester
from EntityA x 
left join x.b z
inner join x.c y
where x.id = :id

这篇关于如何通过使用弹簧数据jpa进行连接来从多个实体返回对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆