【发布时间】:2011-08-14 02:39:54
【问题描述】:
我正在尝试在 Spring 中使用休眠并使用 HSQL DB。我无法将我的对象持久保存到数据库,因为 Hibernate 不从对象中获取值,而是在生成的 Insert 语句中使用空值。我正在尝试调试该问题一段时间,但已经没有想法了。非常感谢您对此问题的任何帮助。
这是我的代码 sn-ps。
应用程序上下文(applicationContext.xml):我正在上下文中配置我的数据源、会话工厂、DAO 和事务管理器。
<context:component-scan base-package="com.wb.services.fileservice" />
<tx:annotation-driven />
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:com/wb/services/testdb/schema.sql" />
<jdbc:script location="classpath:com/wb/services/testdb/test-data.sql" />
</jdbc:embedded-database>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>/com/wb/services/fileservice/file.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="fileServiceDAO" class="com.wb.services.fileservice.FileServiceDAO">
<constructor-arg ref="sessionFactory" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
我的 Hibernate 映射文件(file.hbm.xml):
<class name="com.wb.services.fileservicemodel.FilePrintHibernate" table="T_FILES">
<id name="fileID" column="File_ID" type="int">
<generator class="native"/>
</id>
<property name="name" column="Name"/>
<property name="text" column="Text"/>
</class>
需要持久化的临时类(FilePrintHibernate):
public class FilePrintHibernate implements Serializable {
private int fileID;
private String name;
private String text;
// Skipping getters and setters for the above.
}
将对象持久化到数据库表的 DAO 类。
@Repository
@Transactional
public class FileServiceDAO {
private SessionFactory sessionFactory;
public FileServiceDAO() {
}
public FileServiceDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
@Transactional(readOnly = false, propagation=Propagation.REQUIRED)
public void findByCreditCard(String filename) {
FilePrintHibernate filePrintHibernate = new FilePrintHibernate();
filePrintHibernate.setFileID(2);
filePrintHibernate.setName("Manual");
filePrintHibernate.setText("Hibernate Test Example");
getCurrentSession().save(filePrintHibernate);
getCurrentSession().flush();
}
}
当应用程序运行并且 DAO 类(上面的代码文件 4)尝试持久化 FilePrintHibernate 对象时,我收到的插入语句是:insert into T_FILES (File_ID, Name, Text) values (null, ?, ?)。请注意它试图插入的值。而不是我设置的值 (2,"Manual", "Hibernate Test Example")。
您能否让我知道我在上面做错了什么。非常感谢上述任何帮助。
问候,
虚无
【问题讨论】: