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 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

I have a hibernate mapping as follows: <hibernate-mapping> <class name=kochman.elie.data.types.InvoiceTVO table=INVOICE> <id name=id column=ID>
I have a relation mapping table like this: attributeid bigint productid bigint To clean
I have a problem with my hibernate mapping using EmbeddedId. My code looks like
Using NHibernate.Mapping.Attributes, I have a entity class with something like: [Class] public class EntityA
I have the following Hibernate Mapping, which has to be mapped using the Table
I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
I'm considering using Annotations to define my Hibernate mappings but have run into a
NHibernate is driving me 'Nuts'!!! I have the following mapping. Which returns this error
I have a mapping application that needs to draw a path, and then display
In C# I have a use case where I have a mapping from int

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.