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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:56:10+00:00 2026-06-12T19:56:10+00:00

I have three entities A, B, C. A has many Bs and a C,

  • 0

I have three entities A, B, C.

A has many Bs and a C, B has many Cs. I’d like to have bidirectional relationships.

I mapped all of this in Hibernate:

@Entity
    @Table(name="TABLE_A")
    class A{

        @Id
        @GeneratedValue
        private Integer a_id;

        @ManyToOne
        C c; 

        @OneToMany
        List<B> bs;
    }

    @Entity
    @Table(name="TABLE_B")
    class B{

        @Id
        @GeneratedValue
        private Integer b_id;

        @ManyToOne
        A a; // inverse

        @ManyToOne
        C c;
    }

    @Entity
    @Table(name="TABLE_C")
    class C{

        @Id
        @GeneratedValue
        private Integer c_id;

        @ManyToMany(mappedBy="c")
        List<B> b; // inverse

        @ManyToMany(mappedBy="c")
        List<A> a; //inverse

    }

I use the SchemaUpdate to get the tables generated in MySQL.
The problem is the following:

2012-08-24 14:57:15,437 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter table TABLE_A add index FKCE3C1BF0B8CF4F47 (a_id), add constraint FKCE3C1BF0B8CF4F47 foreign key (a_id) references TABLE_A (a_id)

A constraint is added to A to itself – This is probably due to the C->A relationship. What is wrong with the mapping?

I created a simple test file:

    @Test
        public void tests() throws Exception {
            Logger l = Logger.getLogger("org.hibernate.tool.hbm2ddl");
            l.

setLevel(Level.DEBUG);

        //Hibernate.enableCreationDebug();
        A a = new A();

        SessionFactory sf = configureSessionFactory();
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();
        try{

            session.save(a);
            tx.commit();
        }catch(Exception e){
            tx.rollback();
            throw e;
        }finally{
            session.close();
        }

        session = sf.openSession();
        tx = session.beginTransaction();
        try{

            session.delete(a);
            tx.commit();
        }catch(Exception e){
            tx.rollback();
            throw e;
        }finally{
            session.close();
        }

    }

private static SessionFactory configureSessionFactory() throws HibernateException {
        Configuration configuration = new Configuration();

        String DB_USER_NAME = "kiri";
        String DB_USER_PWD = "kiri";
        String JDBC_CONNECTION_STRING = "jdbc:mysql://localhost/kiri";

        configuration
                .addAnnotatedClass(HibernateCyclicTest.A.class)
                .addAnnotatedClass(HibernateCyclicTest.B.class)
                .addAnnotatedClass(HibernateCyclicTest.C.class)

                .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect")
                .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
                .setProperty("hibernate.connection.url", JDBC_CONNECTION_STRING)
                .setProperty("hibernate.connection.username", DB_USER_NAME)
                .setProperty("hibernate.connection.password", DB_USER_PWD);


        // Create or update the schema to match our objects
        org.hibernate.tool.hbm2ddl.SchemaUpdate schemaUpdate = new SchemaUpdate(configuration);
        schemaUpdate.execute(false,/* update schema */true);
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                .buildServiceRegistry();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
    }

Here is the output:

    2012-08-24 15:03:01,719 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000228: Running hbm2ddl schema update
