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

时间:2014-05-04 12:25:58

标签: java jpa spring-data repository spring-data-jpa

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

我的实体,

@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',但它既没有效果。

最后我重命名将city_id更改为municipalId(删除了下划线),并重命名getter / setter和Repository(findByMunicipalIdOrderByLastnameDesc)并 解决问题

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

5 个答案:

答案 0 :(得分:26)

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

  1. 坚持使用camel-case作为成员变量名称的Java命名约定,一切都会按预期工作。
  2. 使用额外的下划线退出_,即将查询方法重命名为findByMunicipal__idOrderByLastnameDesc(…)
  3. 我推荐前者,因为你不会疏远其他Java开发人员:)。

答案 1 :(得分:20)

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

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

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

我知道这个问题很久以前就有人回答了,但它可以在未来帮助其他人。

根据Docs,下划线是spring用来分隔属性名称的特殊字符。如果你真的想坚持使用snake case notation,你可以将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);

答案 4 :(得分:0)

另一种对我有用的方法是使用 @JsonProperty 来区分 REST 请求/响应中使用的字段名称和用于数据库的字段名称。 例如:

@JsonProperty("municipalId")
private Integer municipal_id;
相关问题