简体   繁体   English

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

[英]Spring-Data-Jpa Repository - Underscore on Entity Column Name

I am using spring-data-jpa on a spring webmvc project.我在 spring webmvc 项目上使用 spring-data-jpa。 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.您可以在下面看到我的实体、我的存储库和异常。

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_id as int, then as Integer and the same for the parameter municipal_id on my Repository, but none worked.我尝试将municipal_id设置为 int,然后设置为Integer并且对于我的存储库中的参数municipal_id ,但没有任何效果。 Also, I renamed the Repository to findByMunicipalidOrderByLastnameDesc and findByMunicipalIdOrderByLastnameDesc but it didn't work either.此外,我将存储库重命名为findByMunicipalidOrderByLastnameDescfindByMunicipalIdOrderByLastnameDesc但它也不起作用。

Finally I renamed the municipal_id to municipalId (underscore removed) and also renamed getters/setters and the Repository ( findByMunicipalIdOrderByLastnameDesc ) and the issue was resolved .最后,我改名municipal_idmunicipalId (下划线删除),也改名为getter / setter方法和存储库( findByMunicipalIdOrderByLastnameDesc和问题就解决了

My question is why this is happening?我的问题是为什么会这样?

I solved this error by renaming field to the name without underscore.我通过将字段重命名为不带下划线的名称解决了这个错误。

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

The underscore _ is a reserved character in Spring Data query derivation (see the reference docs for details) to potentially allow manual property path description.下划线_是 Spring Data 查询派生中的保留字符(有关详细信息,请参阅参考文档)可能允许手动属性路径描述。 So there are two options you have:所以你有两个选择:

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

I'd recommend the former as you're not going to alienate fellow Java developers :).我推荐前者,因为你不会疏远其他 Java 开发人员:)。

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

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

I know the question was answered a long time ago, but it can help others in the future.我知道这个问题很久以前就有人回答了,但它可以在未来帮助其他人。

According to the Docs , underscore is a special character used by spring to separate properties names.根据Docs ,下划线是 spring 用于分隔属性名称的特殊字符。 If you really want to stick with snake case notation, you can set nativeQuery to true and solve this problem:如果你真的想坚持使用蛇形大小写符号,你可以将 nativeQuery 设置为 true 并解决这个问题:

@Query(value = "SELECT * FROM municipalperson WHERE municipal_id=?1 ORDER BY last_name DESC", nativeQuery = true)
List<Municipalperson> findByMunicipal_idOrderByLastnameDesc(int municipal_id);

One other approach that worked for me is to use @JsonProperty to differentiate between field name used in REST request/response and that used for database.另一种对我有用的方法是使用@JsonProperty来区分 REST 请求/响应中使用的字段名称和用于数据库的字段名称。 For example:例如:

@JsonProperty("municipalId")
private Integer municipal_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM