Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table(name = "account")
- public class Account {
- transient EntityManager entityManager;
- @Id
- @GeneratedValue
- private Long id;
- @Column(name = "username", nullable = false, unique = true)
- private String username;
- @Column(name = "password", nullable = false)
- private String password;
- ... getters and setters
- @Transactional
- public void persist() {
- if (this.entityManager == null) this.entityManager = entityManager();
- this.entityManager.persist(this);
- }
- @Transactional
- public Account merge() {
- if (this.entityManager == null) this.entityManager = entityManager();
- Account merged = this.entityManager.merge(this);
- this.entityManager.flush();
- return merged;
- }
- @Configuration
- @ComponentScan
- @EnableAutoConfiguration
- public class Application {
- public static void main(String[] args) throws Exception {
- SpringApplication.run(Application.class, args);
- }
- }
- spring.application.name: Test Application
- spring.datasource.url: jdbc:mysql://localhost/test
- spring.datasource.username=root
- spring.datasource.password=
- spring.datasource.driverClassName=com.mysql.jdbc.Driver
- spring.jpa.hibernate.ddl-auto=update
- @Repository
- public interface AccountRepository extends PagingAndSortingRepository<Account, Long>
- @Configuration
- @ComponentScan
- @EnableAutoConfiguration
- @SpringBootApplication
- @Entity
- @Table(name = "account")
- public class Account {
- @Id
- @GeneratedValue
- private Long id;
- @Column(name = "username", nullable = false, unique = true)
- private String username;
- @Column(name = "password", nullable = false)
- private String password;
- ....
- public interface AccountRepository {
- void persist();
- Account merge();
- }
- @Repository
- class AccountRepositoryJpaImpl impelemnts AccountRepository {
- @Autowired
- private EntityManager entityManager;
- ...
- @Transactional
- public void persist() {
- if (this.entityManager == null) this.entityManager = entityManager();
- this.entityManager.persist(this);
- }
- @Transactional
- public Account merge() {
- if (this.entityManager == null) this.entityManager = entityManager();
- Account merged = this.entityManager.merge(this);
- this.entityManager.flush();
- return merged;
- }
- ....
- }
- @SpringBootApplication
- @EnableTransactionManagement
- class ApplicationConfig {
- public static void main(String[] args) {
- SpringApplication.run(ApplicationConfig .class, args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement