У меня следующий сценарий:
Я создал приложение SpringBoot для копирования некоторых данных из базы данных в другую базу данных. Итак, у меня есть два источника данных в моей конфигурации:
datasources:
new:
driverClassName: org.postgresql.Driver
jdbc-url: jdbc:postgresql://localhost:5432/my_db?currentSchema=public
username: db_user
password: 123456
connection-test-query: SELECT 1
old:
driverClassName: org.postgresql.Driver
jdbc-url: jdbc:postgresql://localhost:5432/old_db?currentSchema=public
username: linedock
password: 123456
connection-test-query: SELECT 1
spring:
jpa:
hibernate:
ddl-auto: none
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
Я создал класс записи, который реализует CommandLineRunner с этим кодом:
@Autowired
private Transformations transformations;
@Override
public void run(String... args) throws Exception {
//some code to get the IDs...
this.transformations.copyData(oldId, newId);
}
Где преобразования имеют следующий код:
@Component
public class Transformations {
@Autowired
private OldRepository oldTreatRep;
@Autowired
private NewRepository treatRep;
...//some methods
@Transactional
public void copyData(Long oldId, Integer newId) {
OldTreatment old = this.oldTreatRep.getOne(oldId);
Treatment t = this.treatRep.getOne(newId);
TreatmentVersion version = t.getVersions().get(0); //I know there is just one
// some operations here
}
}
Оба репозитория выглядят так:
@Repository
public interface NewRepository extends JpaRepository<OldTreatment, Long> {
}
И я получаю следующее исключение:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:783) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:764) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:319) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at migration.App.main(App.java:79) [classes/:na]
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy [migration.model.Treatment#36507] - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at migration.model.Treatment$HibernateProxy$w7TWIE8m.getVersiones(Unknown Source) ~[classes/:na]
at migration.export.Transformations.copyData(Transformations.java:685) ~[classes/:na]
at migration.export.Transformations$$FastClassBySpringCGLIB$$efd88e06.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at migration.export.Transformations$$EnhancerBySpringCGLIB$$e3aeb168.copyData(<generated>) ~[classes/:na]
at migration.FixApp.run(FixApp.java:65) ~[classes/:na]
at migration.FixApp$$FastClassBySpringCGLIB$$5e7412e9.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at migration.FixApp$$EnhancerBySpringCGLIB$$50559e62.run(<generated>) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:780) [spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
... 5 common frames omitted
Является ли использование двух источников данных проблемой для Hibernate или Spring? Мне нужно, чтобы обе сессии были активными для доступа (из одной сущности) и записи (в другую) некоторых свойств, а затем сохранения изменений.