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!
我尝试将 municipal_id
设置为 int,然后设置为 Integer
并且对于我的存储库中的参数 municipal_id
也是如此,但没有任何效果.此外,我将 Repository 重命名为 findByMunicipalidOrderByLastnameDesc
和 findByMunicipalIdOrderByLastnameDesc
但它也不起作用.
I tried to set municipal_id
as int, then as Integer
and the same for the parameter municipal_id
on my Repository, but none worked. Also, I renamed the Repository to findByMunicipalidOrderByLastnameDesc
and findByMunicipalIdOrderByLastnameDesc
but it didn't work either.
最后,我将municipal_id
municipal_id
重命名为municipalId
(删除了下划线)并重命名了getter/setter 和Repository(findByMunicipalIdOrderByLastnameDesc
) 并且问题已解决.
Finally I renamed the municipal_id
to municipalId
(underscore removed) and also renamed getters/setters and the Repository (findByMunicipalIdOrderByLastnameDesc
) and the issue was resolved.
我的问题是为什么会这样?
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 作为成员变量名称的 Java 命名约定,一切都会按预期工作.
- 使用额外的下划线对
_
进行转义,即将您的查询方法重命名为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屋!