Spring-Data-Jpa Repository - Entity列名中的下划线
Spring-Data-Jpa Repository - Entity列名中的下划线
我在一个Spring WebMVC项目中使用spring-data-jpa。我在一个实体的Repository中使用查询创建时遇到了问题。下面是我的实体、我的Repository和异常。
我的实体:
@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:
@Repository public interface MunicipalpersonRepository extends JpaRepository{ List 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`,同样也对Repository中的`municipal_id`参数进行了相同的操作,但都没有成功。我还将Repository重命名为`findByMunicipalidOrderByLastnameDesc`和`findByMunicipalIdOrderByLastnameDesc`,但也没有成功。
最后,我将`municipal_id`重命名为`municipalId`(去掉了下划线),还重命名了getter/setter和Repository(`findByMunicipalIdOrderByLastnameDesc`),问题得到了解决。
我的问题是为什么会发生这种情况?