更新: 错误已通过注释
中的解决方案修复我在尝试向"电子商务"添加新行时遇到此错误表: 当我打电话给" this.ecommerceService.add(ecommerceEntity);"在主控制器中。
错误日志:
SEVERE: Servlet.service() for servlet [searcher] in context with path [/Searcher] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.searcher.entity.EcommerceEntity] with root cause
org.hibernate.PersistentObjectException: detached entity passed to persist: com.searcher.entity.EcommerceEntity
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:756)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:736)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:741)
at com.searcher.dao.EcommerceImp.add(EcommerceImp.java:29)
at com.searcher.service.EcommerceServiceImp.add(EcommerceServiceImp.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy33.add(Unknown Source)
at com.searcher.controller.MainSearcherController.helloWorld(MainSearcherController.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
EcommerceEntity:
package com.searcher.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="ecommerce")
public class EcommerceEntity {
@Id
@Column(name="Id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int Id;
@Column
private String Name;
@Column
private String Path;
public int getId() {
return Id;
}
public void setId(int id) {
this.Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public String getPath() {
return Path;
}
public void setPath(String path) {
this.Path = path;
}
@Override
public String toString(){
return "Id = " + Id + ", Name = " + Name + ", Path = " + Path;
}
}
EcommerceDAO:
package com.searcher.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.searcher.entity.EcommerceEntity;
@Repository
public interface EcommerceDAO {
public void add(EcommerceEntity eCommerce);
public void edit(EcommerceEntity eCommerce);
public void deleteById(int id);
public EcommerceEntity getEcommerceById(int id);
public List<EcommerceEntity> getAllEcommerce();
}
EcommerceImp:
package com.searcher.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.searcher.entity.EcommerceEntity;
@Repository
public class EcommerceImp implements EcommerceDAO{
private static final Logger logger = LoggerFactory.getLogger(UserDAO.class);
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
@Override
public void add(EcommerceEntity eCommerce) {
Session ss = this.sessionFactory.getCurrentSession();
ss.persist(eCommerce);
logger.info("EcommerceEntity deleted successfully, EcommerceEntity Details="+eCommerce);
}
@Override
public void edit(EcommerceEntity eCommerce) {
this.sessionFactory.getCurrentSession().update(eCommerce);
logger.info("EcommerceEntity updated successfully, EcommerceEntity Details="+eCommerce);
}
@Override
public void deleteById(int id) {
EcommerceEntity ecommerceToDelete = getEcommerceById(id);
this.sessionFactory.getCurrentSession().delete(ecommerceToDelete);
logger.info("EcommerceEntity deleted successfully, EcommerceEntity Details="+ecommerceToDelete);
}
@Override
public EcommerceEntity getEcommerceById(int id) {
EcommerceEntity ecommerceToReturn = (EcommerceEntity)this.sessionFactory.getCurrentSession().get(EcommerceEntity.class, id);
logger.info("EcommerceEntity founded successfully, EcommerceEntity Details="+ecommerceToReturn);
return ecommerceToReturn;
}
@SuppressWarnings("unchecked")
@Override
public List<EcommerceEntity> getAllEcommerce() {
@SuppressWarnings("deprecation")
List<EcommerceEntity> ecommerceList = this.sessionFactory.getCurrentSession().createQuery("from ecommerce").list();
logger.info("List<EcommerceEntity> upload successfully, List<EcommerceEntity> Details="+ecommerceList.toString());
return ecommerceList;
}
}
EcommerceService:
package com.searcher.service;
import java.util.List;
import com.searcher.entity.EcommerceEntity;
public interface EcommerceService {
public void add(EcommerceEntity eCommerce);
public void edit(EcommerceEntity eCommerce);
public void deleteById(int id);
public EcommerceEntity getEcommerceById(int id);
public List<EcommerceEntity> getAllEcommerce();
}
EcommerceServiceImp:
package com.searcher.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.searcher.dao.EcommerceDAO;
import com.searcher.entity.EcommerceEntity;
@Service("ecommerceService")
public class EcommerceServiceImp implements EcommerceService{
@Autowired
private EcommerceDAO ecommerceDAO;
public void setEcommerceDAO(EcommerceDAO ecommeceDAO){
this.ecommerceDAO = ecommeceDAO;
}
@Override
@Transactional
public void add(EcommerceEntity eCommerce) {
this.ecommerceDAO.add(eCommerce);
}
public EcommerceDAO getEcommerceDAO() {
return ecommerceDAO;
}
@Override
@Transactional
public void edit(EcommerceEntity eCommerce) {
this.ecommerceDAO.edit(eCommerce);
}
@Override
@Transactional
public void deleteById(int id) {
this.ecommerceDAO.deleteById(id);
}
@Override
@Transactional
public EcommerceEntity getEcommerceById(int id) {
return this.ecommerceDAO.getEcommerceById(id);
}
@Override
@Transactional
public List<EcommerceEntity> getAllEcommerce() {
return (List<EcommerceEntity>)this.ecommerceDAO.getAllEcommerce();
}
}
MainController:
package com.searcher.controller;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.searcher.entity.EcommerceEntity;
import com.searcher.service.EcommerceService;
import com.searcher.service.EcommerceServiceImp;
@Controller
public class MainSearcherController {
@Autowired
private EcommerceService ecommerceService;
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String message = "<br><div style='text-align:center;'>"
+ "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is coming from MainSearcherController.java **********</div><br><br>";
String strEndList = "";
try {
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("searcher-servlet.xml");
//EcommerceDAO ecommerceDAO = context.getBean(EcommerceDAO.class);
//EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("hibernate5AnnotatedSessionFactory");
//EntityManager entityManager = entityManagerFactory.createEntityManager();
// Creating ImageService object
EcommerceEntity ecommerceEntity = new EcommerceEntity();
ecommerceEntity.setId(4564564);
ecommerceEntity.setName("Ebay");
ecommerceEntity.setPath("www.ebay.com");
this.ecommerceService.add(ecommerceEntity);
//strEndList = "<br/><br/>" + ecommerceService.getAllEcommerce().toString() + "<br/><br/>";
} catch (Exception e) {
throw e;
}
return new ModelAndView("welcome", "message", message + strEndList);
}
}
搜索-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Searcher-Servlet 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/" />
<context:annotation-config />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<beans:property name="prefix" value="/WEB-INF/jsp/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- DataSource -->
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url"
value="jdbc:mysql://localhost:3306/SearcherDB" />
<beans:property name="username" value="root" />
<beans:property name="password" value="root" />
</beans:bean>
<!-- Hibernate 5 SessionFactory Bean definition -->
<beans:bean id="hibernate5AnnotatedSessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.searcher.entity.EcommerceEntity</beans:value>
<beans:value>com.searcher.entity.ImageEntity</beans:value>
<beans:value>com.searcher.entity.ProductEntity</beans:value>
<beans:value>com.searcher.entity.ProductRequestEntity</beans:value>
<beans:value>com.searcher.entity.RequestEntity</beans:value>
<beans:value>com.searcher.entity.SellerEntity</beans:value>
<beans:value>com.searcher.entity.UserEntity</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.format_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!-- eCommerce -->
<beans:bean id="ecommerceDAO" class="com.searcher.dao.EcommerceImp">
<beans:property name="sessionFactory"
ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="ecommerceService" class="com.searcher.service.EcommerceServiceImp">
<beans:property name="ecommerceDAO" ref="ecommerceDAO">
</beans:property>
</beans:bean>
<!-- Image -->
<beans:bean id="imageDAO" class="com.searcher.dao.ImageImp">
<beans:property name="sessionFactory"
ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="imageService" class="com.searcher.service.ImageServiceImp">
<beans:property name="imageDAO" ref="imageDAO">
</beans:property>
</beans:bean>
<!-- Product -->
<beans:bean id="productDAO" class="com.searcher.dao.ProductImp">
<beans:property name="sessionFactory"
ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="productService" class="com.searcher.service.ProductServiceImp">
<beans:property name="productDAO" ref="productDAO">
</beans:property>
</beans:bean>
<!-- ProductRequest -->
<beans:bean id="productRequestDAO" class="com.searcher.dao.ProductRequestImp">
<beans:property name="sessionFactory"
ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="productRequestService"
class="com.searcher.service.ProductRequestServiceImp">
<beans:property name="productRequestDAO" ref="productRequestDAO">
</beans:property>
</beans:bean>
<!-- Request -->
<beans:bean id="requestDAO" class="com.searcher.dao.RequestImp">
<beans:property name="sessionFactory"
ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="requestService" class="com.searcher.service.RequestServiceImp">
<beans:property name="requestDAO" ref="requestDAO">
</beans:property>
</beans:bean>
<!-- Seller -->
<beans:bean id="sellerDAO" class="com.searcher.dao.SellerImp">
<beans:property name="sessionFactory"
ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="sellerService" class="com.searcher.service.SellerServiceImp">
<beans:property name="sellerDAO" ref="sellerDAO">
</beans:property>
</beans:bean>
<!-- User -->
<beans:bean id="userDAO" class="com.searcher.dao.UserImp">
<beans:property name="sessionFactory"
ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="userService" class="com.searcher.service.UserServiceImp">
<beans:property name="userDAO" ref="userDAO">
</beans:property>
</beans:bean>
<context:component-scan base-package="com.searcher." />
<tx:annotation-driven transaction-manager="transactionManager" />
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<beans:property name="sessionFactory"
ref="hibernate5AnnotatedSessionFactory" />
</beans:bean>
</beans>
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Searcher</groupId>
<artifactId>Searcher</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.0.RELEASE</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.0.Final</version>
</dependency>
<!-- Apache Commons DBCP -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.21</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<!-- Build -->
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:0)
您似乎正在尝试保留已经存在于表格中的实体。
您还将id作为序列生成器,但在模型对象中设置了id。
你第二次跑步时得到这个例外吗?
要解决此问题,
1.don设置id 2.如果你正在做更新使用方法saveOrUpdate
答案 1 :(得分:0)
错误为detached entity passed to persist: com.searcher.entity.EcommerceEntity
这意味着您正在尝试更新与当前持久性上下文分离的EcommerceEntity
实体。
要更新此实体,您必须使用merge()
方法。
@Override
public void edit(EcommerceEntity eCommerce) {
this.sessionFactory.getCurrentSession().merge(eCommerce);
logger.info("EcommerceEntity updated successfully, EcommerceEntity Details="+eCommerce);
}
会话关闭后修改项目对其在数据库中的持久表示没有影响。一旦关闭持久化上下文,item就会成为一个分离的实例。
如果要保存对分离对象所做的修改,则必须重新附加或合并它。
希望这有帮助。
答案 2 :(得分:0)
第一个异常NullPointerException
是由于引用分配问题引起的。服务类无法识别服务类EcommerceService
。
显示java.lang.NullPointerException 在com.searcher.controller.MainSearcherController.helloWorld(MainSearcherController.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
在@Autowired
中添加标记MainSearcherController
可以解决此问题。
@Autowired
private EcommerceService ecommerceService;
第二个例外PersistentObjectException
是由于Hibernate中的标识符冲突。 EcommerceEntity
使用注释@GeneratedValue
。这个标签会让hibernate为你生成Id,但同时你在控制器中给出了另一个Id:
ecommerceEntity.setId(4564564);
所以Hibernate不知道该怎么做。由于@GeneratedValue
高度用于Hibernate,因此解决方案是禁用ID setter。有关详细信息,请查看:Stack Overflow • org.hibernate.PersistentObjectException: detached entity passed to persist - with JPA。