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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:40:57+00:00 2026-06-13T05:40:57+00:00

I have a problem generating a mysql schema with hibernate. I’ve searched for an

  • 0

I have a problem generating a mysql schema with hibernate. I’ve searched for an answer to my problem but I haven’t found it (many similar problems but not exactly mine).

I need to map a State diagram into mysql (or transition table), and delete the states in cascade. A State may have many States as children (many to many relationship). I succeeded in mapping the State class and the “next state” relationship, but not in generating the correct SQL option ON DELETE CASCADE.

The Java class is as follows:

public class State {
private Integer version;
private Integer oid;
private String name;
private Map<String, State> nextStates;

public State(String name) {
    super();
    this.name = name;
    this.nextStates = new HashMap<String, State>();
}

/**
 * Adds some state to the transitions table. 
 * The table is indexed by states names.
 * 
 */
public void addNextState(State s) {
    this.nextStates.put(s.getName(), s);
}
(...)

The hibernate configuration is :

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/testStatesDB</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">pass</property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>     
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <property name="hibernate.hbm2ddl.auto">create-drop</property>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>       
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <mapping resource="State.hbm.xml" />
</session-factory>
</hibernate-configuration>

and the State mapping is as follows:

<hibernate-mapping>
<class name="State" table="state" optimistic-lock="version">
    <id name="oid" column="OID">
        <generator class="increment" />
    </id>
    <version column="version" name="version" unsaved-value="null"/>     

    <property name="name" type="java.lang.String"/>

    <map name="nextStates" table="nextStates" cascade="delete" optimistic-lock="true"> 
            <key column="parentState_oid" /> 
            <index column="children_stateName" type="java.lang.String"/> 
            <many-to-many column="children_oid" class="State" /> 
    </map>
</class>
</hibernate-mapping>

which creates the following schema:

Hibernate: 
create table nextStates (
    parentState_oid integer not null,
    children_oid integer not null,
    children_stateName varchar(255) not null,
    primary key (parentState_oid, children_stateName)
) ENGINE=InnoDB

Hibernate: 
create table state (
    OID integer not null,
    version integer not null,
    name varchar(255),
    primary key (OID)
) ENGINE=InnoDB

Hibernate: 
alter table nextStates 
    add index FKB4A102D5587BDC23 (parentState_oid), 
    add constraint FKB4A102D5587BDC23 
    foreign key (parentState_oid) 
    references state (OID)

Hibernate: 
alter table nextStates 
    add index FKB4A102D5F71F7FB (children_oid), 
    add constraint FKB4A102D5F71F7FB 
    foreign key (children_oid) 
    references state (OID)

As an example, consider running the following test code,

State pend   = new State("Pending");
State dev   = new State("In development");
State finali = new State("Finalized");

Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();

pend.addNextState(dev);
dev.addNextState(finali);
dev.addNextState(pend);

session.save(pend);
session.save(dev);
session.save(finali);
session.getTransaction().commit();

session.beginTransaction();
dev = (State)session.load(State.class, 3);
session.delete(dev);
session.getTransaction().commit();

I get the following error message:

Cannot delete or update a parent row: a foreign key constraint fails (`testStatesDB`.`nextstates`, CONSTRAINT `FKB4A102D5F71F7FB` FOREIGN KEY (`children_oid`) REFERENCES `state` (`OID`))

and if I go to the database and add the property ON DELETE CASCADE to the generated foreign keys, the example works as I need.

The question is: Is this the correct way of mapping a many to many relationship using HashMaps? And if it is correct, how do I generate the ON DELETE CASCADE option with an hibernate xml mapping?

I’ve been dealing with this problem for a long time, but I didn’t find a good solution.

  • 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-13T05:40:58+00:00Added an answer on June 13, 2026 at 5:40 am

    I’ve partially solved the problem: I used a bidirectional relationship between classes State and State (yes, recursive relationship worked). In each State I have a map for the next states in the graph and another for the parent states.

    The delete=”cascade” option didn’t generate the ON DELETE CASCADE as I would have desired, but, as long as I keep the collections inside the states synchronized, I can delete in cascade. By “synchronized” I mean that if a state S1 has S2 as next state, then if you delete the next state S2 in S1, you also have to let S2 know that S1 is no longer its parent.

    I hope this help someone… I got stucked for a long time with this problem.

    Thank you.
    Ignacio

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

Sidebar

Related Questions

I have problem SIMILAR to preventing form data reposting, but not quite the same
please I have the same problem as I found here MySQL - Selecting data
I have a problem with generating an XML that needs to be generated automatically
I have a problem with random number generating in C# I have tried all
I'm generating dynamic HTML inside Delphi. I have no problem displaying UTF-8 strings inside
I am having a problem with Hibernate generating invalid SQL. Specifically, mixing and matching
I'm more a mysql person, but I have to do a db in pg
I'm using Hibernate/JPA and have an @Entity object called Order, pointing at a MySQL
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This

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.