mysql - Spring Boot Transactions not working -


if write interface repository , implement crudrepository, there no problem persisting data.

but if don't use crudreposity , write own implementation, data fetch works fine when save data below exception..

caused by: javax.persistence.transactionrequiredexception: no transactional entitymanager available @ org.springframework.orm.jpa.sharedentitymanagercreator$sharedentitymanagerinvocationhandler.invoke(sharedentitymanagercreator.java:275) @ com.sun.proxy.$proxy59.persist(unknown source) @ com.eorganiser.commons.repositories.generic.simplejpagenericrepository.persist(simplejpagenericrepository.java:48) @ com.eorganiser.commons.repositories.generic.simplejpagenericrepository$$fastclassbyspringcglib$$12bdea51.invoke(<generated>) @ org.springframework.cglib.proxy.methodproxy.invoke(methodproxy.java:204) @ org.springframework.aop.framework.cglibaopproxy$cglibmethodinvocation.invokejoinpoint(cglibaopproxy.java:717) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:157) @ org.springframework.dao.support.persistenceexceptiontranslationinterceptor.invoke(persistenceexceptiontranslationinterceptor.java:136) ... 56 common frames omitted 

below base dao wrote..

interface jpagenericrepository.java

import org.springframework.data.repository.norepositorybean; import org.springframework.data.repository.repository;  import java.io.serializable; import java.util.list; import java.util.map; import java.util.optional;  /**  * generic dao implememnted other daos  * created piyush.kl on 21/10/15.  */ @norepositorybean public interface jpagenericrepository<t, id extends serializable> extends repository<t, id> {      /**      * persist entity      * @param entity      */     void persist(t entity);      /**      * persist list of entities      * @param entities, {@link java.util.list} of entities      */     void persist(iterable<? extends t> entities);      /**      * merge changes of existing entity.      * @param entity      * @return merged entity      */     <s extends t> s merge(s entity);      /**      * merge changes of existing entity.      * @param entities, {@link java.util.list} of entities      * @return merged entities      */     <s extends t> iterable<s> merge(iterable<s> entities);      /**      * fetch available entities table.      * @return {@link java.util.list} of entities      */     list<t> findall();      /**      * fetch page of entities table.      * @param pagerequest, consisting page number , size of page.      * @return {@link java.util.list} of entities      */     page<t> findall(pagerequest pagerequest);      /**      * fetch page of entities table.      * @param queryname, named query name      * @param parameters, map of keyword replace in query      * @param pagerequest, , consisting page number , size of page.      * @return, page of entities      */     page<t> findallbynamedquery(string queryname,                                        map<string, object> parameters,                                        pagerequest pagerequest);      /**      * fetch entities table satisfy named query.      * @param queryname, named query name      * @param parameters, map of keyword replace in query      * @return {@link java.util.list} of entities      */     list<t> findallbynamedquery(string queryname,                                 map<string, object> parameters);      /**      * fetch single entity given named query , parameters      * @param queryname, named query name      * @param parameters, map of keyword replace in query      * @return {@link java.util.optional} object represents 0 or 1 entity      */     optional<t> findonebynamedquery(string queryname,                                            map<string, object> parameters);      /**      * fetch entity id      * @param id, id of entity      * @return {@link java.util.optional} object represents 0 or 1 entity      */     optional<t> findone(id id);      /**      * delete entity      * @param t, entity      */     void delete(t t);      /**      * delete given entities      * @param entities, {@link java.util.list} of entities      */     void delete(iterable<? extends t> entities);  } 

implementation:

