0
如何在彈簧數據jpa中執行本機查詢,同時獲取子實體?如果我在子實體對象上有Eager FetchType,則spring數據正在執行2個查詢。 1爲父母和1爲子實體。彈簧數據jpa加入本地查詢
有沒有辦法只執行1個本地查詢來獲取父和子實體?
父:
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Temporal(TemporalType.TIMESTAMP)
private Date ts;
@ManyToOne(fetch=FetchType.LAZY
private Child child;
}
孩子:
@Entity
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@OneToMany(fetch=FetchType.LAZY, mappedBy="parent")
private Parent parent;
}
查詢:
public interface ParentRepository extends Repository<Parent, Integer> {
@Query(value = "SELECT * from parents p inner join children c on c.id=p.childId where TIMESTAMPDIFF(SECOND, p.ts, CURRENT_TIMESTAMP) < :interval", nativeQuery = true)
Parent findOneByInterval(@Param("interval") long interval);
}
我使用休眠5和MySQL隨着春天的數據JPA。
我也嘗試添加一個@NamedEntityGraph父類和@EntityGraph的查詢方法,沒有運氣
春天犯規執行任何疑問,你的JPA提供者。 –
@NeilStockton對吧,但是spring或者hibernate做了實體映射嗎? – rodney757