0

I have an hbm file which is as below:

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

<hibernate-mapping auto-import="true" default-lazy="false">
    <class name="com.saman.entity.hibernate.EmployeeEntity"
           table="Employee" optimistic-lock="version">
        <id name="id">
            <column name="Id" sql-type="bigint"/>
            <generator class="native"/>
        </id>
        <timestamp name="version" source="db"/>
        <property  name="firstName">
            <column name="FirstName" sql-type="nvarchar(300)"/>
        </property>
        <property name="lastName">
            <column name="LastName" sql-type="nvarchar(300)"/>
        </property>
        <property name="employeeType">
            <column name="EmployeeType" sql-type="nvarchar(300)"/>
        </property>
        <set name="shifts" table="Shifts" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="Id" not-null="true"/>
            </key>
            <one-to-many class="com.saman.entity.hibernate.ShiftEntity"/>
        </set>
    </class>
</hibernate-mapping>

now I wanted if I add an employee and persist it, if then I add another employee with the previous information, my system raises an exception and tell me that I have another employee in the database with that informations.

does hibernate give me this option?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
saman
  • 199
  • 4
  • 17
  • 1
    Do you want to find if all informations are same or part of it is enough. I mean you have an id column and it must be unique. If you try to add another person with same id hibernate should throw and exception. – mbaydar May 10 '12 at 12:05
  • i know we have id, but consider there is a person with name "alex" and family "peterson". i wanted to tell my system that prevents adding another body with these informations. – saman May 10 '12 at 12:10

4 Answers4

1

Well just add this to your mapping

<properties name="firstName_lastName_unique" unique="true">
        <property name="firstName"/>
        <property name="lastName"/>
</properties>
Sudhakar
  • 4,823
  • 2
  • 35
  • 42
0

I think I understand what you want to achieve. But I don't know if you search your problem in stackoverflow first. This might be your answer How to do multiple column UniqueConstraint in hbm?

Community
  • 1
  • 1
mbaydar
  • 1,144
  • 1
  • 13
  • 18
0

Have you set an auto increment on the ID column in your database?

shawsy
  • 457
  • 1
  • 6
  • 23
0

You already have a generator for the id value. This should generate a unique id, but it only does so if these two conditions are true:

  1. The column either is defined as autoincrement (p. ex. MySQL) or has a sequence (p. ex. Oracle)
  2. When saving a new row, the member variable id is set to 0.

I can imagine, when you save a new value with previous information, the variable id still has a value != 0, and in this case the database uses the given value instead of generating a new unique one, which will fail due to the unique constraint.

This error also can appear if there is a second unique index on the table.

Johanna
  • 5,223
  • 1
  • 21
  • 38