我在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!
我试图将mynicipal_id设置为int,然后将其设置为Integer,并将存储库中的参数'municipal_id'设置为相同,但是它们都没有起作用.另外,我将存储库重命名为" findByMunicipalidOrderByLastnameDesc"和" findByMunicipalIdOrderByLastnameDesc",但两者均无效.
最后我 renamed 将Municipal_id更改为MunicipalId(删除下划线),还重命名getters / setters和存储库(findByMunicipalIdOrderByLastnameDesc)和 the issue solved .
我的问题是为什么会这样?
最新回答
- 2021-1-111 #
- 2021-1-112 #
我通过将字段重命名为不带下划线的名称来解决了该错误。
@Column(name = "municipal_id", nullable = false) private Integer municipalId; // <-- field was renamed
- 2021-1-113 #
请将以下属性添加到 application.properties 文件:
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
相关问题
- java:JAXB将循环引用映射到XMLjavajpaxmlserializationjaxb2021-01-11 22:25
- java:Hibernate / JPA-注释bean方法与字段javahibernateormjpa2021-01-11 11:28
- java:注释以过滤@OneToMany关联的结果javahibernatejpajointable2021-01-11 07:26
下划线
_
是Spring数据查询派生中的保留字符(有关详细信息,请参见参考文档),以可能允许手动描述属性路径.因此,您有两种选择:坚持使用Java命名约定,将驼峰式大小写用作成员变量名称,一切都会按预期进行。
逃脱
_
通过使用其他下划线,即将查询方法重命名为findByMunicipal__idOrderByLastnameDesc(…)
.我建议使用前者,因为您不会疏远其他Java开发人员:)。