v 实做ContentField
Ø 手工生成ContentField.java POJO
Ø 运用AppGen工具自动生成代码,具体原理请参见ReadAppFuse系列
Ant install -Dmodel.name=ContentField -Dmodel.name.lowercase=contentfield
在这里我将AppGen提供的install和install-detailed任务合并为了一个,以便使用方
便,合并很简单:
<targetname="install"depends="install-src-generic,install-src-detailed,install-other"/〉
1)运行DAO测试
ant test-dao -Dtestcase=ContentFieldDAO
© 运行报错
test-dao:
[echo] Testing dao...
[junit] Testsuite: org.dudu.dao.ContentFieldDAOTest
[junit] Tests run: 5, Failures: 1, Errors: 4, Time elapsed: 1.412 sec
[junit] ------------- Standard Output ---------------
[junit] Testcase: testAddContentField(org.dudu.dao.ContentFieldDAOTest):
Caused an ERROR
[junit] Unknown entity: org.dudu.model.ContentField; nested exception is org
.hibernate.MappingException: Unknown entity: org.dudu.model.ContentField
© 错误分析
错误提示为找不到org.dudu.model.ContentField类
© 错误原因
AppGen生成的配置文件applicationContext-hibernate.xml
没有为SessionFactory添加POJO映射配置
© 解决方法
手工添加配置信息
<beans>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="mappingResources">
<list>
<value>org/dudu/model/Role.hbm.xml</value>
<value>org/dudu/model/User.hbm.xml</value>
<value>org/dudu/model/UserCookie.hbm.xml</value>
<value>org/dudu/model/ContentField.hbm.xml</value>
<value>org/dudu/model/ContentFieldValue.hbm.xml</value>
<value>org/dudu/model/ContentLocale.hbm.xml</value>
<value>org/dudu/model/Localizable.hbm.xml</value>
<value>org/dudu/model/ResourceData.hbm.xml</value>
</list>
… …
</beans>
2)再次运行DAO测试
ant test-dao -Dtestcase=ContentFieldDAO
© 运行报错
test-dao:
[echo] Testing dao...
[junit] Testsuite: org.dudu.dao.ContentFieldDAOTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Null Test: Caused an ERROR
[junit] null
[junit] java.lang.ExceptionInInitializerError
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(Class.java:164)
[junit] Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in file [E:/dudu-src/buil
d/dao/gen/META-INF/applicationContext-hibernate.xml]: Initialization of bean fai
led; nested exception is org.hibernate.MappingException: Association references
unmapped class: org.dudu.model.ContentDocument
© 错误原因
不能映射到org.dudu.model.ContentDocument
从代码中可知ContentDocument继承于ContentResource,hibernate工具生成
映射文件时,不会为子类产生映射配置文件, 所以我们在这应该将
ContentDocument父类的映射文件ContentResource添加进配置文件。
© 解决方法
在配置文件applicationContext-hibernate.xml中手工添加
<value>org/dudu/model/ContentResource.hbm.xml</value>
3)再次运行DAO测试
ant test-dao -Dtestcase=ContentFieldDAO
© 运行报错
[junit] Testcase:
testGetContentField(org.dudu.dao.ContentFieldDAOTest):
Caused an ERROR
[junit] Cannot instantiate abstract class or interface:
org.dudu.model.Page;
nested exception is org.hibernate.InstantiationException: Cannot
instantiate abstract class or interface: org.dudu.model.Page
© 错误原因
ContentField对localizable有引用,localizable加载时会初始化
它的子类Page,而Page是抽象类。
© 解决方法
将Page抽象类abstract关键字去掉。