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 6149085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:23:35+00:00 2026-05-23T19:23:35+00:00

Let me be more specific, it was hard to describe this in the title.

  • 0

Let me be more specific, it was hard to describe this in the title.

I have a class called Bucket, which has a map to a series of Labels. The Bucket class has an id attribute. The class Label doesn’t have a reference back to Bucket, instead it has a bucketId field which contains the id of the owning bucket. This was necessary to avoid circular references (Bucket to Label and back Label to Bucket) in order to allow the Bucket objects to be converted to JSON notation.

My Bucket class looks like this:

@Entity(name="Bucket")
@Table(name = "BUCKETS")
public class Bucket implements Serializable {

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "BUCKET_ID")
    protected long id;

    /*
     * Map of labels indexed by the label name. I'm not sure this mapping is correct!
     */
    @ElementCollection
    @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @MapKeyColumn(name="LABEL_NAME")
    private Map<String, Label> labels = new HashMap<String, Label>();

    ...

}

and the Label class looks like this:

@Entity(name="Label")
@Table(name = "LABELS")
public class Label implements Serializable {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "LABEL_ID")
    protected long id;

    /*
     * How do I map this? It should point to the id of the Bucket instance that has
     * this label in its "labels" map
     */
    protected long bucketId;


    @Column(name = "LABEL_NAME", length=250, nullable=false)
    private String name;

    ...

}

If I could have a Bucket reference in the Label, I would map it as a @OneToMany and then in the Buckets class I would map as @ManyToOne(mappedBy=”bucket”), but I don’t have that reference, just the id pointing back.

Does anyone knows how do I map this?

Thank you!
Eduardo

  • 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-05-23T19:23:36+00:00Added an answer on May 23, 2026 at 7:23 pm

    For some reason I am not able to comment below GeorgeG’s answer, so I am responding to your second question here:

    In order to set the bucketId in the Label after the Bucket has been created, you’ll need to register an EventListener to do what you want to, for example (this is not necessarily working code).

        public class LabelPostInsertListener implements PostInsertListener {
    
            public void onPostInsert(PostInsertEvent event)
                    throws HibernateException {
                Bucket bucket = (Bucket)event.getEntity();
                for (Label l : bucket.getLabels().values()) {
                    l.setBucketId(bucket.getId());
                }
    
            }
        }
    

    Google Hibernate EventListeners for details on configuration and the like, there are many different types, though PostInsertListener should do be right for this situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To be more specific, let's say we have a collection sorted by properties P1
I have asked aqbout timezones and date/time before but this is a more specific
I have a class ApprovalTicket that has a property called ApprovalRules that is of
I'm having a hard time explaining this. Please let me know of any specific
Let's say I have 3 or more peers connected to the same WiFi Access
back again with some more SQLAlchemy shenanigans. Let me step through this. My table
Let's say I have class A(<class ...>): pass . Should I be explicitly calling
Let me be more specific..What i want to do is that when i click
Actually this is a more specific version of a question I posted in past
The issue This sounds like a GitExtensions bug, to which I couldn't find specific

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.