16

我正在尝试使用 Hibernate 来持久化一个如下所示的类:

public class Item implements Serializable, Comparable<Item> {

// Item id
private Integer id;

// Description of item in inventory
private String description;

// Number of items described by this inventory item
private int count;

 //Category item belongs to 
private String category;

// Date item was purchased 
private GregorianCalendar purchaseDate;

    public Item() {
}

    public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

    public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}   

public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public GregorianCalendar getPurchaseDate() {
    return purchaseDate;
}

public void setPurchasedate(GregorianCalendar purchaseDate) {
    this.purchaseDate = purchaseDate;
}

我的 Hibernate 映射文件包含以下内容:

<property name="puchaseDate" type="java.util.GregorianCalendar">
  <column name="purchase_date"></column>
</property>

当我尝试运行时,我收到错误消息,表明 purchaseDate 属性没有 getter 函数:

577 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
577 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
577 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
592 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/home_inventory
592 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=****}
1078 [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 5.1.45
1078 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} )
1103 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect
1107 [main] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
1109 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
1110 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
1110 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
1110 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
1110 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
1110 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
1111 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
1111 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
1111 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
1111 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
1111 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
1111 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
1111 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
1111 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
1112 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
1113 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
1113 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
1113 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
1113 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
1113 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
1113 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
1113 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
1114 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
1117 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
1118 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
1118 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
1118 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
1118 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
1118 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
1151 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
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:475)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:295)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
    at service.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:53)
    at service.ItemSvcHibImpl.generateReport(ItemSvcHibImpl.java:78)
    at service.test.ItemSvcTest.testGenerateReport(ItemSvcTest.java:226)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:164)
    at junit.framework.TestCase.runBare(TestCase.java:130)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:107)
    ... 29 more
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for puchaseDate in class domain.Item
    at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:328)
    at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:321)
    at org.hibernate.mapping.Property.getGetter(Property.java:304)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:299)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:158)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:77)
    ... 34 more

我是 Hibernate 的新手,所以我不知道所有的来龙去脉,但我确实有 purchaseDate 属性的 getter 和 setter。我不知道我在这里错过了什么——还有其他人吗?

4

7 回答 7

35

它来过我一次,没有任何错别字。我已将javassist.jar添加到类路径中,它解决了问题。

于 2010-08-19T19:08:43.827 回答
16

在堆栈跟踪的下方,它说:

missing a getter for puchaseDate

您可能想检查拼写错误;)您缺少一个 R 并且 setPurchasedate 应该是 setPurchaseDate

于 2010-04-19T18:10:53.677 回答
5

看起来问题可能出在大写: setPurchasedate()应该是setPurchaseDate()(大写的“D”)。

于 2010-04-19T18:08:19.687 回答
4

如果它可以帮助某人:

就我而言,我的映射文件中有错误。类没有被它们的完整包名引用。我犯了这个错误是因为当我的 bean 类属于默认包时生成了映射(因此没有包名;例如:Order而不是com.mypackage.Order),然后我将 bean 类移动到包(在示例中com.mypackage)。不幸的是,映射文件并没有随着新的包定义而相应改变。

我的提示是重做 Hibernate 逆向工程并查看它产生的结果,并将其与您当前的映射文件进行比较。

于 2010-12-13T10:00:24.797 回答
3

我包括了 java assit.jar 并且它有效

<dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.12.1.GA</version>
</dependency>
于 2014-02-19T05:59:05.930 回答
3

注意方法名,区分大小写

就我而言,它不能很好地识别吸气剂;我的属性名称是uId,我使用getUId名称作为吸气剂名称,当我将其更改为getuId问题解决时!

于 2014-01-12T19:45:32.763 回答
2

我经历了

Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

因为在我之前映射的 POJO 上重命名字段时,我有一个短暂的失误。

我使用可信赖的 IDE 重构热键更改了 Java 属性名称,这也不会更改 getter 和 setter 方法名称。我更改了 getter(因为它的 @Column 注释引起了我的注意,我还需要解决这个问题,因为表列名称正在更改)。

但是,我忽略了更改设置器,这就是错误的原因。

于 2011-03-07T21:06:50.067 回答