Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ...w.s.m.s.DefaultHandlerExceptionResolver :
- Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException:
- Could not write JSON document: failed to lazily initialize a collection of role:
- com.foobar.bar.domain.Bar.manyBars, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]-com.foobar.bar.domain.Bar["manyBars"]);
- nested exception is com.fasterxml.jackson.databind.JsonMappingException:
- failed to lazily initialize a collection of role:
- com.foobar.bar.domain.Bar.manyBars, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.foobar.bar.domain.Bar["manyBars"])
- # MySQL DB - "foo"
- spring.datasource.url=jdbc:mysql://localhost:3306/foo?currentSchema=public
- spring.datasource.username=XXX
- spring.datasource.password=XXX
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- # MySQL DB - "bar"
- bar.datasource.url=jdbc:mysql://localhost:3306/bar?currentSchema=public
- bar.datasource.username=XXX
- bar.datasource.password=XXX
- bar.datasource.driver-class-name=com.mysql.jdbc.Driver
- # JPA
- spring.jpa.show-sql=true
- spring.jpa.hibernate.ddl-auto=none
- spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
- @Configuration
- @EnableTransactionManagement
- @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
- transactionManagerRef = "transactionManager",
- basePackages = {"com.foobar.foo.repo"})
- public class FooDbConfig {
- @Primary
- @Bean(name = "dataSource")
- @ConfigurationProperties(prefix = "spring.datasource")
- public DataSource dataSource() {
- return DataSourceBuilder.create().build();
- }
- @Primary
- @Bean(name = "entityManagerFactory")
- public LocalContainerEntityManagerFactoryBean entityManagerFactory(
- EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
- return builder
- .dataSource(dataSource)
- .packages("com.foobar.foo.domain")
- .persistenceUnit("foo")
- .build();
- }
- @Primary
- @Bean(name = "transactionManager")
- public PlatformTransactionManager transactionManager(
- @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
- return new JpaTransactionManager(entityManagerFactory);
- }
- }
- @Configuration
- @EnableTransactionManagement
- @EnableJpaRepositories(entityManagerFactoryRef = "barEntityManagerFactory",
- transactionManagerRef = "barTransactionManager", basePackages = {"com.foobar.bar.repo"})
- public class BarDbConfig {
- @Bean(name = "barDataSource")
- @ConfigurationProperties(prefix = "bar.datasource")
- public DataSource dataSource() {
- return DataSourceBuilder.create().build();
- }
- @Bean(name = "barEntityManagerFactory")
- public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
- EntityManagerFactoryBuilder builder, @Qualifier("barDataSource") DataSource dataSource) {
- return builder
- .dataSource(dataSource)
- .packages("com.foobar.bar.domain")
- .persistenceUnit("bar")
- .build();
- }
- @Bean(name = "barTransactionManager")
- public PlatformTransactionManager barTransactionManager(
- @Qualifier("barEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
- return new JpaTransactionManager(barEntityManagerFactory);
- }
- }
- @RestController
- public class FooBarController {
- private final FooRepository fooRepo;
- private final BarRepository barRepo;
- @Autowired
- FooBarController(FooRepository fooRepo, BarRepository barRepo) {
- this.fooRepo = fooRepo;
- this.barRepo = barRepo;
- }
- @RequestMapping("/foo")
- public List<Foo> listFoo() {
- return fooRepo.findAll();
- }
- @RequestMapping("/bar")
- public List<Bar> listBar() {
- return barRepo.findAll();
- }
- @RequestMapping("/foobar/{id}")
- public String fooBar(@PathVariable("id") Integer id) {
- Foo foo = fooRepo.findById(id);
- Bar bar = barRepo.findById(id);
- return foo.getName() + " " + bar.getName() + "!";
- }
- }
- @Entity
- @Table(name = "foo")
- public class Foo {
- @Id
- @GeneratedValue(strategy = IDENTITY)
- @Column(name = "id", unique = true, nullable = false)
- private Integer id;
- @Column(name = "name")
- private String name;
- @OneToMany(fetch = FetchType.LAZY, mappedBy = "foo")
- @JsonIgnoreProperties({"foo"})
- private Set<ManyFoo> manyFoos = new HashSet<>(0);
- // Constructors, Getters, Setters
- }
- @Entity
- @Table(name = "many_foo")
- public class ManyFoo {
- @Id
- @GeneratedValue(strategy = IDENTITY)
- @Column(name = "id", unique = true, nullable = false)
- private Integer id;
- @Column(name = "name")
- private String name;
- @ManyToOne(fetch = FetchType.LAZY)
- @JsonIgnoreProperties({"manyFoos"})
- private Foo foo;
- // Constructors, Getters, Setters
- }
- @SpringBootApplication
- public class Application {
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
Add Comment
Please, Sign In to add comment