Are my Spring Boot entities normalised if not How can I do so?

74 Views Asked by At

I have a table having a oneToMany relation with another table

 public class UserDetails {
    @Id
    @Column(nullable = false)
    private String userId;

    @OneToMany(targetEntity = Books.class,
            cascade = CascadeType.ALL,
            orphanRemoval = true,
            fetch = FetchType.EAGER)
    @JoinColumn(name = "userId",
            referencedColumnName = "userId",
            updatable = true,
            insertable = true)
    private List<Books> books = new ArrayList<>();

}

where my Books entity is

public class Books {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;

    @Column(nullable = false)
    private String bookName;

    @Column(nullable = false)
    private Double bookPrice;
}

My entities:

enter image description here

[![enter image description here](https://i.stack.imgur.com/8XHq7.png)](https://i.stack.imgur.com/8XHq7.png)

I can have different prices for the same book for different users.

Are my entities normalised?

If not what changes I can make?

1

There are 1 best solutions below

2
Andrei Lisa On

Your approach look good but i have one suggestion for you, don't use FetchType.Eager for more details please read next ones articles how to avoid it and why it's not a good ideea to use it.

FetchType.Eager code smell

Hibernate 6 guide read about eager fetch type