Guest User

Untitled

a guest
Feb 27th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. ...w.s.m.s.DefaultHandlerExceptionResolver :
  2. Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException:
  3. Could not write JSON document: failed to lazily initialize a collection of role:
  4. 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"]);
  5. nested exception is com.fasterxml.jackson.databind.JsonMappingException:
  6. failed to lazily initialize a collection of role:
  7. 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"])
  8.  
  9. # MySQL DB - "foo"
  10. spring.datasource.url=jdbc:mysql://localhost:3306/foo?currentSchema=public
  11. spring.datasource.username=XXX
  12. spring.datasource.password=XXX
  13. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  14. # MySQL DB - "bar"
  15. bar.datasource.url=jdbc:mysql://localhost:3306/bar?currentSchema=public
  16. bar.datasource.username=XXX
  17. bar.datasource.password=XXX
  18. bar.datasource.driver-class-name=com.mysql.jdbc.Driver
  19. # JPA
  20. spring.jpa.show-sql=true
  21. spring.jpa.hibernate.ddl-auto=none
  22. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  23.  
  24. @Configuration
  25. @EnableTransactionManagement
  26. @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
  27. transactionManagerRef = "transactionManager",
  28. basePackages = {"com.foobar.foo.repo"})
  29. public class FooDbConfig {
  30.  
  31. @Primary
  32. @Bean(name = "dataSource")
  33. @ConfigurationProperties(prefix = "spring.datasource")
  34. public DataSource dataSource() {
  35. return DataSourceBuilder.create().build();
  36. }
  37.  
  38. @Primary
  39. @Bean(name = "entityManagerFactory")
  40. public LocalContainerEntityManagerFactoryBean entityManagerFactory(
  41. EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
  42. return builder
  43. .dataSource(dataSource)
  44. .packages("com.foobar.foo.domain")
  45. .persistenceUnit("foo")
  46. .build();
  47. }
  48.  
  49. @Primary
  50. @Bean(name = "transactionManager")
  51. public PlatformTransactionManager transactionManager(
  52. @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
  53. return new JpaTransactionManager(entityManagerFactory);
  54. }
  55. }
  56.  
  57. @Configuration
  58. @EnableTransactionManagement
  59. @EnableJpaRepositories(entityManagerFactoryRef = "barEntityManagerFactory",
  60. transactionManagerRef = "barTransactionManager", basePackages = {"com.foobar.bar.repo"})
  61. public class BarDbConfig {
  62.  
  63. @Bean(name = "barDataSource")
  64. @ConfigurationProperties(prefix = "bar.datasource")
  65. public DataSource dataSource() {
  66. return DataSourceBuilder.create().build();
  67. }
  68.  
  69. @Bean(name = "barEntityManagerFactory")
  70. public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
  71. EntityManagerFactoryBuilder builder, @Qualifier("barDataSource") DataSource dataSource) {
  72. return builder
  73. .dataSource(dataSource)
  74. .packages("com.foobar.bar.domain")
  75. .persistenceUnit("bar")
  76. .build();
  77. }
  78.  
  79. @Bean(name = "barTransactionManager")
  80. public PlatformTransactionManager barTransactionManager(
  81. @Qualifier("barEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
  82. return new JpaTransactionManager(barEntityManagerFactory);
  83. }
  84. }
  85.  
  86. @RestController
  87. public class FooBarController {
  88.  
  89. private final FooRepository fooRepo;
  90. private final BarRepository barRepo;
  91.  
  92. @Autowired
  93. FooBarController(FooRepository fooRepo, BarRepository barRepo) {
  94. this.fooRepo = fooRepo;
  95. this.barRepo = barRepo;
  96. }
  97.  
  98. @RequestMapping("/foo")
  99. public List<Foo> listFoo() {
  100. return fooRepo.findAll();
  101. }
  102.  
  103. @RequestMapping("/bar")
  104. public List<Bar> listBar() {
  105. return barRepo.findAll();
  106. }
  107.  
  108. @RequestMapping("/foobar/{id}")
  109. public String fooBar(@PathVariable("id") Integer id) {
  110. Foo foo = fooRepo.findById(id);
  111. Bar bar = barRepo.findById(id);
  112.  
  113. return foo.getName() + " " + bar.getName() + "!";
  114. }
  115.  
  116. }
  117.  
  118. @Entity
  119. @Table(name = "foo")
  120. public class Foo {
  121.  
  122. @Id
  123. @GeneratedValue(strategy = IDENTITY)
  124. @Column(name = "id", unique = true, nullable = false)
  125. private Integer id;
  126.  
  127. @Column(name = "name")
  128. private String name;
  129.  
  130. @OneToMany(fetch = FetchType.LAZY, mappedBy = "foo")
  131. @JsonIgnoreProperties({"foo"})
  132. private Set<ManyFoo> manyFoos = new HashSet<>(0);
  133.  
  134. // Constructors, Getters, Setters
  135. }
  136.  
  137. @Entity
  138. @Table(name = "many_foo")
  139. public class ManyFoo {
  140.  
  141. @Id
  142. @GeneratedValue(strategy = IDENTITY)
  143. @Column(name = "id", unique = true, nullable = false)
  144. private Integer id;
  145.  
  146. @Column(name = "name")
  147. private String name;
  148.  
  149. @ManyToOne(fetch = FetchType.LAZY)
  150. @JsonIgnoreProperties({"manyFoos"})
  151. private Foo foo;
  152.  
  153. // Constructors, Getters, Setters
  154. }
  155.  
  156. @SpringBootApplication
  157. public class Application {
  158. public static void main(String[] args) {
  159. SpringApplication.run(Application.class, args);
  160. }
  161. }
Add Comment
Please, Sign In to add comment