S
S
sasuke222020-06-18 06:26:16
Java
sasuke22, 2020-06-18 06:26:16

Add counter to one-to-many?

Hello everyone! Now I have such a one-to-many link
5eadeb0b3199395231909.png

@Entity
@Table(name = "user_table")
@Data
@NoArgsConstructor
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_sequence")
    @SequenceGenerator(name = "user_sequence", allocationSize = 1)
    @Column(name = "user_id")
    private Long id;
    private String firstName;
    private String lastName;
    private String middleName;
    //   @Email(message = "Email is not correct")
    private String email;
    @Enumerated(EnumType.STRING)
    private Gender gender;
    @OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true)
    private List<Item> itemList = new ArrayList<>();
}

@Entity
@Table(name = "item_table")
@Data
@NoArgsConstructor
public class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "item_sequence")
    @SequenceGenerator(name = "item_sequence", allocationSize = 1)
    @Column(name = "item_id")
    private Long id;
    @Enumerated(EnumType.STRING)
    private Category category;
    private String name;
    private double price;
    private LocalDate localDate;

}

Question: How can I add a counter to the user_item_table so that each item has a count attribute in the itemList?
5eadebc48d4d089480064.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-06-18
@sasuke22

The opinion of a java champion and author of a book on persistence on this topic.

S
szanislo, 2020-06-18
@szanislo

Implement the table manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question