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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:55:57+00:00 2026-05-17T21:55:57+00:00

I want to save some Objects to database with predefined IDs using Hibernate. Is

  • 0

I want to save some Objects to database with predefined IDs using Hibernate. Is it possible to do it using save method of Hibernate session?

I know there are following workarounds:

1) execute SQL script with necessary insert statements:

insert into MyObj(id,name) values (100,'aaa'), (101,'bbb');

2) use SQL query in Hibernate:

public static boolean createObj(Long id, String name) {
  Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  if (session.get(MyObj.class, id) == null) {        
    session.beginTransaction();
    SQLQuery sqlQuery = session.createSQLQuery
      ("insert into myobj(id,name) values(?,?)");
    sqlQuery.setLong(0, id);
    sqlQuery.setString(1, name);
    sqlQuery.executeUpdate();
    session.getTransaction().commit();
    return true;
  } else {
    return false;
  }
}

But: is it possible to do without SQLQuery?

In Hibernate reference in section 11.2. Making objects persistent there is example of code:

Alternatively, you can assign the
identifier using an overloaded version
of save().

DomesticCat pk = new DomesticCat();
pk.setColor(Color.TABBY);
pk.setSex('F');
pk.setName("PK");
pk.setKittens( new HashSet() );
pk.addKitten(fritz);
sess.save( pk, new Long(1234) );

But I couldn’t find example of how to do this.

So, is it possible to save new objects with supplied IDs to database with Hibernate not using SQL query? If yes then how?
And how to overload session method save() as mentioned in Hibernate reference?

Thanks!

  • 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-17T21:55:58+00:00Added an answer on May 17, 2026 at 9:55 pm

    The documentation asks you not to override save yourself, but to use the variant of save which takes two arguments. See the [JavaDocs][1] for more details.

    YourEntity entity = // ..
    entity.setFoo(foo);
    entity.setBar(bar);
    //..
    session.save(entity, theIDYouWantToUse);
    

    [1]: http://docs.jboss.org/hibernate/core/3.6/javadocs/org/hibernate/Session.html#save(java.lang.String, java.lang.Object)

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

Sidebar

Related Questions

i've got some binary data which i want to save as an image. When
I want to save the objects I generated in a program. After restart the
I want to save and store simple mail objects via serializing, but I get
Every change of data in some row in database should save the previous row
I am using an ObservableCollection to wrap some of my generated entity framework objects.
I want to open a save file dialog, have the user enter a filename,
Want to know what the stackoverflow community feels about the various free and non-free
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I'm witnessing some strange behaviour from Fluent nHibernate. I have two objects involved in

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.