Spring 无法将本机sql查询转换为jpa查询
嗨,我是jpql新手,在postgres中使用spring数据jpa,我无法翻译下面的查询Spring 无法将本机sql查询转换为jpa查询,spring,postgresql,spring-data-jpa,Spring,Postgresql,Spring Data Jpa,嗨,我是jpql新手,在postgres中使用spring数据jpa,我无法翻译下面的查询 select "user".table_1.id, "user".table_2.name, "user".table_2.email from "user".table_1 left outer join "user".table_2 on "user".table_1.table2_id = "user".table_2
select
"user".table_1.id, "user".table_2.name,
"user".table_2.email
from
"user".table_1
left outer join
"user".table_2
on
"user".table_1.table2_id = "user".table_2.id
where
"user".table_1.parent_id=5
下面是我的模型课
@entity
@table(name="table_1)
class Table1{
@id
@GeneratedValue
private Long id;
@OneToOne(mappedBy = "table_2")
private Table2 table_2;
@ManyToOne
@JoinColumn(name = "parent_id")
private Table1 parent_id;
@OneToMany(mappedBy = "account", fetch = FetchType.LAZY)
private List<Table1> childs;
}
@entity
@table(name="table_2)
class Table2
{
@id
private Long id;
private String emailId;
private String name;
@OneToOne
@JoinColumn(name = "table1_id")
private Table1 table1;
}
请帮我解决这个错误
如果你需要帮助,请告诉我。
谢谢:)您的JPA查询看起来像(但未经测试)
@Query(“选择t1.id作为table1Id,选择t2.name作为name,选择t2.emailId作为来自Table1的电子邮件t1加入table_2 t2,其中t1.parent_id=:parentId”)
public ListfindByParentId(长parentId){
公共接口自定义{
私人长表1id;
私有字符串名称;
私人字符串电子邮件;
}
@query("select t1.id, t2.name,t2.email from Table1 t1 left outer join
t2.table_2 where t1.parent_id=?1")
public List<CustomDTO>findByParentId(Long parentId);
public class CustomDTO{
private Long table1Id;
private String name;
private String email;
}
select
table10_.id as col_0_0_,
table21_.name as col_1_0_,
table21_.email as col_2_0_
from
"user".table1 table0_
left outer join
"user".table2 table_21_
on table10_.id=table_21_.table_1 where
table0_.parent_id=?
@Query("select t1.id as table1Id, t2.name as name ,t2.emailId as email from Table1 t1 join table_2 t2 where t1.parent_id= :parentId")
public List<CustomDTO>findByParentId(Long parentId){
public interface CustomDTO{
private Long table1Id;
private String name;
private String email;
}