Things to keep in mind when designing entities

· Tech· Spring
Spring

Here and there

@Setter

prohibited In entities, preferably

Setter

It is better not to use , but to create separate methods for necessary parts. When operating the actual service, the value

set

This is because maintenance can become difficult if there are too many places to do it.

Using a delay strategy

This is because all relationships are set to lazy loading so that in one query, all related tables can be viewed and loaded into memory. In this case, performance deterioration and unnecessary data queries may occur. Therefore

@ManyToOne

or

@OneToOne

Immediate loading is the default, so be sure to

fetch

We recommend setting your strategy to lazy loading. If you have logic that requires immediate loading, you can use fetch joins or object graph navigation to solve the problem. (See Object-Oriented Query Language)

Initializing fields in a collection

When setting up an entity, there are properties that are initialized as shown in the code below to prevent NPE due to relationship mapping. In addition to NPE, management is done through built-in collections provided by Hibernate, so be careful not to change them arbitrarily.

@OneToMany(mappedBy = "member")
private List<Orders> orders = new ArrayList<>();

How do you manage collections in Hibernate? I didn't understand after just listening to the lecture, but I found an article that had the same problem as me. When Hibernate persists an entity, if there is a collection inside it, Hibernate changes it to a specially crafted collection. Therefore, it is said that when arbitrary modifications are made, a problem occurs in which Hibernate is not recognized and cannot operate properly. So how does Hibernate manage collections? To manage collections efficiently, Hibernate creates a built-in collection that wraps the original collection when making an entity persistent and changes the reference to use the built-in collection.

PersistentBag

,

PersistentList

,

PersistentSet

Use collection wrappers such as Unlike typical Java collections, these collections provide additional functions for linking with databases. For example,

PersistentBag

is implemented to maintain order while allowing duplicate values. Also,

PersistentBag

is optimized to use memory efficiently by loading only necessary data from the database for synchronization with the database.

Change detection

Using change detection,

find()

In the process of importing an entity, it becomes a management target in the persistence context, so a separate

save()

Even if there is no logic, at the end of the transaction

Dirty Checking

Proceed.

private final EntityManager em;
Member m = em.find(Member.class, 1);
m.setName("John");
m.setAge(30);

merge()

In the case of merge processing, when there are values that have changed from the existing values, they are all merged rather than entered separately.

null

If there is a value received as

null

Since it is processed, it is best to be careful when using it.

Writing entity association methods for object-oriented design

Methods associated with an entity are written together with the entity for management. For example, this includes methods for creating and deleting entities, and calculating only the properties of entities. This is also necessary from an object-oriented design perspective and can prevent unnecessary entity manipulation. For example,

Member

The constructor is used so that the object itself cannot be modified from outside.

protect

This includes processing and writing the creation method yourself.

protected Stock(){}
public void addStock(int quantity){
this.stockQuantity += quantity;
}
public void removeStock(int quantity){
int restStock = this.stockQuantity -= quantity;
if(restStock<0){
throw new NotEnoughStockException("need more stock");
}
this.stockQuantity = restStock;
}

Practical action! Using Spring Boot and JPA1 - Web Application Development [Practical action! Spring Boot and JPA Utilization 2 - API development and performance Optimization](https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED% 8A%B8-JPA-API%EA%B0%9C%EB%B0%9C-%EC%84%B1%EB%8A%A5%EC%B5%9C%EC%A0%81%ED%99%94) [The reason for initializing the collection in the field is What is it?](https://www.inflearn.com/questions/258175/%ED%95%84%EB%93%9C%EC%97%90-%EC%9E%88%EB%8A%94-%EC%BB%AC%EB%A0%89%EC%85%98 %EC%9D%84-%EC%B4%88%EA%B8%B0%ED%99%94-%EC%8B%9C%ED%82%A4%EB%8A%94-%EC%9D%B4%EC%9C%A0%EA%B0%80-%EB%AD%94%EA%B0%80%EC%9A%94) PersistentBag

Comments

No comments yet. Be the first!

    164 posts in 테크

    15 / 164