通过多个实体

时间:2016-03-02 19:39:34

标签: java spring jpa spring-data

我有4个JPA实体如下:

实体Farm

@Entity
public class Farm implements Serializable {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idFarm;
    private String nameFarm;
}

实体SAU

@Entity
public class SAU implements Serializable {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idSau;
    private String nameSau;
    @OneToOne
    @JoinColumn(name="farm_id")
    private Farm farm;
}

实体Part

@Entity
public class Part implements Serializable {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idPart;
    private String namePart;
    @ManyToOne
    @JoinColumn(name = "sau_id")
    private Sau sau;
}

实体Cult

@Entity
public class Cult implements Serializable {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idCult;
    private String nameCult;
    @OneToOne
    @JoinColumn(name = "part_id")
    private Part part;
}

我需要检索Collection Cultfarm_id = x个对象public Collection<Cult> = findCultByPartBySauByFarmIdFarm(Long idFarm); ,我显然不能通过使用以下方法的常规命名来实现:

@Query

我无法弄清楚如何在Spring Data Repository中编写自定义import numpy as np import matplotlib import matplotlib.pyplot as plt %matplotlib inline import scipy.stats x=np.linspace(1,len(my_data),len(my_data)) lambda_estimate=np.mean(my_data) prior= scipy.stats.gamma.pdf(x,alpha,beta) #the parameters dont matter for now likelihood_temp = lambda yi, a: scipy.stats.poisson.pmf(yi, a) likelihood = lambda y, a: np.log(np.prod([likelihood_temp(data, a) for data in my_data])) posterior=likelihood(my_data,lambda_estimate) * prior

2 个答案:

答案 0 :(得分:4)

没有显式连接的另一个版本。

@Query("select c from Cult c where c.part.sau.farm.idFarm = ?1")
public Collection<Cult> findCultByFarmId(Long idFarm);

我通常更喜欢使用它,因为我让下划线实现来处理联接。

答案 1 :(得分:2)

尝试:

@Query("select c from Cult c join c.part p join p.sau s join s.farm f where f.idFarm = ?1")
public Collection<Cult> = findCultByFarmId(Long idFarm);