我有一个包含证书类列表的员工类。当我将员工类保存到数据库时,休眠将重复元素插入数据库。以下是我的映射文件详细信息:
<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>
我使用以下代码将 Employee 对象添加到数据库:
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));
但是当我在证书表中选择查询时,我得到以下结果:
+------+--------------------+--------+--------------+
| id | certificate_name | idx | employee_id |
+------+--------------------+--------+--------------+
| 81 | MCA | 1| 164|
+------+--------------------+--------+--------------+
| 82 | MBA | 4| 164|
+------+--------------------+--------+--------------+
| 83 | PMP | 3| 164|
+------+--------------------+--------+--------------+