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?

2
  • 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. Commented May 10, 2012 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. Commented May 10, 2012 at 12:10

4 Answers 4

1

Well just add this to your mapping

<properties name="firstName_lastName_unique" unique="true">
        <property name="firstName"/>
        <property name="lastName"/>
</properties>
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

0

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.