2012-08-24 15:03:01,719 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000102: Fetching database metadata
2012-08-24 15:03:01,923 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000396: Updating schema
2012-08-24 15:03:02,025 [main] INFO  java.sql.DatabaseMetaData - HHH000262: Table not found: TABLE_A
2012-08-24 15:03:02,025 [main] INFO  java.sql.DatabaseMetaData - HHH000262: Table not found: TABLE_A_TABLE_B
2012-08-24 15:03:02,026 [main] INFO  java.sql.DatabaseMetaData - HHH000262: Table not found: TABLE_B
2012-08-24 15:03:02,026 [main] INFO  java.sql.DatabaseMetaData - HHH000262: Table not found: TABLE_C
2012-08-24 15:03:02,027 [main] INFO  java.sql.DatabaseMetaData - HHH000262: Table not found: TABLE_A
2012-08-24 15:03:02,027 [main] INFO  java.sql.DatabaseMetaData - HHH000262: Table not found: TABLE_A_TABLE_B
2012-08-24 15:03:02,028 [main] INFO  java.sql.DatabaseMetaData - HHH000262: Table not found: TABLE_B
2012-08-24 15:03:02,029 [main] INFO  java.sql.DatabaseMetaData - HHH000262: Table not found: TABLE_C
2012-08-24 15:03:02,030 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - create table TABLE_A (a_id integer not null auto_increment, c_c_id integer, primary key (a_id)) ENGINE=InnoDB
2012-08-24 15:03:02,051 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - create table TABLE_A_TABLE_B (TABLE_A_a_id integer not null, bs_b_id integer not null, unique (bs_b_id)) ENGINE=InnoDB
2012-08-24 15:03:02,062 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - create table TABLE_B (b_id integer not null auto_increment, a_a_id integer, c_c_id integer, primary key (b_id)) ENGINE=InnoDB
2012-08-24 15:03:02,070 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - create table TABLE_C (c_id integer not null auto_increment, primary key (c_id)) ENGINE=InnoDB
2012-08-24 15:03:02,078 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter table TABLE_A add index FKCE3C1BF0B8CF4F47 (a_id), add constraint FKCE3C1BF0B8CF4F47 foreign key (a_id) references TABLE_A (a_id)
2012-08-24 15:03:02,087 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter table TABLE_A add index FKCE3C1BF066FAB663 (c_c_id), add constraint FKCE3C1BF066FAB663 foreign key (c_c_id) references TABLE_C (c_id)
2012-08-24 15:03:02,097 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter table TABLE_A_TABLE_B add index FK64E4EEA2F8F999F6 (TABLE_A_a_id), add constraint FK64E4EEA2F8F999F6 foreign key (TABLE_A_a_id) references TABLE_A (a_id)
2012-08-24 15:03:02,107 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter table TABLE_A_TABLE_B add index FK64E4EEA2C26A7AD5 (bs_b_id), add constraint FK64E4EEA2C26A7AD5 foreign key (bs_b_id) references TABLE_B (b_id)
2012-08-24 15:03:02,123 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter table TABLE_B add index FKCE3C1BF1B8CFC3A7 (b_id), add constraint FKCE3C1BF1B8CFC3A7 foreign key (b_id) references TABLE_B (b_id)
2012-08-24 15:03:02,141 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter table TABLE_B add index FKCE3C1BF163901C65 (a_a_id), add constraint FKCE3C1BF163901C65 foreign key (a_a_id) references TABLE_A (a_id)
2012-08-24 15:03:02,151 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - alter table TABLE_B add index FKCE3C1BF166FAB663 (c_c_id), add constraint FKCE3C1BF166FAB663 foreign key (c_c_id) references TABLE_C (c_id)
2012-08-24 15:03:02,161 [main] INFO  org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000232: Schema update complete
2012-08-24 15:03:02,583 [main] WARN  org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1451, SQLState: 23000
2012-08-24 15:03:02,583 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot delete or update a parent row: a foreign key constraint fails (`kiri`.`table_a`, CONSTRAINT `FKCE3C1BF0B8CF4F47` FOREIGN KEY (`a_id`) REFERENCES `TABLE_A` (`a_id`))

Here is the stack:

    org.hibernate.exception.ConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`kiri`.`table_a`, CONSTRAINT `FKCE3C1BF0B8CF4F47` FOREIGN KEY (`a_id`) REFERENCES `TABLE_A` (`a_id`))
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at $Proxy13.executeUpdate(Unknown Source)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3134)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3337)
    at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:100)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:280)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    at com.kireego.tests.HibernateCyclicTest.tests(HibernateCyclicTest.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`kiri`.`table_a`, CONSTRAINT `FKCE3C1BF0B8CF4F47` FOREIGN KEY (`a_id`) REFERENCES `TABLE_A` (`a_id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2450)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2371)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2355)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
    ... 39 more

Thank you for the help.

  • 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-12T19:56:11+00:00Added an answer on June 12, 2026 at 7:56 pm

    I simply removed the mappedBy attribute, that created extra tables but fixed the problem.

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

Sidebar

Related Questions

I have an entity schedule. This schedule has some attributes like name, startdate, enddate,
I have three entities like this. Employee: id name EmployeeDepartment: id departmentID employeeID Department:
I have three entities: EntityA, EntityB and EntityC connected with to-many relationships. See schema
I have a case where i have three entities with one-to-many and one-to-many relationships:
I have 3 entities like this: sorry that this image has a problem that
I have two entities with many-to-many relationship defined on them. <set name=TreasuryCodes table=ServiceProviderAccountTreasuryCode lazy=true
I have three entities: Recipe , RecipeIngredient and Ingredient . Each Recipe has many
I have three entities: Country, State and City with the following relationships: When creating
We have the following two entities with many-to-many association: @Entity public class Role {
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]

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.