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

  • Home
  • SEARCH
  • 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 3931458
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:21:13+00:00 2026-05-19T23:21:13+00:00

I have 2 mappings like this: <hibernate-mapping> <class name=A table=A> <id name=code column=aCode type=integer>

  • 0

I have 2 mappings like this:

<hibernate-mapping>  
    <class name="A" table="A">  
        <id name="code" column="aCode" type="integer">  
            <generator class="assigned"/>  
        </id>  
        <property name="info" type="integer"/>  
        <set name="Bs" lazy="false">
            <key>
                <column name="aCode" not-null="true"/>
            </key>
            <one-to-many class="B"/>
        </set>
    </class>  
    <class name="B" table="B">  
        <id name="code" column="bCode" type="integer">  
             <generator class="assigned"/>  
        </id>  
        <many-to-one name="a"/>  
    </class>  
</hibernate-mapping>  

These are the classes:

public class A {  
    private int code;  
    private int info;  
    private Set<B> bs = new HashSet<B>(0);  
    public A() {};  
    public int getCode() { return code; }  
    public void setCode(int code) { this.code = code; }  
    public int getInfo() { return info; }  
    public void setInfo(int info) { this.info = info; }  
    public Set<B> getBs() { return bs; }  
    public void setBs(Set<B> bs) { this.bs = bs; }  
}

public class B {  
    private int code;  
    private A a;  
    public B() {};  
    public int getCode() { return code; }  
    public void setCode(int code) { this.code = code; }  
    public A getA() { return a; }  
    public void setA(A a) { this.a = a; }  
}  

I’m in a scenario where I have to deal with a long transition and do the following:

// Persistence Layer
Session session = factory.getCurrentSession();  
session.beginTransaction();  

A a1 = new A(); // Create transient object  
a1.setCode(1);  
a1.setInfo(10);  
session.save(a1); // Persist it  

// Something happening in another layer (see below)

// Continuing with the transaction
Object b = ... // Recover object
session.save(b); // Persist it using transient a2 object as a link but don't change/update its data

System.out.println(b.getA().getInfo()); // Returns 0 not 10;  
session.commit();  

This happens in another layer (don’t have access to the session here):

// * Begin in another layer of the application *  
A a2 = new A(); // Create another transient object same *code* as before  
a2.setCode(1);  
B b = new B(); // Create another transient object  
b.setCode(1);  
b.set(a2);  
// * End and send the b Object to the persistence layer *  

Is there any way load/get the persistent child object before saving parent one or is there other way to save the child object without change the info and flush it all? I’m not using JPA. Sorry if I’m terrible wrong.

Thank you.

  • 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-19T23:21:14+00:00Added an answer on May 19, 2026 at 11:21 pm

    Currently state of the new child is not saved into the database since your relationship doesn’t have cascading, so wrong state of the child shouldn’t be a big problem.

    However, if you want to have consistent state of the entities in memory, you can use merge() instead of save(), without cascading it should work exactly as desired:

    b = session.merge(b); // Persist it using transient a2 object as a link but don't change/update its data  
    System.out.println(b.getA().getInfo()); // Should return 10
    

    See also:

    • Hibernate: will merge work with many-to-one object transtively?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have a mapping like this @MappedSuperclass public abstract class Base
I have a spring app with one servlet that has url-mapping of *.html. Everything
I have a simple WAR project managed by Maven. I have the servlet code
There are a few posts about this, but after hours of searching I still
I know this question has been asked/answered but I can't find it for the
Let's assume we have the following generic scenario: An RDBMS as a data source,
I have a question concerning dependency injection. I have been keeping it simple so
When is Browse To Find source enabled in Visual Studio 2010? (see below) In
I need some advice on part of my model I am designing. As you
I'm writing an ORM and am unsure of the expected behaviour of the Repository,

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.