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

Geo*_*lou 22 java jpa repository spring-data spring-data-jpa

我在spring webmvc项目中使用spring-data-jpa.我在我的一个实体的存储库上使用查询创建时遇到了问题.您可以在下面看到我的实体,我的存储库和例外.

我的实体,

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的存储库,

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

    List<Municipalperson> findByMunicipal_idOrderByLastnameDesc(int municipal_id);
}
Run Code Online (Sandbox Code Playgroud)

和例外,

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!  
Run Code Online (Sandbox Code Playgroud)

我试图将mynicipal_id设置为int,然后设置为Integer,并将其设置为我的Repository中的参数'municipal_id',但它们都没有用.此外,我将存储库重命名为'findByMunicipalidOrderByLastnameDesc'和'findByMunicipalIdOrderByLastnameDesc',但它既没有效果.

最后我改名的municipal_id到municipalId(下划线删除),并重新命名getter/setter方法和存储库(findByMunicipalIdOrderByLastnameDesc)和问题解决.

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

Val*_*kov 28

我通过将字段重命名为没有下划线的名称来解决此错误.

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

  • 这确实是处理这种情况的最佳方式,应该是公认的答案。我意识到这个答案是在问题和第一个答案之后几年出现的,但是我发表这个评论是为了引起未来访问者对这个答案的注意。以这种方式解决允许您使用以您无法控制的方式命名字段的数据库,而不会偏离整个 Java 开发人员领域普遍接受的编码标准,例如,将下划线完全排除在您的方法和变量名称之外。 (2认同)
  • 这个答案应该被标记为正确的,一个很小的努力就可以克服问题。 (2认同)

Oli*_*ohm 26

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

  1. 坚持使用camel-case作为成员变量名的Java命名约定,一切都将按预期工作.
  2. _通过使用额外的下划线来逃避,即将查询方法重命名为findByMunicipal__idOrderByLastnameDesc(…).

我推荐前者,因为你不会疏远Java开发人员:).

  • 这不起作用.双下划线不会改变任何东西.Java JPA对象中的字段只是遵循数据库中显然不遵循Java约定的字段的名称.所以这必须是一个非常常见的用例. (11认同)
  • 双下划线解决方案对我也不起作用,我无法撤消我的赞成票...... (3认同)
  • 对不起,但双下划线不起作用. (2认同)

小智 7

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

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
Run Code Online (Sandbox Code Playgroud)