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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:06:25+00:00 2026-05-26T00:06:25+00:00

Ok, say i have a Person entity which needs to have Persons as property

  • 0

Ok, say i have a “Person” entity which needs to have “Person”s as property (say, friends)
Since this is a many to many connection, If i’d create this schema using SQL (Which i’m not, i’m using the hbm.ddl.auto to do it for me) i’d probably make another table containing 2 columns with 2 ids (1 for each friend)
However, i’m not sure how to annotate this in hibernate, i did this:

class PersonEntity {

.
.

@ElementCollection
private List<PersonEntity> friends ;

.
.
}

Problem is, i’m not sure it’s the best way.. for instance, i’m “thinking” that whenever i add a friend to the list and persist it will be inserted as the next row in the table, and since i can’t seem to index this field i’m imagining the data retrieval will be inefficient.
Can you suggest better ways to solve this problem?

  • 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-26T00:06:26+00:00Added an answer on May 26, 2026 at 12:06 am

    You’re looking for @ManyToMany and @JoinTable.

    The api doc of ElementCollection says:

    Defines a collection of instances of a basic type or embeddable class

    You have a collection of entity instances. See http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#eentity-mapping-association-collection-manytomany for how to use those annotations.

    • 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 a House entity which maps to many Person entities. I
Let's say I have this entity (for Hibernate): @Entity public class Person { @Id
Say you have a model Person. Each Person object can have many Friends (Field_HasMany).
Say I have a model object 'Person' defined, which has a field called 'Name'.
Say I have a class which represents a person, a variable within that class
So let's say you have something like this: Class Person: height = 6' 0
let's say I have entity Person. When I try to use with hibernate createQuery,
I have a entity like this: Person - id_person - name - child //self
Lets say I have two tables like this: person: id, first_name, last_name, phone_id phone:
I have an entity called PendingPartners which has a navigation property to the Residents

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.