简体   繁体   中英

Spring Jpa Hibernate / sql

I am new to this so please pardon me on explaining and the question itself. I would appreciate any feedback suggestions on my approach.

User have an account -> account have a list of currencies -> currency have name and balance.

By researching some my UserEntity would look something like this:

     @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @OneToOne
        @JoinColumn(name = "account_id")
        Account account;

2)   

    @Id
    private Long accountId;

    @OneToOne
    private UserEntity userEntity;

    @ManyToOne
    private List<Currency> currencyList;`

3)

     @Data
        public class Currency 
        
            private final String name;
            private final BigDecimal balance;
    
  1. What will be benefit of one to one if i would not join column account id and have it there as a foreign key

  2. I have created Currency wrapper as you can see above but it wont let me have it there as long as Currency is an entity, does Currency must be an entity in my case or i am missing something out? how to store a list of in db. The idea is to be able to pull all the currencies from the account.

I would like to achieve the following by sending post request from UI Bank account creation

Input: ● Customer ID ● List of currencies

Output: ● Account ID ● Customer ID ● List of balances: ○ Available amount ○ Currency

The approach is not bad. In the end you must have as many entities as tables in DB, the one to one relationship is correct if the data model is relational.

Regarding Currency, if you want to save it in DB it will have to be an entity.

In the event that you had the 3 entities, of course you could persist and recover them. But for this you should refine the entities more. For example, two-way relationships would be missing,set type relation correctly and set lazy relations:

Account Entity:

@OneToMany(mappedBy="account",fetch=FetchType.LAZY) //Add lazy for performance, 1 Account N currency
private List<Currency> currencyList;`

Currency Entity:

@ManyToOne(fetch = FetchType.LAZY) //Add lazy for performance, 1 Account N currency
private Account account;

For example if you need insert:

Account a = new Account();
a.set [....]

a.setCurrencyList(new ArrayList<>);

Currency c = new Currency();
c.set [....]
a.getCurrencyList().add(c)


entityManager.persist(a);

For the cascading insert to work, the following entry must be added to the currencyList property of the Account entity, thus N entities can be nested.

@OneToMany(mappedBy="account",fetch=FetchType.LAZY, cascade = {CascadeType.PERSIST})

If you also want the Account entity to update Currency, and the relationship between the two should establish the following annotation:

@OneToMany(orphanRemoval=true ,account="nece",fetch=FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})

orphanRemoval causes if you remove a Currency entity from the currencyList list, it will automatically remove the Currency entity from DB

Your approach is good, but you must create the entities and review them in order to get the full potential of JPA through its annotations.

Also as a tip do not send entities in requests, send DTO with the data you need using frameworks like Orika, it is a security error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM