我有一个关于使用嵌套的“ custom”子列表创建具有自定义响应(弹簧投影)的本机查询的问题,即我正在尝试使用嵌套的子列表生成JSON输出。
子实体为:
@Entity
public class Child {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@NotNull
@Column(nullable = false)
private String firstName;
@NotNull
@Column(nullable = false)
private String secondName;
@Enumerated(EnumType.STRING)
private Gender gender;
private Date dateOfBirth;
private String phone;
@ManyToOne
@JoinColumn(name="child_id")
private List<Parent> parents = new ArrayList<Parent>();
//...
}
父实体例如:
@Entity
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@NotNull
@Column(nullable = false)
private String firstName;
@NotNull
@Column(nullable = false)
private String secondName;
private Date dateOfRegistration;
@OneToMany(fetch = FetchType.LAZY)
private List<Child> child = new ArrayList<Child>();
//...
}
投影界面:
public interface ChildProjectionInterface {
public int getParentId();
public Date getFirstName();
public List<ChildResponse> getChildData();
interface ChildResponse {
public int getChildID();
public String getFirstName();
}
}
查询(但显然不起作用):
@Query(value = "SELECT p.id AS parentId, p.firstName AS firstName, c.id AS childData.childId, c.firstName AS childData.firstName FROM parent p LEFT JOIN child c ON p.child_id = c.child_id AND p.secondName = :secondName", nativeQuery = true)
List<ChildProjectionInterface> getListWithSubList(@Param(value ="secondName") String secondName);
我正在阅读,研究和尝试..但是没有任何效果(我看到了https://medium.com/swlh/spring-data-jpa-projection-support-for-native-queries-a13cd88ec166,看到了“ for json auto”子句,但没有看到spring jpa等)
答案 0 :(得分:0)
您尝试过jpa fetch运算符吗?
@Query("select p from parent p left join fetch p.child c where p.secondName = :secondName")