Is there any way to limit the list's size of the @OneToMany relationship in JPA? Here's my code sample:
@OneToMany(mappedBy = "publication", cascade=CascadeType.PERSIST)
private List<Comment> commentList;
I'm using EclipseLink 2.3 JPA implementation. Thanks in advance.
In simple terms, one to many mapping means that one row in a table can be mapped to multiple rows in another table. For example, think of a Cart system where we have another table for Items. A cart can have multiple items, so here we have one to many mapping.
The @JoinColumn annotation helps us specify the column we'll use for joining an entity association or element collection. On the other hand, the mappedBy attribute is used to define the referencing side (non-owning side) of the relationship.
Simply put, one-to-many mapping means that one row in a table is mapped to multiple rows in another table. Let's look at the following entity-relationship diagram to see a one-to-many association: For this example, we'll implement a cart system where we have a table for each cart and another table for each item.
The bidirectional Many-to-One association mapping is the most common way to model this relationship with JPA and Hibernate. It uses an attribute on the Order and the OrderItem entity. This allows you to navigate the association in both directions in your domain model and your JPQL queries.
Part of the Bean Validation Specification (JSR-303) is the @Size(min=, max=)
annotation:
Supported types are String, Collection, Map and arrays. Check if the annotated element size is between min and max (inclusive).
You could validate the collection.
@OneToMany(mappedBy = "publication", cascade=CascadeType.PERSIST)
@Size(min=1, max=10)
private List<Comment> commentList;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With