1
回答
求教一个spring hibernate jpa配置的问题

我的所有java代码都在net包下,我刚开始学spring jpa的配置,谁能帮忙看下下面问题出在哪里,或是给个配置的例子供学习下,纠结好几天了,先写过了

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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-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/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	
	<!-- 默认扫描的包路径 -->  
    <context:component-scan base-package="net" />
    <!-- 添加注解驱动 -->  
    <mvc:annotation-driven />  
	
	<bean id="dataSource" lazy-init="true" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/data" /> 
		<property name="user" value="root" /> 
		<property name="password" value="" />
		<property name="maxPoolSize">
			<value>4</value>
		</property>
		<property name="maxIdleTime">
			<value>0</value>
		</property>
		<property name="acquireRetryDelay">
			<value>3000</value>
		</property>
	</bean>
	
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">   
        <property name="dataSource" ref="dataSource"/>  
        <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>  
        <property name="packagesToScan" value="net"/><!--待扫描的实体类包,不再需要persistence.xml了-->  
        <property name="jpaProperties">  
            <props>  
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
            </props>  
        </property>  
    </bean>  
      
    <!--指定实现JPA的适配器-->  
    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
        <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />  
    </bean>  
      
    <!-- Jpa 事务配置 -->  
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
        <property name="entityManagerFactory" ref="entityManagerFactory"/>  
    </bean>  
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
	
	<bean id="HelloExecute" class="net.dao.HelloExecute">
		<!-- <property name="sessionFactory" ref="sessionFactory"></property> -->
		<property name="entityManagerFactory" ref="entityManagerFactory"></property>
	</bean>
	
	<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>
</beans>



HelloExecute.java代码如下
package net.dao;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;

import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.stereotype.Repository;

/**
 * @璇存槑锛�
 * @author: gaoll
 * @CreateTime:2013-12-23
 * @ModifyTime:2013-12-23
 */
@Repository
public class HelloExecute extends JpaDaoSupport{
	/*private SessionFactory sessionFactory;*/
	@Resource(name = "entityManagerFactory")
	public void overEntityManager(EntityManagerFactory entityManagerFactory) {
		super.setJpaTemplate(createJpaTemplate(entityManagerFactory));
	}
}



执行request请求的代码如下:
package net.controller;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.dao.HelloExecute;
import net.domain.Member;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


/**
 * @璇存槑锛�
 * @author: gaoll
 * @CreateTime:2013-12-23
 * @ModifyTime:2013-12-23
 */
@Controller
public class HelloWord {
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbc;
	
	@Resource(name="HelloExecute")
	private HelloExecute daoExcut;
	
	@RequestMapping(value = "/index.html",method = RequestMethod.GET)
	public String test(HttpServletRequest request,HttpServletResponse response,Model model){
		Member member = new Member();
		member.setId(2);
		member.setName("bbbbbbbbbbbbbbbbbbb");
		member.setPwd("2222222");
		daoExcut.getJpaTemplate().persist(member);
		return "/html/index";
	}
}



dao  Member如下:
package net.domain;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @说明:
 * @author: gaoll
 * @CreateTime:2013-12-23
 * @ModifyTime:2013-12-23
 */
@Entity
@Table(name="biz_member")
public class Member implements Serializable{
	private long id;
	private String name;
	private String pwd;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}



编译过程没有错误了,但是在执行请求是就会出如下错误:
org.hibernate.PersistentObjectException: detached entity passed to persist: net.domain.Member
	at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)
	at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
	at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:843)
	at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:818)
	at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:822)
	at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:875)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
	at $Proxy42.persist(Unknown Source)
	at org.springframework.orm.jpa.JpaTemplate$5.doInJpa(JpaTemplate.java:268)
	at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:187)
	at org.springframework.orm.jpa.JpaTemplate.persist(JpaTemplate.java:266)
	at net.controller.HelloWord.test(HelloWord.java:41)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:722)


举报
顶部