我正在学习 Spring 数据访问,并尝试通过 hibernateTempate 插入数据。这是我的代码:
更新
应用程序上下文.xml:
<?xml version="1.0" encoding="UTF-8"?> <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">
<context:annotation-config/>
<context:component-scan base-package="com.eric.mvnlab"/>
<context:property-placeholder properties-ref="properties"/>
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="defaultEncoding" value="${source.encoding}"/>
</bean>
<!-- DAO layer -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
...
</bean>
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="packagesToScan" value="com.eric.mvnlab.model.*" />-->
<property name="annotatedClasses">
<list>
<value>com.eric.mvnlab.model.Machine</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
MachineDAO.java
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<Machine, Integer> {
public void addMachine(Machine machine) {
getHibernateTemplate().save(machine);
}
public Machine findById(int id) {
return (Machine) getHibernateTemplate().get(Machine.class, id);
}
}
主.java
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);
}
}
机器.java
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;
}
}
Table Machine 有三列:mid、hostname 和 ipaddress。mid 是主键,是自动递增的。
当我运行 Main 时,我得到以下输出:
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
谁能告诉我为什么实体属性数据没有传递给sql语句?
注意: 如果您使用的是 DB2 9.7,并且您可能会遇到此错误:
org.hibernate.HibernateException:数据库没有返回本地生成的标识值
是DB2 jdbc驱动的bug,解决方法是使用更高版本的驱动,比如10.1