JpaRepository: No Transaction in progress
Ich bin mit Spring Data Jpa zusammen mit hibernate. Ich habe ein repository, das reicht JpaRepository und ich benutze es zum speichern von Datensätzen in der Datenbank. Aber wenn ich rufe .flush(), der folgende Fehler tritt auf.
Ich habe gesehen, andere Fragen, die mit einem ähnlichen Fehler, aber nach dem Versuch es, @Transaktion im repository als gut, bekomme ich den gleichen Fehler.
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
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:606)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
at com.sun.proxy.$Proxy64.flush(Unknown Source)
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:606)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:262)
at com.sun.proxy.$Proxy64.flush(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.flush(SimpleJpaRepository.java:397)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.saveAndFlush(SimpleJpaRepository.java:365)
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:606)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:333)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 75 more
Repository
@Repository
@Transactional
public interface AttributeRepository extends JpaRepository<Attribute, Long> {
Attribute findById(long id);
}
Meine config enthält die entity manager factory und die platform transaction manager JPA verlangt,
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(BoneCPDataSource ds) throws IOException {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(ds);
em.setPackagesToScan(new String[] { "org.scripps.branch.entity" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public PlatformTransactionManager transactionManager(
EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
Service, wo die saveandFlush () - Methode aufgerufen wird,
@Service
public interface FeatureService {
@Transactional
public List<Feature> rankFeatures(Instances data, List<String> entrezIds, Dataset d);
}
Nur hinzufügen, ich bin Injektion das repository mit @Autowired
in der Umsetzung des Dienstes.
- Ja, ich habe die Anmerkung. Ich bin in der Lage, zum abrufen von Objekten, die mithilfe des repository, aber nicht in der Lage zu speichern und bündig ein Unternehmen.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Anmerkungen zu Ihrem repository mit
@Transactional
wird nicht helfen; die haben Sie zu beschriften die service-Methode mit@Transactional
. "service-Methode", das heißt der Methode, Kapselung sowohl der Aufruf in dersave
Methode des repo-und dieflush
nennen.@Transactional
an die service-Methode, aber ich bekomme immer noch den gleichen Fehler. Ich bin über ein interface für den service, und ich habe kommentiert die Methode in der Schnittstelle und deren Umsetzung mit@Transactional
.Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Transactional annotation, as opposed to annotating interfaces.
Es wird nicht Schaden, Anmerkungen, zweimal, aber es kann verwirrend sein.Ich denke, du hast vergessen zu aktivieren Jpa-Transaktions-oder zu verknüpfen Feder von Daten mit dem Transaktions-manager:
Ex aktivieren transaction management:
Ex associate transaction manager mit Jpa Repositories:
@EnableTransactionManagement
und@EnableJpaRepositories(basePackages = { "org.scripps.branch.repository" })
in meiner config. Hinweis: Das repository ist in der Lage, Daten abzufragen, wird der Fehler geworfen wird nur, wenn ichflush()
Ich es gelöst. Meine Konfigurations-Dateien wurden unterteilt in 2 Klassen, eine für den Anwendungskontext und eine für die Persistenz-Kontext. Ich kombinierte beide und der Fehler verschwand.
final
oder nicht über einen parameterlosen Konstruktor? Gut zu hören, es funktioniert zumindest.