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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:37:37+00:00 2026-05-30T15:37:37+00:00

I have two basic domain classes: public class Event { private Long id; private

  • 0

I have two basic domain classes:

public class Event {

    private Long id;
    private String title;
    private Date date;
    // getters/setters...
}

public class Person {

    private Long id;
    private int age;
    private String firstname;
    private String lastname;
    private Set<Event> events = new HashSet();
    // getters/setters...
}

This is the hibernate mapping file for the classes:

<class name="Event" table="EVENTS">
    <id name="id" column="EVENT_ID">
        <generator class="native"/>
    </id>
    <property name="date" type="timestamp" column="EVENT_DATE"/>
    <property name="title"/>
</class>

<class name="Person" table="PERSON">
    <id name="id" column="PERSON_ID">
        <generator class="native"/>
    </id>
    <property name="age"/>
    <property name="firstname"/>
    <property name="lastname"/>
    <set name="events" table="PERSON_EVENT">
        <key column="PERSON_ID"/>
        <many-to-many column="EVENT_ID" class="Event"/>
    </set>
</class>

If I execute the following:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person person = (Person) session.load(Person.class, 8L);
Event event = (Event) session.load(Event.class, 4L);
person.getEvents().add(event);
session.getTransaction().commit();

The following SQL statements are executed:

select
    person0_.PERSON_ID as PERSON1_1_0_,
    person0_.age as age1_0_,
    person0_.firstname as firstname1_0_,
    person0_.lastname as lastname1_0_
from PERSON person0_
where person0_.PERSON_ID=?

-- Why does this do an inner join?
select
    events0_.PERSON_ID as PERSON1_1_1_,
    events0_.EVENT_ID as EVENT2_1_,
    event1_.EVENT_ID as EVENT1_0_0_,
    event1_.EVENT_DATE as EVENT2_0_0_,
    event1_.title as title0_0_
from PERSON_EVENT events0_
inner join EVENTS event1_ on events0_.EVENT_ID=event1_.EVENT_ID
where events0_.PERSON_ID=?

insert into PERSON_EVENT (PERSON_ID, EVENT_ID) values (?, ?)

So finally, the question:

Why does the session.load(Event.class, 4L); use an inner join as shown above? And not just a simple select on the Event table where the ID = 4?

  • 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-30T15:37:39+00:00Added an answer on May 30, 2026 at 3:37 pm

    The inner join is due to the many-to-many mapping of person and events. All the events of a person are loaded by Person.getEvents() before the event can be added.

    UPDATE

    There is no database query needed for session.load() because with session.load() Hibernate assumes the entity with the given id exists (Exception otherwise) and just generates a proxy for the given id. With session.get() Hibernate will go to the database (if the entity with this id is not already in the session cache or 2nd level cache) and return a persistent instance.

    I think when you debug your code you will also not see a query for loading the person until person.getEvents() is called.

    Hope that helps.

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

Sidebar

Related Questions

I have two basic models, Story and Category: class Category(models.Model): title = models.CharField(max_length=50) slug
Let me have two very basic objects like: public class View { public View(Controller
I'm new to C++, so this question may be basic: I have two classes
I have two classes, and want to include a static instance of one class
I have a custom user control UserProfileLink on which I have two basic string
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string
I've got a basic question about obj-c coding, say I have two classes represented
I have two basic interface-related concepts that I need to have a better understanding
I have a basic ActiveRecord model in which i have two fields that i
I have two basic Cpp tasks, but still I have problems with them. First

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.