Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 KB | None | 0 0
  1. Feb 03, 2017 5:51:03 PM org.apache.catalina.core.StandardWrapperValve invoke
  2. 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
  3. org.hibernate.PersistentObjectException: detached entity passed to persist: com.gif.code.model.GifModel
  4. at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:139)
  5. at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
  6. at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
  7. at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
  8. at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
  9. at com.gif.code.dao.GifDao.addData(GifDao.java:38)
  10. at com.gif.code.service.GifService.addData(GifService.java:32)
  11. at com.gif.code.service.GifService$$FastClassBySpringCGLIB$$c23a8dd4.invoke(<generated>)
  12. at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
  13. at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
  14. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
  15. at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
  16. at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
  17. at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
  18. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  19. at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
  20. at com.gif.code.service.GifService$$EnhancerBySpringCGLIB$$4ee26afc.addData(<generated>)
  21. at com.gif.code.controller.GifController.addData(GifController.java:53)
  22. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  23. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  24. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  25.  
  26. package com.gif.code.controller;
  27.  
  28. import java.util.List;
  29.  
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.web.bind.annotation.PathVariable;
  32. import org.springframework.web.bind.annotation.RequestBody;
  33. import org.springframework.web.bind.annotation.RequestMapping;
  34. import org.springframework.web.bind.annotation.RequestMethod;
  35. import org.springframework.web.bind.annotation.RestController;
  36.  
  37. import com.gif.code.model.GifModel;
  38. import com.gif.code.service.GifService;
  39.  
  40. @RestController
  41. public class GifController {
  42.  
  43. @Autowired
  44. GifService gifService;
  45.  
  46. @RequestMapping(value = "/getAllData", method = RequestMethod.GET, headers = "Accept=application/json")
  47. public List<GifModel> getData() {
  48. System.out.println("In Controller");
  49. List<GifModel> listOfData = gifService.getAllData();
  50. return listOfData;
  51. }
  52.  
  53. @RequestMapping(value = "/getData/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
  54. public GifModel getDataById(@PathVariable int id) {
  55. return gifService.getData(id);
  56. }
  57.  
  58. @RequestMapping(value = "/addData", method = RequestMethod.POST, headers = "Accept=application/json")
  59. public void addData(@RequestBody GifModel gifModel) {
  60. gifService.addData(gifModel);
  61.  
  62. }
  63.  
  64. }
  65.  
  66. package com.gif.code.model;
  67.  
  68. import javax.persistence.Column;
  69. import javax.persistence.Entity;
  70. import javax.persistence.GeneratedValue;
  71. import javax.persistence.GenerationType;
  72. import javax.persistence.Id;
  73. import javax.persistence.Table;
  74.  
  75. @Entity
  76. @Table(name="WEBSITE")
  77. public class GifModel {
  78.  
  79. @Id
  80. @Column(name="id")
  81. @GeneratedValue(strategy=GenerationType.IDENTITY)
  82. int id;
  83.  
  84. @Column(name="title")
  85. String title;
  86.  
  87. @Column(name="description")
  88. String description;
  89.  
  90. public GifModel() {
  91. super();
  92. }
  93. public GifModel(int i, String title,String description) {
  94. super();
  95. this.id = i;
  96. this.title = title;
  97. this.description=description;
  98. }
  99. public int getId() {
  100. return id;
  101. }
  102. public void setId(int id) {
  103. this.id = id;
  104. }
  105. public String getTitle() {
  106. return title;
  107. }
  108. public void setTitle(String title) {
  109. this.title = title;
  110. }
  111. public String getDescription() {
  112. return description;
  113. }
  114. public void setDescription(String description) {
  115. this.description = description;
  116. }
  117.  
  118. package com.gif.code.service;
  119.  
  120.  
  121. import java.util.List;
  122.  
  123. import org.springframework.beans.factory.annotation.Autowired;
  124. import org.springframework.stereotype.Service;
  125. import org.springframework.transaction.annotation.Transactional;
  126.  
  127. import com.gif.code.dao.GifDao;
  128. import com.gif.code.model.GifModel;
  129.  
  130. @Service("gifService")
  131. public class GifService {
  132.  
  133. @Autowired
  134. GifDao gifDao;
  135.  
  136. @Transactional
  137. public List<GifModel> getAllData() {
  138. System.out.println("In Service");
  139. return gifDao.getAllData();
  140. }
  141.  
  142. @Transactional
  143. public GifModel getData(int id) {
  144. return gifDao.getData(id);
  145. }
  146.  
  147. @Transactional
  148. public void addData(GifModel gifmodel) {
  149. gifDao.addData(gifmodel);
  150. }
  151.  
  152. }
  153.  
  154. package com.gif.code.dao;
  155.  
  156. import java.util.List;
  157.  
  158. import org.hibernate.Session;
  159. import org.hibernate.SessionFactory;
  160. import org.springframework.beans.factory.annotation.Autowired;
  161. import org.springframework.stereotype.Repository;
  162.  
  163. import com.gif.code.model.GifModel;
  164.  
  165. @Repository
  166. public class GifDao {
  167.  
  168. @Autowired
  169. private SessionFactory sessionFactory;
  170.  
  171. public void setSessionFactory(SessionFactory sf) {
  172. this.sessionFactory = sf;
  173. }
  174.  
  175. @SuppressWarnings("unchecked")
  176. public List<GifModel> getAllData() {
  177. System.out.println("In Dao");
  178. Session session = this.sessionFactory.getCurrentSession();
  179. List<GifModel> textList = session.createQuery("from GifModel").list();
  180. return textList;
  181. }
  182.  
  183. public GifModel getData(int id) {
  184. Session session = this.sessionFactory.getCurrentSession();
  185. GifModel gifmodel = (GifModel) session.load(GifModel.class, new Integer(id));
  186. return gifmodel;
  187. }
  188.  
  189. public GifModel addData(GifModel gifmodel) {
  190. Session session = this.sessionFactory.getCurrentSession();
  191. session.persist(gifmodel);
  192. return gifmodel;
  193. }
  194.  
  195. }
  196.  
  197. <?xml version="1.0" encoding="UTF-8"?>
  198. <beans:beans xmlns="http://www.springframework.org/schema/mvc"
  199. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
  200. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  201. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  202. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  203. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  204. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  205.  
  206. <annotation-driven />
  207.  
  208. <resources mapping="/resources/**" location="/resources/" />
  209.  
  210. <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  211. destroy-method="close">
  212. <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
  213. <beans:property name="url"
  214. value="jdbc:mysql://localhost:3306/gifdata" />
  215. <beans:property name="username" value="root" />
  216. <beans:property name="password" value="Chirpn123" />
  217. </beans:bean>
  218.  
  219. <!-- Hibernate 4 SessionFactory Bean definition -->
  220. <beans:bean id="hibernate4AnnotatedSessionFactory"
  221. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  222. <beans:property name="dataSource" ref="dataSource" />
  223. <beans:property name="annotatedClasses">
  224. <beans:list>
  225. <beans:value>com.gif.code.model.GifModel</beans:value>
  226. </beans:list>
  227. </beans:property>
  228. <beans:property name="hibernateProperties">
  229. <beans:props>
  230. <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
  231. </beans:prop>
  232. <beans:prop key="hibernate.show_sql">true</beans:prop>
  233. </beans:props>
  234. </beans:property>
  235. </beans:bean>
  236.  
  237. <context:component-scan base-package="com.gif.code" />
  238.  
  239. <tx:annotation-driven transaction-manager="transactionManager"/>
  240.  
  241. <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  242. <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
  243. </beans:bean>
  244.  
  245.  
  246. </beans:beans>
  247.  
  248. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  249. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  250. <modelVersion>4.0.0</modelVersion>
  251. <groupId>com.gif.code</groupId>
  252. <artifactId>Gif</artifactId>
  253. <packaging>war</packaging>
  254. <version>0.0.1-SNAPSHOT</version>
  255. <name>Gif Maven Webapp</name>
  256. <url>http://maven.apache.org</url>
  257. <dependencies>
  258. <dependency>
  259. <groupId>junit</groupId>
  260. <artifactId>junit</artifactId>
  261. <version>3.8.1</version>
  262. <scope>test</scope>
  263. </dependency>
  264. <!-- <dependency>
  265. <groupId>org.springframework</groupId>
  266. <artifactId>spring-context</artifactId>
  267. <version>3.1.0.RELEASE</version>
  268. </dependency> -->
  269. <dependency>
  270. <groupId>javax.servlet</groupId>
  271. <artifactId>javax.servlet-api</artifactId>
  272. <version>3.1.0</version>
  273. </dependency>
  274.  
  275. <dependency>
  276. <groupId>org.springframework</groupId>
  277. <artifactId>spring-core</artifactId>
  278. <version>${spring.version}</version>
  279. </dependency>
  280. <dependency>
  281. <groupId>org.springframework</groupId>
  282. <artifactId>spring-webmvc</artifactId>
  283. <version>${spring.version}</version>
  284. </dependency>
  285. <dependency>
  286. <groupId>com.fasterxml.jackson.core</groupId>
  287. <artifactId>jackson-databind</artifactId>
  288. <version>2.4.1</version>
  289. </dependency>
  290. <!-- Hibernate -->
  291. <dependency>
  292. <groupId>org.hibernate</groupId>
  293. <artifactId>hibernate-core</artifactId>
  294. <version>${hibernate.version}</version>
  295. </dependency>
  296. <dependency>
  297. <groupId>org.hibernate</groupId>
  298. <artifactId>hibernate-entitymanager</artifactId>
  299. <version>${hibernate.version}</version>
  300. </dependency>
  301.  
  302. <!-- Apache Commons DBCP -->
  303. <dependency>
  304. <groupId>commons-dbcp</groupId>
  305. <artifactId>commons-dbcp</artifactId>
  306. <version>1.4</version>
  307. </dependency>
  308. <!-- Spring ORM -->
  309. <dependency>
  310. <groupId>org.springframework</groupId>
  311. <artifactId>spring-orm</artifactId>
  312. <version>${spring.version}</version>
  313. </dependency>
  314.  
  315. <!-- AspectJ -->
  316. <dependency>
  317. <groupId>org.aspectj</groupId>
  318. <artifactId>aspectjrt</artifactId>
  319. <version>${org.aspectj-version}</version>
  320. </dependency>
  321. <dependency>
  322. <groupId>mysql</groupId>
  323. <artifactId>mysql-connector-java</artifactId>
  324. <version>5.1.6</version>
  325. </dependency>
  326. </dependencies>
  327. <build>
  328. <finalName>Gif</finalName>
  329.  
  330. <plugins>
  331. <plugin>
  332. <groupId>org.apache.maven.plugins</groupId>
  333. <artifactId>maven-compiler-plugin</artifactId>
  334. <version>3.1</version>
  335. <configuration>
  336. <source>${jdk.version}</source>
  337. <target>${jdk.version}</target>
  338. </configuration>
  339. </plugin>
  340. <plugin>
  341. <groupId>org.apache.maven.plugins</groupId>
  342. <artifactId>maven-war-plugin</artifactId>
  343. <configuration>
  344. <failOnMissingWebXml>false</failOnMissingWebXml>
  345. </configuration>
  346. </plugin>
  347. </plugins>
  348.  
  349. </build>
  350. <properties>
  351. <spring.version>4.2.1.RELEASE</spring.version>
  352. <security.version>4.0.3.RELEASE</security.version>
  353. <jdk.version>1.7</jdk.version>
  354. <hibernate.version>4.3.5.Final</hibernate.version>
  355. <org.aspectj-version>1.7.4</org.aspectj-version>
  356. </properties>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement