Delete all previous records and insert new ones

Posted by carlos on Stack Overflow See other posts from Stack Overflow or by carlos
Published on 2014-06-13T08:50:13Z Indexed on 2014/06/13 9:25 UTC
Read the original article Hit count: 195

Filed under:
|

When updating an employee with id = 1 for example, what is the best way to delete all previous records in the table certificate for this employee_id and insert the new ones?.

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

create table CERTIFICATE (
   id INT NOT NULL auto_increment,
   certificate_name VARCHAR(30) default NULL,
   employee_id INT default NULL,
   PRIMARY KEY (id)
);

Hibernate mapping

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="Employee" table="EMPLOYEE">
      <id name="id" type="int" column="id">
        <generator class="sequence">
            <param name="sequence">employee_seq</param>
        </generator>
      </id>
      <set name="certificates" lazy="false" cascade="all">
         <key column="employee_id" not-null="true"/>
         <one-to-many class="Certificate"/>
      </set>
      <property name="firstName" column="first_name"/>
      <property name="lastName" column="last_name"/>
      <property name="salary" column="salary"/>
   </class>

   <class name="Certificate" table="CERTIFICATE">
      <id name="id" type="int" column="id">
         <param name="sequence">certificate_seq</param>
      </id>
      <property name="employee_id" column="employee_id" insert="false" update="false"/>
      <property name="name" column="certificate_name"/>
   </class>

</hibernate-mapping>

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate