Hibernate会生成比平常更多的查询

时间:2015-02-03 08:19:25

标签: java mysql spring hibernate

我正在开发一个小型的spring + hibernate web项目。以下是它的一些片段

的applicationContext.xml

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

<context:component-scan base-package="com.catb">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/catb_db" />
    <property name="user" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.catb.model.User</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="userDAO" class="com.catb.dao.impl.UserDAOImpl">
    <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>

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

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>

SpringDispatcher-servlet.xml中

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

<context:component-scan base-package="com.catb.web" />
<mvc:annotation-driven />

<mvc:resources mapping="/resources/**" location="/resources/" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

User.java - 这是映射类

@SuppressWarnings("serial")
@Entity
@Table(name = "user")
public class User implements Serializable {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "id", unique = true, nullable = false)
 private Integer id;

 @Column(name = "username", nullable = false)
 private String username;

 @Column(name = "password", nullable = false)
 private String password;
}

UserDAO类

@Repository
public class UserDAOImpl implements UserDAO {

  private SessionFactory sessionFactory;

  public void setSessionFactory(SessionFactory sessionFactory) {
      this.sessionFactory = sessionFactory;
  }


  @Override
  public boolean addUser(User user) {
      Session session = sessionFactory.getCurrentSession();
      session.save(user);

      return true;
  }
}

和UserBO类

@Override
@Transactional
public void addUser(User user) {
    userDAO.addUser(user);  
}

当我尝试在控制器中使用userBO.addUser(用户)插入单个用户时,我间歇性地发现了此错误

org.hibernate.exception.ConstraintViolationException: could not execute statement
org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
...
caused by
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '60' for key 'username'
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

我知道在唯一字段(用户名)中插入具有重复值的新行会导致此错误。但是当我试图通过showbase的Hibernate进行调试时,它打印出2行而不是只有一行(我只插入了一个用户)

Hibernate: insert into user (id, password, username) values (?, ?, ?)
Hibernate: insert into user (id, password, username) values (?, ?, ?)

显然,第二次插入导致异常,因为它添加了与第一次

相同的值

任何人都可以告诉我为什么会遇到此错误以及如何解决此问题

提前致谢

2 个答案:

答案 0 :(得分:1)

尝试在addUser服务方法中添加日志,因为看起来服务被调用了两次。

您确定不会触发从浏览器保存来电吗?检查浏览器开发人员工具中的网络选项卡。

使用当前配置,Hibernate没有其他解释来发布到INSERTS。

答案 1 :(得分:1)

您确定发布了所有相关代码吗?日志中提到了position_id,但您没有发布与position相关的任何内容,可能存在一些映射问题。当然,您可以将User实体真正剥离到您发布的字段(用户名和密码),看看它是否仍会导致异常。