首页>Program>source

我在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-11
    1 #

    下划线 _ 是Spring数据查询派生中的保留字符(有关详细信息,请参见参考文档),以可能允许手动描述属性路径.因此,您有两种选择:

    坚持使用Java命名约定,将驼峰式大小写用作成员变量名称,一切都会按预期进行。

    逃脱 _ 通过使用其他下划线,即将查询方法重命名为 findByMunicipal__idOrderByLastnameDesc(…) .

    我建议使用前者,因为您不会疏远其他Java开发人员:)。

  • 2021-1-11
    2 #

    我通过将字段重命名为不带下划线的名称来解决了该错误。

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

  • 2021-1-11
    3 #

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

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

  • C ++ string ::发现复杂性
  • 如何在Perl web搜寻器中处理Javascript?