Java Hibernate - PropertyNotFoundException:找不到

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/921239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:05:49  来源:igfitidea点击:

Hibernate - PropertyNotFoundException: Could not find a getter for

javahibernate

提问by Ben Noland

I have a class that looks like the following:

我有一个如下所示的类:

public class MyClass {
    private String dPart1;

    public String getDPart1() {
        return dPart1;
    }

    public void setDPart1(String dPart1) {
        this.dPart1 = dPart1;
    }
}

My hibernate mapping file maps the property as follows:

我的休眠映射文件映射属性如下:

<property name="dPart1" not-null="true"/>

I get the following error:

我收到以下错误:

org.hibernate.PropertyNotFoundException: Could not find a getter for dPart1 in class com.mypackage.MyClass
        at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282)
        at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:275)
        at org.hibernate.mapping.Property.getGetter(Property.java:272)
        at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:247)
        at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:125)
        at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
        at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56)
        at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:302)
        at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434)
        at 

It appears that hibernate doesn't like my capitalization. How should I fix this?

看来 hibernate 不喜欢我的大写。我应该如何解决这个问题?

采纳答案by Fortega

<property name="DPart1" not-null="true"/>

should work...

应该管用...

回答by dfa

for a property called "dPart1" a hibernate will try a getter named "getDpart1" not "getDPart1" IIRC

对于名为“dPart1”的属性,休眠将尝试名为“getDpart1”而不是“getDPart1”IIRC的getter

回答by Rune Sundling

Can't you just access it like a field?

你不能像访问字段一样访问它吗?

access="field"

访问=“字段”

回答by Rune Sundling

I got the solution

我得到了解决方案

Please make dPart1 to dpart1 and change the getter and setter again..

请将 dPart1 设为 dpart1 并再次更改 getter 和 setter。

It is working for me now.

它现在对我有用。

Remember to change the xml also.

记住也要更改xml。

回答by JuserNt

private String rptausu;

public String getRptausu() {
    return rptausu;
}

public void setRptausu(String rpta) {
    rptausu = rpta;
}

mapping:

映射:

        <property name="prtausu" />

works correctly

工作正常

回答by Mike Demenok

From what I've seen, Hibernate (at least version 3.2.4) will expect a property like dPart to have a getter named getdPart: d stays lowercase. Look at dfa's answer as well - I am guessing that other versions might expect getDpart instead.

从我所见,Hibernate(至少版本 3.2.4)会期望像 dPart 这样的属性有一个名为 getdPart 的 getter:d 保持小写。也看看 dfa 的答案 - 我猜其他版本可能会期望 getDpart 代替。

回答by Sayantan

The setter & getter should look like this

setter 和 getter 应该是这样的

getdPart1()
setdPart1(....)

That's how the setters & getters are generated if generated through an IDE like eclipse.

如果通过 Eclipse 之类的 IDE 生成,那么 setter 和 getter 就是这样生成的。

回答by Prasanna Kumar

For a property private Integer carId;

对于私有的 Integer carId 属性;

the setters and getters should be

setter 和 getter 应该是

getCarId() setCarId(Integer carId)

getCarId() setCarId(Integer carId)

回答by askinss

The naming convention of the property matters example in my own case I initially used

在我最初使用的我自己的案例中,属性的命名约定很重要

private String newimsi, getNewImsi();

the above failed with same exception

以上失败,但有同样的例外

propertynotfoundexception

属性未找到异常

until I corrected to below before it worked

直到我在它起作用之前更正到下面

getNewimsi();

回答by Piyush Upadhyay

Best Practice is not to create getters/setters by our self but use the Eclipse Shortcut(Alt+Shift+S) to create the same for the variables defined in a bean/pojo.

最佳实践不是我们自己创建 getter/setter,而是使用 Eclipse Shortcut(Alt+Shift+S) 为 bean/pojo 中定义的变量创建相同的变量。

Naming convention does matters.

命名约定很重要。