Spring-Data-Jpa存储库 - 实体列名称的下划线 [英] Spring-Data-Jpa Repository - Underscore on Entity Column Name
问题描述
我在spring webmvc项目中使用spring-data-jpa。我正在使用查询创建在我的一个实体的存储库中。您可以在下面看到我的实体,我的存储库和异常。
I am using spring-data-jpa on a spring webmvc project. I am facing an issue using query creation on a Repository of one of my Entities. Below you can see my Entity, my Repository and the Exception.
我的实体,
@Entity
@Table(schema = "mainschema")
@XmlRootElement
public class Municipalperson implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Integer id;
@Basic(optional = false)
@Column(name = "municipal_id", nullable = false)
private Integer municipal_id;
@Basic(optional = false)
@Column(nullable = false, length = 60)
private String firstname;
public Municipalperson(Integer id, Integer municipal_id, String firstname) {
this.id = id;
this.municipal_id = municipal_id;
this.firstname = firstname;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getMunicipal_id() {
return municipal_id;
}
public void setMunicipal_id(Integer municipal_id) {
this.municipal_id = municipal_id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}
我的存储库,
@Repository
public interface MunicipalpersonRepository extends JpaRepository<Municipalperson, Integer> {
List<Municipalperson> findByMunicipal_idOrderByLastnameDesc(int municipal_id);
}
和例外,
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'municipalpersonRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property municipal found for type Municipalperson!
我试图将mynicipal_id设置为int,然后设置为Integer,并将参数'municipal_id'设置为相同我的存储库,但他们都没有工作。此外,我将存储库重命名为'findByMunicipalidOrderByLastnameDesc'和'findByMunicipalIdOrderByLastnameDesc',但它既不起作用。
I tried to set mynicipal_id as int, then as Integer and the same for the parameter 'municipal_id' on my Repository, but they all didn't worked. Also, i rename the Repository to 'findByMunicipalidOrderByLastnameDesc' and 'findByMunicipalIdOrderByLastnameDesc' but it didn't worked neither.
最后我重命名将city_id更改为municipalId(删除了下划线),并重命名getter / setter和Repository(findByMunicipalIdOrderByLastnameDesc)和 问题解决了 。
Finally i renamed the municipal_id to municipalId (underscore removed) and also rename getters/setters and the Repository (findByMunicipalIdOrderByLastnameDesc) and the issue solved.
我的问题是为什么会发生这种情况?
My question is why this is happening?
推荐答案
下划线 _
是Spring Data查询派生中的保留字符(参见参考文档以获取详细信息)以潜在地允许手动属性路径描述。所以你有两个选择:
The underscore _
is a reserved character in Spring Data query derivation (see the reference docs for details) to potentially allow manual property path description. So there are two options you have:
- 坚持使用 camel-case 用于成员变量名称,一切都将按预期工作。
- 转义
_
使用额外的下划线,即将查询方法重命名为findByMunicipal__idOrderByLastnameDesc(...)
。
- Stick to the Java naming conventions of using camel-case for member variable names and everything will work as expected.
- Escape the
_
by using an additional underscore, i.e. rename your query method tofindByMunicipal__idOrderByLastnameDesc(…)
.
我推荐前者,因为你不会疏远其他Java开发人员:)。
I'd recommend the former as you're not going to alienate fellow Java developers :).
这篇关于Spring-Data-Jpa存储库 - 实体列名称的下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!