[英]How can I implement these 2 simple queries using Spring data JPA with the “query creation from method names” strategy?
I am pretty new in Spring Data and I have to write what in the official documentation seems to be called Query creation from method names , here the reference: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation 我是Spring Data的新手,我必须在官方文档中写一些所谓的“ 从方法名称创建查询” ,此处为参考: https : //docs.spring.io/spring-data/jpa/docs/current /reference/html/#repositories.query-methods.query-creation
As you can see in the previous example show the creation of a query by the definition of a method name, for example: 如您在前面的示例中看到的,通过方法名称的定义显示了查询的创建,例如:
List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
that I think return a list of Person object that have a specific email affress and a specific lastname. 我认为返回具有特定电子邮件地址和特定姓氏的Person对象的列表。
So I am trying to do the same thing in my project that use Hibernate as JPA provider. 所以我试图在我的项目中使用Hibernate作为JPA提供程序做同样的事情。
In my project I have this Twb1012Regione entity class that map the anagrafiche.TWB1012_REGIONE on the database: 在我的项目中,我有一个Twb1012Regione实体类,该实体类将anagrafiche.TWB1012_REGIONE映射到数据库中:
@Entity
@Table(name="anagrafiche.TWB1012_REGIONE")
@NamedQuery(name="Twb1012Regione.findAll", query="SELECT t FROM Twb1012Regione t")
public class Twb1012Regione implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="COD_REG")
private String codReg;
@Column(name="COD_ARE_GEO")
private String codAreGeo;
@Column(name="COD_CIT")
private String codCit;
@Column(name="COD_IST")
private int codIst;
@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_REG")
private String desReg;
//bi-directional many-to-one association to Tpg1029Provnuoist
@OneToMany(mappedBy="twb1012Regione")
private List<Tpg1029Provnuoist> tpg1029Provnuoists;
//bi-directional many-to-one association to Twb1013Provincia
@OneToMany(mappedBy="twb1012Regione")
private List<Twb1013Provincia> twb1013Provincias;
public Twb1012Regione() {
}
public String getCodReg() {
return this.codReg;
}
public void setCodReg(String codReg) {
this.codReg = codReg;
}
public String getCodAreGeo() {
return this.codAreGeo;
}
public void setCodAreGeo(String codAreGeo) {
this.codAreGeo = codAreGeo;
}
public String getCodCit() {
return this.codCit;
}
public void setCodCit(String codCit) {
this.codCit = codCit;
}
public int getCodIst() {
return this.codIst;
}
public void setCodIst(int codIst) {
this.codIst = codIst;
}
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 getDesReg() {
return this.desReg;
}
public void setDesReg(String desReg) {
this.desReg = desReg;
}
public List<Tpg1029Provnuoist> getTpg1029Provnuoists() {
return this.tpg1029Provnuoists;
}
public void setTpg1029Provnuoists(List<Tpg1029Provnuoist> tpg1029Provnuoists) {
this.tpg1029Provnuoists = tpg1029Provnuoists;
}
public Tpg1029Provnuoist addTpg1029Provnuoist(Tpg1029Provnuoist tpg1029Provnuoist) {
getTpg1029Provnuoists().add(tpg1029Provnuoist);
tpg1029Provnuoist.setTwb1012Regione(this);
return tpg1029Provnuoist;
}
public Tpg1029Provnuoist removeTpg1029Provnuoist(Tpg1029Provnuoist tpg1029Provnuoist) {
getTpg1029Provnuoists().remove(tpg1029Provnuoist);
tpg1029Provnuoist.setTwb1012Regione(null);
return tpg1029Provnuoist;
}
public List<Twb1013Provincia> getTwb1013Provincias() {
return this.twb1013Provincias;
}
public void setTwb1013Provincias(List<Twb1013Provincia> twb1013Provincias) {
this.twb1013Provincias = twb1013Provincias;
}
public Twb1013Provincia addTwb1013Provincia(Twb1013Provincia twb1013Provincia) {
getTwb1013Provincias().add(twb1013Provincia);
twb1013Provincia.setTwb1012Regione(this);
return twb1013Provincia;
}
public Twb1013Provincia removeTwb1013Provincia(Twb1013Provincia twb1013Provincia) {
getTwb1013Provincias().remove(twb1013Provincia);
twb1013Provincia.setTwb1012Regione(null);
return twb1013Provincia;
}
}
So, into my project I have defined a Twb1012RegioneRepository interface that is my repository class defined on the previous Twb1012Regione entity class: 因此,在我的项目中,我定义了一个Twb1012RegioneRepository接口,该接口是在上一个Twb1012Regione实体类上定义的存储库类:
@RepositoryDefinition(domainClass=Twb1012Regione.class, idClass=String.class)
public interface Twb1012RegioneRepository extends JpaRepository<Twb1012Regione, String> {
// I have to implement it
}
Now my problem is that I want to create 2 methods (that implement 2 queries by method name as described by the previous tutorial) that perform the following tasks: 现在,我的问题是我想创建2个方法(执行如上一教程中所述的按方法名实现2个查询)来执行以下任务:
1) Return the list of all the Twb1012Regione representing all the record of the TWB1012_REGIONE table on the DB. 1)返回代表在DB的TWB1012_REGIONE表的所有记录的所有Twb1012Regione名单。
2) Given a specific id (the value of the String codReg field, PK of the Twb1012Regione class) I want to obtain the Twb1012Regione object associated to this record. 2)给定一个特定的ID( Twb1012Regione类的String codReg字段的值,PK),我想获取与此记录关联的Twb1012Regione对象。
How can I implement these queries? 如何实现这些查询? I have some difficulties to do it
我有一些困难
Tnx 特纳克斯
You don't need to implement the methods. 您不需要实现这些方法。 The Spring Data Repository API will construct query for you as the
JpaRepository
already has following methods: Spring Data Repository API将为您构造查询,因为
JpaRepository
已经具有以下方法:
That's the whole point with the Spring Data Repository - To reduce the boiler plate code that you write. 这就是Spring Data Repository的重点- 减少您编写的样板代码 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.