Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9036079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:54:11+00:00 2026-06-16T08:54:11+00:00

Say I have an entity like this @Entity Class A{ //fields @Onetomany Set<B> b;

  • 0

Say I have an entity like this

@Entity
Class A{
//fields

@Onetomany
Set<B> b; // 
}

Now, how do I limit the number of ‘B’s in the collection in such a way that, when there is a new entry in the collection, the oldest one is removed, some thing like removeEldestEntry we have in a LinkedHashMap.

I am using MySQL 5.5 DB with Hibernate. Thanks in advance.

EDIT

My goal is not to have more than N number of entries in that table at any point of time.
One solution I have is to use a Set and schedule a job to remove the older entries. But I find it dirty. I am looking for a cleaner solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:54:12+00:00Added an answer on June 16, 2026 at 8:54 am

    I would use the code to manually enforce this rule. The main idea is that the collection B should be well encapsulated such that client only can change its content by a public method (i.e addB()) . Simply ensure this rule inside this method (addB()) to ensure that the number of entries inside the collection B cannot larger than a value.

    A:

    @Entity
    public class A {
    
    
        public static int MAX_NUM_B = 4;
    
        @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
        private Set<B> b= new LinkedHashSet<B>();
    
        public void addB(B b) {
            if (this.b.size() == MAX_NUM_B) {
                Iterator<B> it = this.b.iterator();
                it.next();
                it.remove();
            }
            this.b.add(b);
        }
    
        public Set<B> getB() {
            return Collections.unmodifiableSet(this.b);
        }
    }
    

    B:

    @Entity 
    public class B{
    
        @ManyToOne
        private A a;
    }
    

    Main points:

    • A should be the owner of the relationship.
    • In A , do not simply return B as client can bypass the checking logic implemented in addB(B b) and change its content freely.Instead , return an unmodifiable view of B .
    • In @OneToMany , set orphanRemovalto true to tell JPA to remove the B ‘s DB records after its corresponding instances are removed from the B collection.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an entity that looks like this: public class Album() {
I have like this entity, public class Receiving{ [Key] public int ID {get; set;}
Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
If I have entities like: @Entity public class Person { public String name; @OneToMany
Let's say I have a domain object that looks like this: @Entity @Indexed public
Say I have this class: @Entity @Table(name=PICTURE) public class Picture{ private String category1, category2;
Let's say I have a Student entity like this implemented using the Play Framework's
Let's say I have a couple of entities that look like this: @Entity public
Lets say I retrieve an entity object that looks like this: @Entity public class
Let's say there is a entity class like this @Entity public class User {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.