@norepositorybean @data public class simplejpagenericrepository<t, id extends serializable>         implements jpagenericrepository<t, id> {      @persistencecontext(type = persistencecontexttype.transaction)     private entitymanager entitymanager;      private class<t> entityclass;      public simplejpagenericrepository(class clazz){         this.entityclass = clazz;     }       /**      * {@inheritdoc}      */     @override     public void persist(t t) {         getentitymanager().persist(t);     }      /**      * {@inheritdoc}      */     @override     public void persist(iterable<? extends t> entities) {         (t entity : entities) {             persist(entity);         }     }      /**      * {@inheritdoc}      */     @override     public <s extends t> s merge(s entity) {         return getentitymanager().merge(entity);     }      /**      * {@inheritdoc}      */     @override     public <s extends t> iterable<s> merge(iterable<s> entities) {         list<s> result = new arraylist<>();         (s entity : entities) {             result.add(merge(entity));         }         return result;     }      /**      * {@inheritdoc}      */     @override     public list<t> findall() {         return getentitymanager()                 .createquery("select x " + entityclass.getsimplename() + " x")                 .getresultlist();     }     /**      * {@inheritdoc}      */     @override     public page<t> findall(pagerequest pagerequest) {         long count = (long) getentitymanager()                 .createquery("select count(x) " + entityclass.getsimplename() + " x")                 .getsingleresult();         list<t> content = getentitymanager()                 .createquery("select x " + entityclass.getsimplename() + " x")                 .setfirstresult(pagerequest.getoffset())                 .setmaxresults(pagerequest.getpagesize())                 .getresultlist();          boolean hasmore = pagerequest.getpagesize() == content.size();         return new page<t>(count, content, hasmore);     }      /**      * {@inheritdoc}      */     @override     public page<t> findallbynamedquery(string queryname, map<string, object> parameters,                                        pagerequest pagerequest) {         typedquery<t> countquery = getentitymanager().createnamedquery(queryname + ".count", entityclass);          (map.entry<string, object> entry : parameters.entryset()) {             countquery.setparameter(entry.getkey(), entry.getvalue());         }         long count = (long) countquery                 .getsingleresult();          typedquery<t> query = getentitymanager().createnamedquery(queryname, entityclass);          (map.entry<string, object> entry : parameters.entryset()) {             query.setparameter(entry.getkey(), entry.getvalue());         }         list<t> content = query                 .setfirstresult(pagerequest.getoffset())                 .setmaxresults(pagerequest.getpagesize())                 .getresultlist();          boolean hasmore = pagerequest.getpagesize() == content.size();         return new page<t>(count, content, hasmore);     }      /**      * {@inheritdoc}      */     @override     public list<t> findallbynamedquery(string queryname, map<string, object> parameters) {         typedquery<t> countquery = getentitymanager().createnamedquery(queryname + ".count", entityclass);          (map.entry<string, object> entry : parameters.entryset()) {             countquery.setparameter(entry.getkey(), entry.getvalue());         }          query query = getentitymanager().createnamedquery(queryname);          (map.entry<string, object> entry : parameters.entryset()) {             query.setparameter(entry.getkey(), entry.getvalue());         }          return query.getresultlist();     }      /**      * {@inheritdoc}      */     @override     public optional<t> findonebynamedquery(string queryname, map<string, object> parameters) {         typedquery<t> query = getentitymanager().createnamedquery(queryname, entityclass);          (map.entry<string, object> entry : parameters.entryset()) {             query.setparameter(entry.getkey(), entry.getvalue());         }          try {             return optional.of((t) query.getsingleresult());         } catch (noresultexception e) {             return optional.empty();         }     }      /**      * {@inheritdoc}      */     @override     public optional<t> findone(id id) {         return optional.ofnullable(getentitymanager().find(entityclass, id));     }       /**      * {@inheritdoc}      */     @override     public void delete(t t) {         getentitymanager().remove(t);     }      /**      * {@inheritdoc}      */     @override     public void delete(iterable<? extends t> entities) {         (t entity : entities) {             delete(entity);         }     } } 

implementation

@repository @transactional(readonly = true) public class jobservicesrepository extends simplejpagenericrepository<jobservices, long>{      public jobservicesrepository(){         super(jobservices.class);     }  } 

i have annotated service's public method spring's transactional annotation.

i using spring boot 1.2.7-release, jersey , mysql.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -