Hello,
I'm new to hibernate. I'm working with XML to Database mapping rather than POJO to database. My database is MySql5.
I have 2 tables, parent and child.
My hibernate configuration file is the following:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">login</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="parent.hbm.xml"/>
</session-factory>
</hibernate-configuration>
parent.hbm.xml looks like this:
<?xml version="1.0" ?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="Parent" table="parent" node="parent">
<id name="id_p" column="id_p" node="@id_p" type="int"/>
<property name="name" column="name" node="name" type="string"/>
</class>
<class entity-name="Child" table="child" node="child">
<id name="id_c" node="@id_c" column="id_c" type="int"/>
<property name="name" column="name" node="name" type="string"/>
<many-to-one name="fk_c_p" column="id_p" node="@id_p" entity-name="Parent" embed-xml="false"/>
</class>
</hibernate-mapping>
I want that when I delete a parent, all its children are deleted (on delete cascade).
This can be done when I execute the following sql command
alter table child add foreign key (id_p) references parent (id_p) on delete cascade;
but I want this to be done with the hibernate config file and not an SQL command.
I tried different solutions but can't succeed.
Can anyone help me please?
|