Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.jpademo;
- import jakarta.persistence.*;
- import jakarta.transaction.Transactional;
- import org.springframework.boot.CommandLineRunner;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.annotation.Bean;
- import org.springframework.core.annotation.Order;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
- import java.util.List;
- import java.util.UUID;
- @SpringBootApplication
- public class JpademoApplication {
- public static void main(String[] args) {
- SpringApplication.run(JpademoApplication.class, args);
- }
- @Bean
- @Order(1)
- public CommandLineRunner save(ReckonerRepository reckonerRepository, AccountsRepository accountsRepository) {
- return new CommandLineRunner() {
- @Override
- @Transactional
- public void run(String... args) throws Exception {
- AccountsEntity accountsEntity = new AccountsEntity();
- accountsEntity.setName("hello world");
- accountsRepository.save(accountsEntity);
- accountsRepository.flush();
- ReckonerEntity entity = new ReckonerEntity();
- entity.setFromAcctEntity(accountsEntity);
- reckonerRepository.save(entity);
- reckonerRepository.flush();
- }
- };
- }
- @Bean
- @Order(2)
- public CommandLineRunner look(ReckonerRepository reckonerRepository, AccountsRepository accountsRepository) {
- return new CommandLineRunner() {
- @Override
- @Transactional
- public void run(String... args) throws Exception {
- List<ReckonerEntity> reckoner = reckonerRepository.findAll();
- ReckonerEntity reckonerEntity = reckoner.get(0);
- AccountsEntity fromAcctEntity = reckonerEntity.getFromAcctEntity();
- System.out.println(reckonerEntity.getUuid());
- System.out.println(fromAcctEntity.getUuid());
- // System.out.println(fromAcctEntity.getName());
- }
- };
- }
- }
- interface ReckonerRepository extends JpaRepository<ReckonerEntity, UUID>, JpaSpecificationExecutor<ReckonerEntity> {
- }
- interface AccountsRepository extends JpaRepository<AccountsEntity, UUID>, JpaSpecificationExecutor<AccountsEntity> {
- }
- @Entity
- class ReckonerEntity {
- @Id
- @GeneratedValue
- private UUID uuid;
- private String name;
- @OneToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
- @JoinColumn(name = "from_acct")
- private AccountsEntity fromAcctEntity;
- public AccountsEntity getFromAcctEntity() {
- return fromAcctEntity;
- }
- public void setFromAcctEntity(AccountsEntity fromAcctEntity) {
- this.fromAcctEntity = fromAcctEntity;
- }
- public UUID getUuid() {
- return uuid;
- }
- public void setUuid(UUID uuid) {
- this.uuid = uuid;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- @Entity
- class AccountsEntity {
- @Id
- @GeneratedValue
- private UUID uuid;
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public UUID getUuid() {
- return uuid;
- }
- public void setUuid(UUID uuid) {
- this.uuid = uuid;
- }
- }
- // Output
- Hibernate: insert into accounts_entity (name,uuid) values (?,?)
- Hibernate: insert into reckoner_entity (from_acct,name,uuid) values (?,?,?)
- Hibernate: select r1_0.uuid,r1_0.from_acct,r1_0.name from reckoner_entity r1_0
- 3106b0ab-551f-4819-aadf-d6db371bf96a
- 8bb64f6d-5a29-44ed-a0a8-02929ae45295
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement