user2490003
user2490003

Reputation: 11890

Hibernate: PropertyNotFoundException: Could not find a getter for XXX

I have a model (see below) and Hibernate is complaining it can't find a getter for the field stockId.

Fixing the error is simple enough, but I'm still learning about hibernate and wondering WHY it throws this error? I don't attempt to access the value from anywhere, so I'm assuming hibernate is trying to call it behind the scenes.

Does it look at each field in my model and expect it to have a getter defined?

Or does it see that the variable stockId is private and requires that it have a getter?

Thanks.

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;
  }
}

EDIT: Just fixed the above error and now it's complaining that it can't find a setter either. So it just seems that all tuples/fields require a getter and setter defined?

EDIT: Adding the error as requested

[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]

Upvotes: 2

Views: 2460

Answers (1)

VHS
VHS

Reputation: 10184

Regardless of whether your bean properties are private or public, you need both getters and setters.

Upvotes: 2

Related Questions