最近在实战SSH项目,遇到一个错误,如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.ztg.elec.dao.impl.ElecTextDaoImpl': Injection of resource methods failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
....
.....
.....//一直纠结于上面的报错信息,忽略了下面错误信息
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for textReamark in class com.ztg.elec.domain.ElecText
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)
... 72 more
其实就是因为自己的粗心,在配置elecText.hbm.xml文件时,
<property name="textReamark" type="string">
<column name="textRemark" sql-type="varchar(500)"></column>
</property>,这里property中的name对应于bean类中的属性textRemark,所以这里应该写textRemark,而不是textReamark,
所以就报错了:其实应该耐心读懂报错信息,读到下面
Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for textReamark in class com.ztg.elec.domain.ElecText
才知道错误,然后修改后的正确配置是:
<property name="textRemark" type="string">
<column name="textRemark" sql-type="varchar(500)"></column>
</property>
总结:
在写hbm.xml配置文件的时候,
<property name="textRemark" type="string">中的name值和bean类属性名应该一致,否则会报以上错误
而<column name="textRemark" sql-type="varchar(500)"></column>中的name值应该和数据库表中的列段名一致,否则在数据库表中会产生一个新列。