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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:00:14+00:00 2026-05-23T07:00:14+00:00

I have 3 tables, Parent , Child and ParentChild which joins the first two

  • 0

I have 3 tables, Parent, Child and ParentChild which joins the first two together. When doing an insert (Parent.getChilds().add(new Child());) I get an error because, apparently, the primary key hasn’t been created and I get a constraint violation:

java.sql.BatchUpdateException: ORA-02291: integrity constraint (TU.SYS_C0072908) violated - parent key not found

If I let my test case rollback, this works fine, I don’t get a constraint violation, and all of my assertions pass. If I set my test case to not roll back I get the above error. Also, if the Parent and Child were created outside of the current transaction then everything works peachy-keen. I.e.:

Parent parent = parentDao.get(parentId);
Child child = childDao.get(childId);
parent.getChilds().add(child);
child.setParent(parent);
parentDao.save(parent);

Mappings look like this:

Parent

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.whatev">
    <class name="Parent">
        <id name="parentId">
            <generator class="native"/>
        </id>
        <set name="childs" table="ParentChild" cascade="all">
            <key column="parentId"/>
            <many-to-many class="Child" column="childId" unique="true"/>
        </set>
    </class>
</hibernate-mapping>

Child

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.whatev">
    <class name="Child">
        <id name="childId">
            <generator class="native"/>
        </id>
        <join table="ParentChild" inverse="true">
            <key column="childId"/>
            <many-to-one name="Parent" column="parentId" not-null="true"/>
        </join>
    </class>
</hibernate-mapping>

A Parent can have more than one Child and a Child can have only one Parent. (It has been pointed out that there should be no join table and, instead, be a PARENTID column in the CHILD table but this design decision is not mine to make.)

Tables are pretty straight forward as well:

CREATE TABLE PARENT (
    PARENTID NUMBER(38) NOT NULL,
    PRIMARY KEY(PARENTID)
);

CREATE TABLE CHILD (
    CHILDID NUMBER(38) NOT NULL,
    PRIMARY KEY(CHILDID)
);

CREATE TABLE PARENTCHILD (
    PARENTID NUMBER(38) NOT NULL,
    CHILDID NUMBER(38) NOT NULL,
    FOREIGN KEY(PARENTID) REFERENCES PARENT(PARENTID),
    FOREIGN KEY(CHILDID) REFERENCES CHILD(CHILDID),
    UNIQUE (PARENTID, CHILDID)
);

The primary keys are populated by sequences through a trigger when the record is created.

What’s wrong with my setup?

  • 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-23T07:00:15+00:00Added an answer on May 23, 2026 at 7:00 am

    The issue is with the trigger applying the sequence nextval.

    By specifying native but not telling hibernate about the sequence, hibernate thinks it has to come up with the id itself since Oracle doesn’t have anything to natively create unique ids. Child.childId will have the nextval from the sequence (because of the trigger overwriting what hibernate comes up with) and ParentChild.childId gets the value that hibernate assigns, creating a constraint issue.

    To handle this in a cross-database-friendly way, either drop the trigger, keep the sequence and let Oracle handle the ids:

    <id name="childId">
        <generator class="native">
            <param name="sequence">CHILDID_SEQ</param>
        </generator>
    </id>
    

    Or drop the trigger and sequence and let hibernate assign the ids:

    <id name="childId">
        <generator class="native"/>
    </id>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables a parent and a child table. The child table has
I have two tables Loan(Parent Table) and Receipt(Child table)what i want to do is
I have the following sample of data to insert into tables(from parent to child,
I have a table with two child tables. For each record in the parent
I have 3 tables: parent child parentchild There is a many to many relationship
I have two DB tabels which form a parent child relationship from Planung to
Suppose I have two tables in a parent - child relationship. Let's call them
I have two tables with parent - child relationship: PARENT table with id as
I have two tables with a parent/child relationship. I want to update the parent
I have a simple parent/child tables. Contract is the parent table. Each contract can

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.