Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. @Entity
  2. @Table(name = "account")
  3. public class Account {
  4.  
  5. transient EntityManager entityManager;
  6.  
  7. @Id
  8. @GeneratedValue
  9. private Long id;
  10.  
  11. @Column(name = "username", nullable = false, unique = true)
  12. private String username;
  13.  
  14. @Column(name = "password", nullable = false)
  15. private String password;
  16.  
  17. ... getters and setters
  18.  
  19. @Transactional
  20. public void persist() {
  21. if (this.entityManager == null) this.entityManager = entityManager();
  22. this.entityManager.persist(this);
  23. }
  24.  
  25. @Transactional
  26. public Account merge() {
  27. if (this.entityManager == null) this.entityManager = entityManager();
  28. Account merged = this.entityManager.merge(this);
  29. this.entityManager.flush();
  30. return merged;
  31. }
  32.  
  33. @Configuration
  34. @ComponentScan
  35. @EnableAutoConfiguration
  36. public class Application {
  37.  
  38. public static void main(String[] args) throws Exception {
  39. SpringApplication.run(Application.class, args);
  40. }
  41.  
  42. }
  43.  
  44. spring.application.name: Test Application
  45.  
  46. spring.datasource.url: jdbc:mysql://localhost/test
  47. spring.datasource.username=root
  48. spring.datasource.password=
  49. spring.datasource.driverClassName=com.mysql.jdbc.Driver
  50. spring.jpa.hibernate.ddl-auto=update
  51.  
  52. @Repository
  53. public interface AccountRepository extends PagingAndSortingRepository<Account, Long>
  54.  
  55. @Configuration
  56. @ComponentScan
  57. @EnableAutoConfiguration
  58.  
  59. @SpringBootApplication
  60.  
  61. @Entity
  62. @Table(name = "account")
  63. public class Account {
  64.  
  65. @Id
  66. @GeneratedValue
  67. private Long id;
  68.  
  69. @Column(name = "username", nullable = false, unique = true)
  70. private String username;
  71.  
  72. @Column(name = "password", nullable = false)
  73. private String password;
  74. ....
  75.  
  76. public interface AccountRepository {
  77. void persist();
  78. Account merge();
  79. }
  80.  
  81. @Repository
  82. class AccountRepositoryJpaImpl impelemnts AccountRepository {
  83.  
  84. @Autowired
  85. private EntityManager entityManager;
  86.  
  87. ...
  88. @Transactional
  89. public void persist() {
  90. if (this.entityManager == null) this.entityManager = entityManager();
  91. this.entityManager.persist(this);
  92. }
  93.  
  94. @Transactional
  95. public Account merge() {
  96. if (this.entityManager == null) this.entityManager = entityManager();
  97. Account merged = this.entityManager.merge(this);
  98. this.entityManager.flush();
  99. return merged;
  100. }
  101. ....
  102. }
  103.  
  104. @SpringBootApplication
  105. @EnableTransactionManagement
  106. class ApplicationConfig {
  107.  
  108. public static void main(String[] args) {
  109. SpringApplication.run(ApplicationConfig .class, args);
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement