【问题标题】:Hibernate inserts null values to table when used with Spring与 Spring 一起使用时,Hibernate 将空值插入到表中
【发布时间】: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")。

您能否让我知道我在上面做错了什么。非常感谢上述任何帮助。

问候,

虚无

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    我相信这是因为您的 id 字段有一个本地生成器集,因此 hibernate 决定将选择权留给数据库。尝试手动运行查询,看看将设置什么 id。如果是正确的,请在 hibernate 中提交一个错误(升级到最新版本后)。

    【讨论】:

    • 感谢博卓的回复。我已经想通了这个问题。似乎 HSQL 创建的表没有声明主键。该列被创建为唯一但不是主键。我昨天生病了,无法更新帖子。我会将其更新为已关闭。
    猜你喜欢
    • 2014-01-24
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2015-12-26
    • 2014-04-06
    • 1970-01-01
    相关资源
    最近更新 更多