Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@OneToMany mapping list size limit

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.

like image 329
gus Avatar asked Oct 25 '11 19:10

gus


People also ask

How do you map one-to-many?

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.

What is difference between mappedBy and @JoinColumn?

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.

What is @OneToMany mappedBy?

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.

What is bidirectional mapping in JPA?

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.


1 Answers

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;
like image 126
jeha Avatar answered Sep 19 '22 18:09

jeha