Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity attribute not passed to sql statement with hibernateTemplate
    primarykey
    data
    text
    <p>I am learning spring data access and I've tried to insert data through hibernateTempate. Here is my code:</p> <p><strong>UPDATED</strong></p> <p>applicationContext.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt; &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.eric.mvnlab"/&gt; &lt;context:property-placeholder properties-ref="properties"/&gt; &lt;bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;classpath:application.properties&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt; &lt;property name="basename" value="classpath:messages"/&gt; &lt;property name="defaultEncoding" value="${source.encoding}"/&gt; &lt;/bean&gt; &lt;!-- DAO layer --&gt; &lt;tx:annotation-driven/&gt; &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt; &lt;property name="sessionFactory" ref="sessionFactory"/&gt; &lt;/bean&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt; ... &lt;/bean&gt; &lt;!-- Hibernate session factory --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource"/&gt; &lt;!--&lt;property name="packagesToScan" value="com.eric.mvnlab.model.*" /&gt;--&gt; &lt;property name="annotatedClasses"&gt; &lt;list&gt; &lt;value&gt;com.eric.mvnlab.model.Machine&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="hibernateProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.HSQLDialect&lt;/prop&gt; &lt;prop key="hibernate.hbm2ddl.auto"&gt;create&lt;/prop&gt; &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>MachineDAO.java</p> <pre><code>package com.eric.mvnlab.server.dao.impl; import com.eric.mvnlab.model.Machine; import org.springframework.stereotype.Repository; @Repository("machineDao") public class MachineDAO extends GenericHibernateDAOImpl&lt;Machine, Integer&gt; { public void addMachine(Machine machine) { getHibernateTemplate().save(machine); } public Machine findById(int id) { return (Machine) getHibernateTemplate().get(Machine.class, id); } } </code></pre> <p>Main.java</p> <pre><code>package com.eric.mvnlab; import com.eric.mvnlab.model.Machine; import com.eric.mvnlab.server.dao.impl.MachineDAO; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created with IntelliJ IDEA. * User: eric * Date: 9/25/12 * Time: 3:15 PM * To change this template use File | Settings | File Templates. */ public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); MachineDAO machineDAO = (MachineDAO)context.getBean("machineDao"); machine.setHostname("MyLaptop"); machine.setIpaddress("127.0.0.1"); machineDAO.addMachine(machine); } } </code></pre> <p>Machine.java</p> <pre><code>package com.eric.mvnlab.model; import javax.persistence.Table; import javax.persistence.Id; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @Entity @Table(name="MACHINE") public class Machine { @Column(name="MID") @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int mid; @Column(name="HOSTNAME") private String hostname; @Column(name="IPADDRESS") private String ipaddress; public String getIpaddress() { return ipaddress; } public void setIpaddress(String ipaddress) { this.ipaddress = ipaddress; } public String getHostname() { return hostname; } public void setHostname(String hostname) { this.hostname = hostname; } public int getMid() { return mid; } public void setMid(int machineId) { this.mid = machineId; } } </code></pre> <p>Table Machine has three columns: mid, hostname and ipaddress. mid is the primary key and is auto increment. </p> <p>When I run Main, I got below output:</p> <pre><code>Hibernate: insert into MACHINE (MID, HOSTNAME, IPADDRESS) values (null, ?, ?) Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.eric.mvnlab.model.Machine]; SQL [insert into MACHINE (MID, HOSTNAME, IPADDRESS) values (null, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.eric.mvnlab.model.Machine] WARN - JDBCExceptionReporter - SQL Error: -798, SQLState: 428C9 at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629) ERROR - JDBCExceptionReporter - DB2 SQL Error: SQLCODE=-798, SQLSTATE=428C9, SQLERRMC=MID, DRIVER=4.7.85 </code></pre> <p>Could anyone tell me why the entity attribute data is not passed to sql statement?</p> <p><strong>NOTE:</strong> If you are using DB2 9.7, and you are likely to met this error:</p> <p><em>org.hibernate.HibernateException: The database returned no natively generated identity value</em></p> <p>It's a DB2 jdbc driver bug, and the solution is to use a later version driver, such as 10.1</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload