Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Feb 03, 2017 5:51:03 PM org.apache.catalina.core.StandardWrapperValve invoke
- SEVERE: Servlet.service() for servlet [spring] in context with path [/Gif] threw exception [Request processing failed; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.gif.code.model.GifModel] with root cause
- org.hibernate.PersistentObjectException: detached entity passed to persist: com.gif.code.model.GifModel
- at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:139)
- at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
- at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
- at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
- at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
- at com.gif.code.dao.GifDao.addData(GifDao.java:38)
- at com.gif.code.service.GifService.addData(GifService.java:32)
- at com.gif.code.service.GifService$$FastClassBySpringCGLIB$$c23a8dd4.invoke(<generated>)
- at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
- at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
- 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.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
- at com.gif.code.service.GifService$$EnhancerBySpringCGLIB$$4ee26afc.addData(<generated>)
- at com.gif.code.controller.GifController.addData(GifController.java:53)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
- package com.gif.code.controller;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RestController;
- import com.gif.code.model.GifModel;
- import com.gif.code.service.GifService;
- @RestController
- public class GifController {
- @Autowired
- GifService gifService;
- @RequestMapping(value = "/getAllData", method = RequestMethod.GET, headers = "Accept=application/json")
- public List<GifModel> getData() {
- System.out.println("In Controller");
- List<GifModel> listOfData = gifService.getAllData();
- return listOfData;
- }
- @RequestMapping(value = "/getData/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
- public GifModel getDataById(@PathVariable int id) {
- return gifService.getData(id);
- }
- @RequestMapping(value = "/addData", method = RequestMethod.POST, headers = "Accept=application/json")
- public void addData(@RequestBody GifModel gifModel) {
- gifService.addData(gifModel);
- }
- }
- package com.gif.code.model;
- 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="WEBSITE")
- public class GifModel {
- @Id
- @Column(name="id")
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- int id;
- @Column(name="title")
- String title;
- @Column(name="description")
- String description;
- public GifModel() {
- super();
- }
- public GifModel(int i, String title,String description) {
- super();
- this.id = i;
- this.title = title;
- this.description=description;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- package com.gif.code.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.gif.code.dao.GifDao;
- import com.gif.code.model.GifModel;
- @Service("gifService")
- public class GifService {
- @Autowired
- GifDao gifDao;
- @Transactional
- public List<GifModel> getAllData() {
- System.out.println("In Service");
- return gifDao.getAllData();
- }
- @Transactional
- public GifModel getData(int id) {
- return gifDao.getData(id);
- }
- @Transactional
- public void addData(GifModel gifmodel) {
- gifDao.addData(gifmodel);
- }
- }
- package com.gif.code.dao;
- import java.util.List;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- import com.gif.code.model.GifModel;
- @Repository
- public class GifDao {
- @Autowired
- private SessionFactory sessionFactory;
- public void setSessionFactory(SessionFactory sf) {
- this.sessionFactory = sf;
- }
- @SuppressWarnings("unchecked")
- public List<GifModel> getAllData() {
- System.out.println("In Dao");
- Session session = this.sessionFactory.getCurrentSession();
- List<GifModel> textList = session.createQuery("from GifModel").list();
- return textList;
- }
- public GifModel getData(int id) {
- Session session = this.sessionFactory.getCurrentSession();
- GifModel gifmodel = (GifModel) session.load(GifModel.class, new Integer(id));
- return gifmodel;
- }
- public GifModel addData(GifModel gifmodel) {
- Session session = this.sessionFactory.getCurrentSession();
- session.persist(gifmodel);
- return gifmodel;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans:beans xmlns="http://www.springframework.org/schema/mvc"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="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/context http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- <annotation-driven />
- <resources mapping="/resources/**" location="/resources/" />
- <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/gifdata" />
- <beans:property name="username" value="root" />
- <beans:property name="password" value="Chirpn123" />
- </beans:bean>
- <!-- Hibernate 4 SessionFactory Bean definition -->
- <beans:bean id="hibernate4AnnotatedSessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <beans:property name="dataSource" ref="dataSource" />
- <beans:property name="annotatedClasses">
- <beans:list>
- <beans:value>com.gif.code.model.GifModel</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:props>
- </beans:property>
- </beans:bean>
- <context:component-scan base-package="com.gif.code" />
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
- </beans:bean>
- </beans:beans>
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.gif.code</groupId>
- <artifactId>Gif</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>Gif Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <!-- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>3.1.0.RELEASE</version>
- </dependency> -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.4.1</version>
- </dependency>
- <!-- Hibernate -->
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>${hibernate.version}</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <version>${hibernate.version}</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>${spring.version}</version>
- </dependency>
- <!-- AspectJ -->
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjrt</artifactId>
- <version>${org.aspectj-version}</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.6</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>Gif</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.1</version>
- <configuration>
- <source>${jdk.version}</source>
- <target>${jdk.version}</target>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <configuration>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <properties>
- <spring.version>4.2.1.RELEASE</spring.version>
- <security.version>4.0.3.RELEASE</security.version>
- <jdk.version>1.7</jdk.version>
- <hibernate.version>4.3.5.Final</hibernate.version>
- <org.aspectj-version>1.7.4</org.aspectj-version>
- </properties>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement