我在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; } }
我的资料库:
@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!
我试图将municipal_idIntory上Integer的参数设置为int,然后设置为和municipal_id,但是都没有设置。另外,我将存储库重命名为findByMunicipalidOrderByLastnameDesc,findByMunicipalIdOrderByLastnameDesc但也没有用。
municipal_id
Integer
findByMunicipalidOrderByLastnameDesc
findByMunicipalIdOrderByLastnameDesc
最后,我 改名 了municipal_id以municipalId(下划线删除),也改名为getter / setter方法和存储库(findByMunicipalIdOrderByLastnameDesc),并 在问题得到解决 。
municipalId
我的问题是为什么会这样?
下划线_是Spring Data查询派生中的保留字符(有关详细信息,请参见参考文档),以潜在地允许手动描述属性路径。因此,您有两种选择:
_
findByMunicipal__idOrderByLastnameDesc(…)
我建议使用前者,因为您不会疏远其他Java开发人员:)。