Rasoul Taheri
Rasoul Taheri

Reputation: 822

why hibernate does not insert repititive value of ArrayList in database table

i have a employee class that conaint a List from Certificate class. when i save employee class to the database hibernate pass up insert repititive element to database. following is my mapping file detail :

<hibernate-mapping package="ir.imrasta.hibernate.sample4">
<class name="Employee" table="employee">
    <meta attribute="class-description">
        This class contains employee detail.
    </meta>
    <id name="id" type="int" column="id" >
        <generator class="native" />
    </id>
    <list name="certificates" cascade="all"  lazy="false">
        <key column="employee_id" />
        <list-index column="idx" />
        <one-to-many class="Certificate"/>
    </list>
    <property name="firstName" type="string" column="first_name" />
    <property name="lastName" type="string" column="last_name" />
    <property name="salary" type="int" column="salary" />
</class>

<class name="Certificate" table="list_certificate">
    <meta attribute="class-description">
        This class contains certificate records.
    </meta>
    <id name="id" type="int" column="id" >
        <generator class="native" />
    </id>
    <property name="name" type="string" column="certificate_name" />
</class>

i use following code for add Employee object to database:

ManageEmployee me=new ManageEmployee();
List<Certificate> lst1=new ArrayList<Certificate>();
lst1.add(new Certificate("MCA"));
Certificate a=new Certificate("MBA");
lst1.add(a);
lst1.add(new Certificate("PMP"));
lst1.add(a);
int id1=me.addEmployee(new Employee("Ali","Habibi",200,lst1));  

but when i do select query in certificate table, i get following result:

+------+--------------------+--------+--------------+  
|  id  | certificate_name   |  idx   | employee_id  |  
+------+--------------------+--------+--------------+  
|  81  | MCA                |       1|           164|  
+------+--------------------+--------+--------------+  
|  82  | MBA                |       4|           164|  
+------+--------------------+--------+--------------+  
|  83  | PMP                |       3|           164|  
+------+--------------------+--------+--------------+  

Upvotes: 0

Views: 616

Answers (1)

Angga
Angga

Reputation: 2323

that is because you add multiple time with the same object like this :

Certificate a=new Certificate("MBA");
lst1.add(a);
lst1.add(new Certificate("PMP"));
lst1.add(a);

if you want MBA to be add 2 times you need to insert it like this :

lst1.add(new Certificate("MBA"));
lst1.add(new Certificate("PMP"));
lst1.add(new Certificate("MBA"));

when you save same value to list:

List l = new ArrayList();
Integer a = new Integer(1);
l.add(a);
l.add(a);
l.add(new Integer(1));
l.add(new Integer(1));

l.get(0) and l.get(1) is the same object(have 1 object id), but l.get(2) and l.get(3) is a different object that have different id although have the same value. the object brought by this list is only 3, with 4 different door.

Upvotes: 3

Related Questions