2013-05-02 59 views
0

我試圖整合Spring和Hibernate但是當我運行的代碼,並提交我的,然後它會拋出這個異常
org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.xgrid.evaltask.Entities.User]; SQL [insert into User (UserName, passwd, ID) values (?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.xgrid.evaltask.Entities.User]
我發現谷歌的原因,但被卡住
請指導我我在哪裏錯
這裏的applicationContext.xml
Spring + Hibernate的confuguration

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jee="http://www.springframework.org/schema/jee" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xsi:schemaLocation="http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

    <context:component-scan base-package="com.xgrid.evaltask"/> 


    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
      p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
      p:url="jdbc:sqlserver://DBURL:1433;databaseName=Test_DB" 
      p:username="sampleUser" 
      p:password="@123456asdfgh" /> 


    <tx:annotation-driven transaction-manager="transactionManager" /> 


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
     p:dataSource-ref="dataSource"  
     p:packagesToScan="com.xgrid.evaltask" 
     /> 

    <context:annotation-config /> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
     p:sessionFactory-ref="sessionFactory" /> 

</beans> 

這裏是hibernate.cfg.xml中
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <!-- Enable this to see the SQL statements in the logs--> <property name="show_sql">true</property> <!-- This will drop our existing database and re-create a new one. Existing data will be deleted! --> <property name="hbm2ddl.auto">create</property> </session-factory> </hibernate-configuration>
這裏是我的DAO類

@Repository("authorizeDao") 
public class AuthorizeDaoImpl implements AuthorizeDao { 

    @Autowired 
    HiberAdd hiberAdd; 

    @Override 
    public boolean doAuthorize(Authentication authentication) { 
     System.out.println("Name: " + authentication.getUserName()); 
     System.out.println("PW: " + authentication.getPassWord()); 
     User user = new User(); 
     user.setId(1212); 
     user.setName(authentication.getUserName()); 
     user.setPasswd(authentication.getPassWord()); 
     hiberAdd.add(user); 
     return true; 
    } 
} 

,這是我的CRUD類

`@Service("hiberAdd") 
@Transactional 
public class HiberAdd { 

    @Resource(name="sessionFactory") 
private SessionFactory sessionFactory; 
    public void add(User user) { 


    // Retrieve session from Hibernate 
    Session session = sessionFactory.getCurrentSession(); 

    // Save 
     session.save(user); 
     System.out.println("Saved ......................... "); 
} 
}` 


這是我的實體類

`@Entity 
@Table(name="User") 
public class User implements Serializable{ 

    @Id 
    @Column(name="ID") 
    private Integer id; 

    @Column(name="UserName") 
    private String name; 

    @Column(name="passwd") 
    private String passwd; 
//getters setters 
+0

什麼是您的用戶表結構? – 2013-05-02 09:00:20

+0

id(int),name(varchar),password(varchar) – Despicable 2013-05-02 09:40:57

+0

你能粘貼更多的錯誤信息嗎? – 2013-05-02 09:43:47

回答

1

USER是你的數據庫的reserved keyword。爲您的表使用另一個名稱。

+0

是downvoter應該說明原因。無論如何,你的意思是我必須改變桌子的名字?好吧,讓我試試:) – Despicable 2013-05-02 09:46:25

+0

是的,你是對的:)生病upvote你:)它現在插入。但請清除我一件事它只是插入數據時,只有當表已創建.Hibernate不創建表,如果它不不存在。爲什麼它發生? – Despicable 2013-05-02 09:56:35

+0

Hibernate是一個ORM。這不是創建表的工作。它*可以*做到這一點,但只有當你設置一個特定的hibernate屬性(hibernate.hbm2ddl.auto)。搜索此屬性的文檔以獲取更多信息。 – 2013-05-02 10:01:40