我在 Spring Data JPA 项目中绝对是新手,我遇到了以下问题。
我有这两个类来映射2个表以及它们之间连接的相关关系。
1) Tgu1002Anagrafeprofilo 在我的数据库上映射 TGU1002_ANAGRAFEPROFILO 表:
/**
* The persistent class for the TGU1002_ANAGRAFEPROFILO database table.
*
*/
@Entity
@Table(name="profdb.TGU1002_ANAGRAFEPROFILO")
@NamedQuery(name="Tgu1002Anagrafeprofilo.findAll", query="SELECT t FROM Tgu1002Anagrafeprofilo t")
public class Tgu1002Anagrafeprofilo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="COD_PRF")
private String codPrf;
@Column(name="COD_PGM_ULT_MOV")
private String codPgmUltMov;
@Column(name="COD_UTE_ULT_MOV")
private String codUteUltMov;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="DAT_ORA_ULT_MOV")
private Date datOraUltMov;
@Column(name="DES_PRF")
private String desPrf;
//bi-directional many-to-one association to Tgu1001Anagrafeapplicazione
@ManyToOne
@JoinColumn(name="COD_APP")
private Tgu1001Anagrafeapplicazione tgu1001Anagrafeapplicazione;
//bi-directional many-to-one association to Tgu1007Gesutenteprfcontesto
@OneToMany(mappedBy="tgu1002Anagrafeprofilo")
private List<Tgu1007Gesutenteprfcontesto> tgu1007Gesutenteprfcontestos;
//bi-directional many-to-many association to Tgu1003Anagrafefunzione
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="profdb.TGU1008_PROFILOFUNZIONE",
joinColumns=
@JoinColumn(name="COD_PRF", referencedColumnName="COD_PRF"),
inverseJoinColumns=
@JoinColumn(name="COD_FNZ", referencedColumnName="COD_FNZ")
)
//@JoinColumn(name="COD_PRF")
private List<Tgu1003Anagrafefunzione> tgu1003Anagrafefunziones;
public Tgu1002Anagrafeprofilo() {
}
public String getCodPrf() {
return this.codPrf;
}
public void setCodPrf(String codPrf) {
this.codPrf = codPrf;
}
public String getCodPgmUltMov() {
return this.codPgmUltMov;
}
public void setCodPgmUltMov(String codPgmUltMov) {
this.codPgmUltMov = codPgmUltMov;
}
public String getCodUteUltMov() {
return this.codUteUltMov;
}
public void setCodUteUltMov(String codUteUltMov) {
this.codUteUltMov = codUteUltMov;
}
public Date getDatOraUltMov() {
return this.datOraUltMov;
}
public void setDatOraUltMov(Date datOraUltMov) {
this.datOraUltMov = datOraUltMov;
}
public String getDesPrf() {
return this.desPrf;
}
public void setDesPrf(String desPrf) {
this.desPrf = desPrf;
}
public Tgu1001Anagrafeapplicazione getTgu1001Anagrafeapplicazione() {
return this.tgu1001Anagrafeapplicazione;
}
public void setTgu1001Anagrafeapplicazione(Tgu1001Anagrafeapplicazione tgu1001Anagrafeapplicazione) {
this.tgu1001Anagrafeapplicazione = tgu1001Anagrafeapplicazione;
}
public List<Tgu1007Gesutenteprfcontesto> getTgu1007Gesutenteprfcontestos() {
return this.tgu1007Gesutenteprfcontestos;
}
public void setTgu1007Gesutenteprfcontestos(List<Tgu1007Gesutenteprfcontesto> tgu1007Gesutenteprfcontestos) {
this.tgu1007Gesutenteprfcontestos = tgu1007Gesutenteprfcontestos;
}
public Tgu1007Gesutenteprfcontesto addTgu1007Gesutenteprfcontesto(Tgu1007Gesutenteprfcontesto tgu1007Gesutenteprfcontesto) {
getTgu1007Gesutenteprfcontestos().add(tgu1007Gesutenteprfcontesto);
tgu1007Gesutenteprfcontesto.setTgu1002Anagrafeprofilo(this);
return tgu1007Gesutenteprfcontesto;
}
public Tgu1007Gesutenteprfcontesto removeTgu1007Gesutenteprfcontesto(Tgu1007Gesutenteprfcontesto tgu1007Gesutenteprfcontesto) {
getTgu1007Gesutenteprfcontestos().remove(tgu1007Gesutenteprfcontesto);
tgu1007Gesutenteprfcontesto.setTgu1002Anagrafeprofilo(null);
return tgu1007Gesutenteprfcontesto;
}
public List<Tgu1003Anagrafefunzione> getTgu1003Anagrafefunziones() {
return this.tgu1003Anagrafefunziones;
}
public void setTgu1003Anagrafefunziones(List<Tgu1003Anagrafefunzione> tgu1003Anagrafefunziones) {
this.tgu1003Anagrafefunziones = tgu1003Anagrafefunziones;
}
}
2) Tgu1003Anagrafefunzione 类,用于映射数据库中的 TGU1003_ANAGRAFEFUNZIONE 表:
/**
* The persistent class for the TGU1003_ANAGRAFEFUNZIONE database table.
*
*/
@Entity
@Table(name="profdb.TGU1003_ANAGRAFEFUNZIONE")
@NamedQuery(name="Tgu1003Anagrafefunzione.findAll", query="SELECT t FROM Tgu1003Anagrafefunzione t")
public class Tgu1003Anagrafefunzione implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="COD_FNZ")
private String codFnz;
@Column(name="COD_PGM_ULT_MOV")
private String codPgmUltMov;
@Column(name="COD_UTE_ULT_MOV")
private String codUteUltMov;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="DAT_ORA_ULT_MOV")
private Date datOraUltMov;
@Column(name="DES_FNZ")
private String desFnz;
@Column(name="DES_URL_FNZ")
private String desUrlFnz;
//bi-directional many-to-one association to Tgu1004Tipofunzione
@ManyToOne
@JoinColumn(name="COD_TIP_FNZ")
private Tgu1004Tipofunzione tgu1004Tipofunzione;
//bi-directional many-to-many association to Tgu1002Anagrafeprofilo
@ManyToMany(mappedBy="tgu1003Anagrafefunziones")
private List<Tgu1002Anagrafeprofilo> tgu1002Anagrafeprofilos;
public Tgu1003Anagrafefunzione() {
}
public String getCodFnz() {
return this.codFnz;
}
public void setCodFnz(String codFnz) {
this.codFnz = codFnz;
}
public String getCodPgmUltMov() {
return this.codPgmUltMov;
}
public void setCodPgmUltMov(String codPgmUltMov) {
this.codPgmUltMov = codPgmUltMov;
}
public String getCodUteUltMov() {
return this.codUteUltMov;
}
public void setCodUteUltMov(String codUteUltMov) {
this.codUteUltMov = codUteUltMov;
}
public Date getDatOraUltMov() {
return this.datOraUltMov;
}
public void setDatOraUltMov(Date datOraUltMov) {
this.datOraUltMov = datOraUltMov;
}
public String getDesFnz() {
return this.desFnz;
}
public void setDesFnz(String desFnz) {
this.desFnz = desFnz;
}
public String getDesUrlFnz() {
return this.desUrlFnz;
}
public void setDesUrlFnz(String desUrlFnz) {
this.desUrlFnz = desUrlFnz;
}
public Tgu1004Tipofunzione getTgu1004Tipofunzione() {
return this.tgu1004Tipofunzione;
}
public void setTgu1004Tipofunzione(Tgu1004Tipofunzione tgu1004Tipofunzione) {
this.tgu1004Tipofunzione = tgu1004Tipofunzione;
}
public List<Tgu1002Anagrafeprofilo> getTgu1002Anagrafeprofilos() {
return this.tgu1002Anagrafeprofilos;
}
public void setTgu1002Anagrafeprofilos(List<Tgu1002Anagrafeprofilo> tgu1002Anagrafeprofilos) {
this.tgu1002Anagrafeprofilos = tgu1002Anagrafeprofilos;
}
}
正如您在 Tgu1002Anagrafeprofilo 类中所看到的,确定了与Tgu1003Anagrafefunzione 类的双向多对多关联(在DB上传递的关系模型中)到另一个关联表),这一个:
//bi-directional many-to-many association to Tgu1003Anagrafefunzione
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="profdb.TGU1008_PROFILOFUNZIONE",
joinColumns=
@JoinColumn(name="COD_PRF", referencedColumnName="COD_PRF"),
inverseJoinColumns=
@JoinColumn(name="COD_FNZ", referencedColumnName="COD_FNZ")
)
//@JoinColumn(name="COD_PRF")
private List<Tgu1003Anagrafefunzione> tgu1003Anagrafefunziones;
所以在这个执行查询的项目中,它使用了所谓的从方法名称创建查询,这里有一个参考:
http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods
所以,我将有一个包含这样的东西的存储库接口来执行查询(这是在这个项目中找到的另一个存储库类,只是为了展示它是如何完成的):
@RepositoryDefinition(domainClass=Tid022Anagraficarup.class, idClass=Integer.class)
public interface Tid022AnagraficarupRepository extends JpaRepository<Tid022Anagraficarup, Integer> {
public Tid022Anagraficarup findByCodFisRup(@Param("codFisRup") String codFisRup);
}
所以我认为这个存储库基于一个名为 Tid022Anagraficarup 的类,并使用此类\ table中定义的 codFisRup 参数的值在此表上执行aqyery。
因此回到与我之前的实体映射类相关的问题,我必须实现一个简单的查询(使用另一个存储库中的方法),它执行以下操作:
检索所有 Tgu1003Anagrafefunzione 对象的列表,这一个:
private List<Tgu1003Anagrafefunzione> tgu1003Anagrafefunziones;
Tgu1002Anagrafeprofilo codPrf 字段的特定值。
所以我基于 Tgu1002Anagrafeprofilo 类实现了存储库接口,如下所示:
@RepositoryDefinition(domainClass=Tgu1002Anagrafeprofilo.class, idClass=String.class)
public interface Tgu1002AnagrafeprofiloRepository extends JpaRepository<Tgu1002Anagrafeprofilo, String> {
........................................................
........................................................
HERE THE QUERY METHOD DEFINITION
........................................................
........................................................
}
现在我认为我“只”使用prvious存储库类的相同系统来定义查询方法,但我对如何使用它没有太多想法。
我想也许我只能通过 Tgu1002Anagrafeprofilo 的 codPrf 值将其作为参数传递,因为 Tgu1003Anagrafefunzione 的列表相关我想要检索的对象似乎是急切加载的,所以当我重新检索 Tgu1002Anagrafeprofilo 对象时,可能会自动检索它。
您能否就如何使用Spring Data JPA实现此查询给出一些建议?
答案 0 :(得分:0)
要创建一个返回给定Tgu1002Anagrafeprofilo
的所有codPrf
实体的查找程序,您只需将接口声明为:
public interface Tgu1002AnagrafeprofiloRepository extends JpaRepository<Tgu1002Anagrafeprofilo, String> {
List<Tgu1002Anagrafeprofilo> findByCodPrf(String codPrf);
}
完成后,请检查Spring配置。如果已经使用了Data JPA,则配置应包含唤醒框架的注释:@EnableJpaRepositories
。注释还可能列出框架查找DAO的包,因此请确保列出正确的包。
基本上就是这样。一旦界面和注释都到位,只需在需要的地方编辑DAO @Inject
的实例。
编辑:如果您的实体的关系设置正确,那么您确实可以从获取的实体获得。