Java Spring-Data-Jpa 存储库 - 实体列名称上的下划线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23456197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:05:51  来源:igfitidea点击:

Spring-Data-Jpa Repository - Underscore on Entity Column Name

javajpaspring-datarepositoryspring-data-jpa

提问by George Siggouroglou

I am using spring-data-jpa on a spring webmvc project. I am facing an issue using query creationon a Repository of one of my Entities. Below you can see my Entity, my Repository and the Exception.

我在 spring webmvc 项目上使用 spring-data-jpa。我在我的一个实体的存储库上使用查询创建时遇到问题。您可以在下面看到我的实体、我的存储库和异常。

My Entity:

我的实体:

@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;
    }
}

my Repository:

我的存储库:

@Repository
public interface MunicipalpersonRepository extends JpaRepository<Municipalperson, Integer> {

    List<Municipalperson> findByMunicipal_idOrderByLastnameDesc(int municipal_id);
}

and the exception,

和例外,

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!  

I tried to set municipal_idas int, then as Integerand the same for the parameter municipal_idon my Repository, but none worked. Also, I renamed the Repository to findByMunicipalidOrderByLastnameDescand findByMunicipalIdOrderByLastnameDescbut it didn't work either.

我尝试将存储库上的参数设置municipal_id为 int,然后设置为Integer相同municipal_id,但没有任何效果。另外,我将 Repository 重命名为findByMunicipalidOrderByLastnameDescandfindByMunicipalIdOrderByLastnameDesc但它也不起作用。

Finally I renamedthe municipal_idto municipalId(underscore removed) and also renamed getters/setters and the Repository (findByMunicipalIdOrderByLastnameDesc) and the issue was resolved.

最后,我重命名municipal_idmunicipalId(删除了下划线)并重命名了 getter/setter 和 Repository ( findByMunicipalIdOrderByLastnameDesc),问题就解决了

My question is why this is happening?

我的问题是为什么会这样?

采纳答案by Oliver Drotbohm

The underscore _is a reserved character in Spring Data query derivation (see the reference docsfor details) to potentially allow manual property path description. So there are two options you have:

下划线_是 Spring Data 查询派生中的保留字符(有关详细信息,请参阅参考文档)可能允许手动属性路径描述。所以你有两个选择:

  1. Stick to the Java naming conventions of using camel-casefor member variable names and everything will work as expected.
  2. Escape the _by using an additional underscore, i.e. rename your query method to findByMunicipal__idOrderByLastnameDesc(…).
  1. 坚持对成员变量名称使用驼峰命名的 Java 命名约定,一切都会按预期工作。
  2. _使用额外的下划线转义,即将您的查询方法重命名为findByMunicipal__idOrderByLastnameDesc(…).

I'd recommend the former as you're not going to alienate fellow Java developers :).

我推荐前者,因为您不会疏远其他 Java 开发人员:)。

回答by Valentyn Kolesnikov

I solved this error by renaming field to the name without underscore.

我通过将字段重命名为不带下划线的名称解决了这个错误。

@Column(name = "municipal_id", nullable = false)
private Integer municipalId; // <-- field was renamed

回答by Jagannath Nanda

Please add the following properties to application.propertiesfile:

请将以下属性添加到application.properties文件:

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy