Advertisement
Guest User

Untitled

a guest
Jul 31st, 2023
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. package com.example.jpademo;
  2.  
  3. import jakarta.persistence.*;
  4. import jakarta.transaction.Transactional;
  5. import org.springframework.boot.CommandLineRunner;
  6. import org.springframework.boot.SpringApplication;
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.core.annotation.Order;
  10. import org.springframework.data.jpa.repository.JpaRepository;
  11. import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
  12.  
  13. import java.util.List;
  14. import java.util.UUID;
  15.  
  16. @SpringBootApplication
  17. public class JpademoApplication {
  18.  
  19. public static void main(String[] args) {
  20. SpringApplication.run(JpademoApplication.class, args);
  21. }
  22.  
  23. @Bean
  24. @Order(1)
  25. public CommandLineRunner save(ReckonerRepository reckonerRepository, AccountsRepository accountsRepository) {
  26. return new CommandLineRunner() {
  27. @Override
  28. @Transactional
  29. public void run(String... args) throws Exception {
  30. AccountsEntity accountsEntity = new AccountsEntity();
  31. accountsEntity.setName("hello world");
  32. accountsRepository.save(accountsEntity);
  33. accountsRepository.flush();
  34.  
  35. ReckonerEntity entity = new ReckonerEntity();
  36. entity.setFromAcctEntity(accountsEntity);
  37. reckonerRepository.save(entity);
  38. reckonerRepository.flush();
  39. }
  40. };
  41. }
  42.  
  43.  
  44. @Bean
  45. @Order(2)
  46. public CommandLineRunner look(ReckonerRepository reckonerRepository, AccountsRepository accountsRepository) {
  47. return new CommandLineRunner() {
  48. @Override
  49. @Transactional
  50. public void run(String... args) throws Exception {
  51.  
  52. List<ReckonerEntity> reckoner = reckonerRepository.findAll();
  53. ReckonerEntity reckonerEntity = reckoner.get(0);
  54. AccountsEntity fromAcctEntity = reckonerEntity.getFromAcctEntity();
  55. System.out.println(reckonerEntity.getUuid());
  56. System.out.println(fromAcctEntity.getUuid());
  57.  
  58. // System.out.println(fromAcctEntity.getName());
  59. }
  60. };
  61. }
  62. }
  63.  
  64. interface ReckonerRepository extends JpaRepository<ReckonerEntity, UUID>, JpaSpecificationExecutor<ReckonerEntity> {
  65. }
  66.  
  67. interface AccountsRepository extends JpaRepository<AccountsEntity, UUID>, JpaSpecificationExecutor<AccountsEntity> {
  68. }
  69.  
  70. @Entity
  71. class ReckonerEntity {
  72. @Id
  73. @GeneratedValue
  74. private UUID uuid;
  75.  
  76. private String name;
  77.  
  78. @OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
  79. @JoinColumn(name = "from_acct")
  80. private AccountsEntity fromAcctEntity;
  81.  
  82. public AccountsEntity getFromAcctEntity() {
  83. return fromAcctEntity;
  84. }
  85.  
  86. public void setFromAcctEntity(AccountsEntity fromAcctEntity) {
  87. this.fromAcctEntity = fromAcctEntity;
  88. }
  89.  
  90. public UUID getUuid() {
  91. return uuid;
  92. }
  93.  
  94. public void setUuid(UUID uuid) {
  95. this.uuid = uuid;
  96. }
  97.  
  98. public String getName() {
  99. return name;
  100. }
  101.  
  102. public void setName(String name) {
  103. this.name = name;
  104. }
  105. }
  106.  
  107.  
  108. @Entity
  109. class AccountsEntity {
  110.  
  111. @Id
  112. @GeneratedValue
  113. private UUID uuid;
  114.  
  115. private String name;
  116.  
  117. public String getName() {
  118. return name;
  119. }
  120.  
  121. public void setName(String name) {
  122. this.name = name;
  123. }
  124.  
  125. public UUID getUuid() {
  126. return uuid;
  127. }
  128.  
  129. public void setUuid(UUID uuid) {
  130. this.uuid = uuid;
  131. }
  132. }
  133.  
  134. // Output
  135.  
  136. Hibernate: insert into accounts_entity (name,uuid) values (?,?)
  137. Hibernate: insert into reckoner_entity (from_acct,name,uuid) values (?,?,?)
  138. Hibernate: select r1_0.uuid,r1_0.from_acct,r1_0.name from reckoner_entity r1_0
  139. 3106b0ab-551f-4819-aadf-d6db371bf96a
  140. 8bb64f6d-5a29-44ed-a0a8-02929ae45295
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement