我有一个简单的SpringHibernate应用程序,我正在使用self join创建Categories。它适用于4个类别,但是当我输入5个类别时它会挂起,Transaction需要花费太多时间,请帮帮我 以下是我的代码
DaoImpl
public void addCategory(Category cat) {
// TODO Auto-generated method stub
Session session = this.sessionFactory.openSession();
Transaction trx = session.beginTransaction();
Long pid = cat.getParentCategory();
if (pid != null) {
Category parentCat = (Category) session.get(Category.class, pid);
cat.setCategory(parentCat);
}
session.save(cat);
trx.commit();
System.out.println("done");
}
@Override
public List getCategory() {
Session session = this.sessionFactory.openSession();
List<Category> categoryList = session.createQuery("from Category").list();
return categoryList;
}
hbm文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD //EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="amit.com.model">
<class name="Category" table="Category">
<id name="primaryKey" column="primaryKey">
<generator class="native"/>
</id>
<property name="name" column="NAME"></property>
<!-- <property name="parentId" column="PARENT_ID" /> -->
<many-to-one name="category" class="amit.com.model.Category" column="PARENT_ID" not-null="false"/>
<!-- <set name="categories" table="Category" lazy="false" cascade="all-delete-orphan" inverse="false">
<key column="parentId"/>
<one-to-many class="amit.com.model.Category" />
</set>
-->
</class>
</hibernate-mapping>
servlet的context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Welcome page -->
<mvc:view-controller path="/" view-name="home"/>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="amit.com" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>WEB-INF/spring/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.databaseurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/river"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean> -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- <bean id="userDao" class="net.codejava.spring.dao.UserDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean> -->
</beans>
由于
答案 0 :(得分:0)
您应该正确使用交易和会话。在异常上回滚事务,在finally块中关闭会话。您可以在此处查看使用会话的简单方法fluent-hibernate(类com.github.fluent.hibernate.HibernateSessionFactory)。可以在fluent-hibernate-mysql
找到使用MySQL和Hibernate 5的简单控制台应用程序当然,您可以使用DAO模式将具有会话和事务的工作委派给Spring。