开发者

Hibernate inserts null values to table when used with Spring

I am trying to use hibernate in Spring and using HSQL DB. I am not able to persist my objects to database since Hibernate does not take the values from object and uses null values instead in the generated Insert statements. I am trying to debug the issue for a while now and have run out of ideas. Any help on the issue is greatly appreciated.

Here are my code snippets.

Application Context (applicationContext.xml): I am configuring my data Source, session Factory, DAO and transaction Manager in the context.

<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>
           开发者_JAVA百科 <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>

My Hibernate Mapping file (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>

The transient class that needs to be persisted (FilePrintHibernate ):

public class FilePrintHibernate implements Serializable {

    private int fileID;
    private String name;
    private String text;

// Skipping getters and setters for the above.

}

DAO class that will be persisting the objects to DB table.

@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();
    }
}

When the app is run and the DAO class (Code file 4 above) tries to persist the FilePrintHibernate object, the insert statement I receive is: insert into T_FILES (File_ID, Name, Text) values (null, ?, ?). Please note the values it is trying to insert. rather than the values that I set (2,"Manual", "Hibernate Test Example").

Can you please let me know what I am doing wrong above. Any help of the above is really appreciated.

Regards,

Nihil


I believe this is because you have a native generator set for your id field, and so hibernate decides to leave the choice for it to the database. Try running the query manually and see what id will be set. If it is the correct one, file a bug in hibernate (after upgrading to the latest version).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