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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:46:14+00:00 2026-06-11T09:46:14+00:00

I have a many to many (with a join table) relationship in my database

  • 0

I have a many to many (with a join table) relationship in my database that I need to model in hibernate.

I have POJO classes for all three tables in the database as the join table contains more than just the mapping information.

My problem occurs on save() of class A where class A is told to cascade to class AB (the join table) which is told to cascade to class B, but when it gets to the point where it is saving AB the foriegn key reference to table A is null.

At the end of the day I want to be able to call save() on an instance of the TableA class and have it save all three tables.

Table Diagram

TABLE A Mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 27, 2012 2:16:37 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.example.dbtables.TableA" table="Table A">
        <id name="id" type="int">
            <column name="id" />
            <generator class="sequence">
                <param name="sequence">table_a_seq</param>
            </generator>
        </id>
        <natural-id>
        <property name="data1" type="int">
            <column name="DATA1" not-null="true" />
        </property>
        <property name="data2" type="string">
            <column name="DATA2" length="16" not-null="true" />
        </property>
        </natural-id>
        <set name="tableAB" cascade="save-update" table="TableAB" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="Table_AB_ID" not-null="true" />
            </key>
            <one-to-many class="com.example.dbtables.TableAB" />
        </set>
    </class>
</hibernate-mapping>

TABLE AB Mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 27, 2012 2:16:37 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.example.dbtables.TableAB" table="Table AB">
        <id name="id" type="int">
            <column name="id" />
            <generator class="sequence">
                <param name="sequence">table_ab_seq</param>
            </generator>
        </id>
        <natural-id>
        <many-to-one name="tableA" class="com.example.dbtables.TableA" fetch="select">
            <column name="Table_A_ID" not-null="true" />
        </many-to-one>
        <many-to-one name="tableB" cascade="save-update" class="com.example.dbtables.TableB" fetch="select">
            <column name="Table_B_ID" not-null="true" />
        </many-to-one>
        <property name="data1" type="int">
            <column name="DATA1" not-null="true" />
        </property>
            <property name="data2" type="int">
            <column name="DATA2" not-null="true" />
        </property>
        </natural-id>
    </class>
</hibernate-mapping>

TABLE B Mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 27, 2012 2:16:37 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.examples.dbtables.TableB" table="Table B">
        <id name="id" type="int">
            <column name="id" />
            <generator class="sequence">
                <param name="sequence">table_b_seq</param>
            </generator>
        </id>
        <natural-id>
        <property name="data1" type="string">
            <column name="DATA1" length="16" />
        </property>
        <property name="data2" type="java.lang.Integer">
            <column name="DATA2" />
        </property>
        </natural-id>
        <set name="TableAB" table="Table AB" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="Table_AB_ID" not-null="true" />
            </key>
            <one-to-many class="com.example.dbtables.TableAB" />
        </set>
    </class>
</hibernate-mapping>

Thanks for any help you can give. Let me know if you need clarification or anymore information.

EDIT: My problem was that the references from AB -> to A were not getting correctly populated. This issue has been resolved thanks to everyone who helped.

  • 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-06-11T09:46:15+00:00Added an answer on June 11, 2026 at 9:46 am
      A a = new A();
      B b = new B();
      AB ab = new AB();
      ab.setA(a);
      ab.setB(b);
      a.setAB(ab);
      b.setAB(ab);
    
      save(a);  
    

    This example not working? Did you get some exception?
    EDIT
    Look into this Hibernate doc Chapter 5. Basic O/R Mapping to chapter 5.1.4.1. Generator
    PostgreSQL doesn’t support identity generation type.
    Use sequence generation instead of identity, or something else

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

Sidebar

Related Questions

I have two database tables that have a many to many relationship: People and
I have a many-to-many relationship between 2 tables that I define with a join
I have two classes with many-to-many relation so I created a join table in-between
If I have two objects that have a many-to-many relationship, I would typically model
I have 1 table in my database ( instance_main ) that I need to
I have an intermediate table that defines many-to-many relationship between, for instance, Customer and
My database contains 3 tables: User and Service entities have many-to-many relationship and are
I have the following query with many LEFT JOIN clauses that has 7 result
I have many PowerPoint presentations that I need to be able to add to
I have two entities that have a many-to-many relationship; they are mapped with annotations

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.