Hibernate:PropertyNotFoundException:找不到XXX的getter

时间:2017-01-20 04:05:08

标签: java hibernate

我有一个模型(见下文),Hibernate抱怨它无法为该字段stockId找到一个吸气剂。

修复错误很简单,但是我还在学习hibernate并想知道为什么会抛出这个错误?我不想尝试从任何地方访问该值,因此我假设hibernate试图在后台调用它。

它是否会查看模型中的每个字段并期望它定义了一个getter?

或者它是否看到变量stockId是私有的并且要求它有一个getter?

感谢。

package com.mkyong.stock.model;

import java.io.Serializable;

public class Stock implements Serializable {

  private static final long serialVersionUID = 1L;

  private Long stockId;
  private String stockCode;
  private String stockName;

  public void setStockCode(String stockCode) {
    this.stockCode = stockCode;
  }

  public void setStockName(String stockName) {
    this.stockName = stockName;
  }
}

编辑:刚刚解决了上述错误,现在它抱怨它无法找到设置器。所以似乎所有元组/字段都需要定义getter和setter?

编辑:按要求添加错误

[WARNING]
java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:497)
  at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:294)
  at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [database/Hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
  at java.security.AccessController.doPrivileged(Native Method)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
  at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
  at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
  at com.mkyong.common.App.main(App.java:13)
  ... 6 more
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
  at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:110)
  at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:135)
  at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
  at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:323)
  at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:456)
  at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:131)
  at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
  at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:267)
  at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
  at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:814)
  at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:732)
  at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
  ... 21 more
Caused by: java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
  at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:107)
  ... 34 more
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for stockCode in class com.mkyong.stock.model.Stock
  at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:306)
  at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:299)
  at org.hibernate.mapping.Property.getGetter(Property.java:294)
  at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:300)
  at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:141)
  at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:78)
  ... 39 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.308 s
[INFO] Finished at: 2017-01-19T20:07:49-08:00
[INFO] Final Memory: 13M/298M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:java (default-cli) on project HelloWorld: An exception occured while executing the Java class. null: InvocationTargetException: Error creating bean with name 'sessionFactory' defined in class path resource [database/Hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]: Could not find a getter for stockCode in class com.mkyong.stock.model.Stock -> [Help 1]

1 个答案:

答案 0 :(得分:2)

无论您的bean属性是私有还是公共属性,都需要getter和setter。