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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:04:34+00:00 2026-05-20T13:04:34+00:00

I configured a simple OneToOne mapping in my entity, but when I persist the

  • 0

I configured a simple OneToOne mapping in my entity, but when I persist the object I see a “Select” statement being executed by Hibernate just before the Insert and I don’t know why.

@Entity 
@Table( name = "USER" )
public class NewUser {

   @OneToOne
   @JoinColumn(name="user_status_type_id", nullable=false)
   private UserStatusType userStatusType;

UserStatusType is just a read-only lookup table, so nothing persists in that table when I save User.

User u = new User();
u.setUserStatusType( new UserStatusType(101));
session.persis(u);

But when I persist the User object, the output from Hibernate reads as follows:

Hibernate: select userstatus_.user_status_type_id, userstatus_.user_status_name as user2_3_ from USER_STATUS_TYPE userstatus_ where userstatus_.user_status_type_id=?
Hibernate: insert into USER (created, first_name, last_name, password, user_name, user_status_type_id) values (?, ?, ?, ?, ?, ?)

I don’t know if that’s normal for Hibernate. I figured since it was a “persist” operation I would only see an Insert statement; not sure of the purpose for the Select.

Is the mapping incorrect?

  • 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-20T13:04:34+00:00Added an answer on May 20, 2026 at 1:04 pm

    OK, I was able to get an answer from the Hibernate forums. To prevent the SELECT before INSERT, if you are sure the referenced row exists in the database, then use Session.load() or EntityManager.getReference().

    User u = new User();
    u.setUserStatusType( session.load(UserStatusType.class, new Long(id));
    session.persist(u);
    

    The session.load() method doesn’t initially load anything, it just creates a proxy for the object and avoids a hit to the database. Now, if you want to access a member (other than the id field) from the class within the context of the session, then Hibernate will go to the database. In my case I just needed the primary key from the referenced object for the INSERT, so I avoided the database hit.

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

Sidebar

Related Questions

I am new to SoapUI and have just configured a very simple MockService. Is
I must be missing something simple, but I can't see it. First, the setup:
I've configured it so my simple unit test passes. But nosexunit.xml is reporting on
I'm sure the solution is simple, but it evades me. A table is configured
I was trying to configure spring 3.1 and hibernate. I have written a simple
I configured Second Level Cache using Ehcache in hibernate.cfg.xml ,ehcache.xml.And setting the cache-usage property
just simple question, recently i have setup a web server to build project using
I'm probably missing something simple, but I'm tired of fighting with it. There seem
I have a simple console application using MassTransit. My bus is configured to use
I have a simple windows service, that might need to be configured on-the-fly (a

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.