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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:53:08+00:00 2026-05-28T07:53:08+00:00

We are using Hibernate as persistency layer and have complex object model. Without exposing

  • 0

We are using Hibernate as persistency layer and have complex object model. Without exposing the real data model I want to explain the problem using the following simple example.

class Person {
    private Integer id; //PK
    private String name;
    private Account account;
    // other data, setters, getters
}


class Account {
    private Integer id; //PK
    // other data, setters, getters
}

The DB mapping is defined using HBM as following:

 <class name="Person" table="PERSON">
    <id name="id" column="ID">
        <generator class="native"/>
    </id>
    <version name="version" type="java.lang.Long"/>
    <property name="name" type="java.lang.String" length="50" column="NAME"/>
    <many-to-one name="account" column="ACCOUNT_ID"
                class="com.mycompany.model.Account"/>

</class>

I have to save new populated instance of Person linked to existing Account. The call is originated by web client, so at my layer I get instance of Person referenced to instance of Account that holds its ID only.

If I try to call saveOrUpdate(person) the following exception is thrown:

org.hibernate.TransientObjectException: 
object references an unsaved transient instance - save the transient instance before flushing: 
com.mycompany.model.Account

To avoid this I have to find the persisted object of Account by ID and then call person.setAccount(persistedAccount). In this case everything works fine.

But in real life I deal with dozens of entities referenced to each other. I do not want to write special code for each reference.

I wonder whether there is some kind of generic solution for 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-28T07:53:09+00:00Added an answer on May 28, 2026 at 7:53 am

    To persist one entity, you just need to have the references to its direct dependencies. The fact that these other entities reference other entities doesn’t matter.

    The best way to do it is to get a proxy to the referenced entity, without even hitting the database, using session.load(Account.class, accountId).

    What you’re doing is the right thing to do: get a reference to the persistent account, and set this reference into the newly created account.

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

Sidebar

Related Questions

I'm using NHibernate as a persistency layer and I have many places in my
I am using hibernate as a persistence layer. There are 2 entities that live
when using hibernate with spring, can someone explain how the session unit of work
I'm using JPA 2 with Hibernate 3.6.8 as the implementation. Let's say we have
I have a Java app using a MySQL database through hibernate. The database is
i am using the following approach to solve lazy initialization problem in hibernate. Please
i am using the following approach to sole lazy initialization problem in hibernate.Pleas tell
i'm developing an application using Wicket as the view layer and JPA(Hibernate) as ORM.
I have two table Student and Address.I am using hibernate 3 annotation for here.
Hi, I'm busy developing a Java EE app using Hibernate and Spring. I have

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.