2012-09-26 64 views
2

我正在學習spring數據訪問,並試圖通過hibernateTempate插入數據。這裏是我的代碼:實體屬性沒有傳遞給帶有hibernateTemplate的sql語句

修訂

的applicationContext.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); 
    } 
} 

Main.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); 
    } 
} 

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; 
    } 
} 

臺式機有三列:中,主機名和ip地址。 mid是主鍵並且是自動增量。

當我運行主,我有以下的輸出:

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驅動程序錯誤,解決方案是使用更高版本的驅動程序,如10.1

+1

到目前爲止,一切看起來都是正確的。你可以展示你的「機器」課程嗎? – axtavt

+0

@axtavt,已更新。 – eric2323223

回答

1

請注意,您已將Hibernate配置爲使用HSQLDB方言,而您的錯誤消息來自DB2。您需要配置與您的DBMS匹配的方言,請參閱3.4.1. SQL Dialects

+0

感謝您的糾正,我已將其更改爲org.hibernate.dialect.DB2Dialect,輸出現在爲:Hibernate:插入MACHINE(MID,HOSTNAME,IPADDRESS)值(默認值???),爲什麼hostname和ipaddress仍然沒有在SQL代碼中設置? – eric2323223

+2

它們已設置。 Hibernate使用JDBC準備好的語句。問號是準備好的陳述的參數。請參閱http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html。如果你想記錄參數的值,你需要設置'org.hibernate.type'的日誌級別爲DEBUG。請參閱http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#configuration-logging –

+0

@JB,感謝您的鏈接。 – eric2323223