【问题标题】:Spring + Hibernate confugurationSpring + Hibernate 配置
【发布时间】:2013-05-02 08:19:11
【问题描述】:

我正在尝试集成 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

<?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

【问题讨论】:

  • 你的用户表结构是什么?
  • id(int),name(varchar),password(varchar)
  • 你能粘贴更多的错误信息吗?
  • 亲,问题开头已经粘贴了

标签: java spring hibernate java-ee-6


【解决方案1】:

USER 是您的数据库的reserved keyword。为您的表使用其他名称。

【讨论】:

  • 是的,downvoter 应该说明原因。无论如何,您的意思是我必须更改表的名称?好的,让我试试:)
  • 是的,你是对的 :) 不赞成你 :) 它现在正在插入。但请澄清一件事 它只是在表已经创建时才插入数据。如果它,Hibernate 不会创建表不存在。为什么会这样?
  • Hibernate 是一个 ORM。它的工作不是创建表格。它可以做到这一点,但前提是您设置了特定的休眠属性 (hibernate.hbm2ddl.auto)。搜索此属性的文档以获取更多信息。
  • 是的,我已经在我的 hibernate.cfg.xml &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt; 中设置了它,正如您在我的问题中看到的那样。但我不知道为什么它不起作用:(
  • 属性是hibernate.hbm2ddl.auto。不是 hbm2ddl.auto。
猜你喜欢
  • 2012-01-23
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 2015-06-25
  • 2012-04-13
相关资源
最近更新 更多