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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:34:16+00:00 2026-05-21T16:34:16+00:00

I have two entities which are Student and Class entities. Student and Class are

  • 0

I have two entities which are Student and Class entities.
Student and Class are many to one relationship. So student contains class attribute.

Now i want to save or create a student associate with existing class(means i know primary key ID already).

Solution 1:

Student student = new Student();
Class class = session.load(classId);  
student.setClass(class);
session.save(student);

Solution 2:

Student student = new Student();
Class class = new Class();
class.setClassId(classId);
student.setClass(class);
session.save(student);

My question here is in solution 1 it will issue two SQL, one is to get Class another is to insert student. But in solution 2 only need to have one SQL. If I have more class attribute,
i will load and issue more select sql before insert. It seems not that efficient. Is there any side-effect in solution 2?

which way to do save/insert is better? By the way, i do not set up cascade.

Thank you
Yee Chen

  • 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-21T16:34:17+00:00Added an answer on May 21, 2026 at 4:34 pm

    Solution 1 won’t issue an SQL query to load Class. Unlike get(), load() returns a proxy object with the specified identifier and doesn’t perform a database query immediately. Thus, load() method is a natural choice for this scenario (when you actually need to load an object, use get()).

    Possible side effect of solution 2 depends on cascading configuration of relationship and so on. Even if it works fine in your current case, it makes your code more fragile, since seemingly unrelated changes in the code may break it.

    So, I recommend you to use Solution 1 and don’t worry about performance.

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

Sidebar

Related Questions

I have two entities, Entity1 and Entity2, on a one to many relationship. On
i have two entities named Parent and Child , linked in a one-to-many relationship.
I have two entities that have a relationship for which I create a join
I have a data model which has two entities in a relationship of one
i have two entities one called User and another called Membership which has a
I have two entities Foo and Bar with a Many to Many relationship between
I have two entities: Patient and Job. Patient has a to-many relationship to Job
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many
I have two objects (class, student) in a database with a many to many
I have two entities that have a many-to-many relationship; they are mapped with annotations

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.