-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to specify UNIQUE key in hibernate mappings
PostPosted: Tue Dec 04, 2007 2:41 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
In a Person class, i have id property for natural id which is primary key. Then i have two more fields, subject and year. How to specify both of them as UNIQUE key. So in database, combination of (subject, year) should be unique and id is the primary key.

class Person{
private Long id;
private String subject;
private int year;
}


I am looking for simple solution instead of writing a PersonId class for composite key.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 4:48 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
As annotations:
Code:
@Entity
@Table(name="person",
uniqueConstraints = {@UniqueConstraint(columnNames={"subject", "year"})}
public class Person ...


As xml:
Code:
<properties name="subjectYearCombo_just_a_logical_name" unique="true">
        <property name="subject"/>
        <property name="year"/>
</properties>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 04, 2007 5:56 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Mike,
Thanks for your reply. It has partially solved my problem. Here is my actual POJO:

class Person{
private Long id;
private Subject subject; //subject
private Batch batch; //student batch
//other properties
}

I want to make Subject, Batch as UNIQUE key. Also I am using xdoclet2 to genernate the hbm.

In Person.hbm.xml as the Subject and Batch has many-to-one relation, there is a properties-name tag. But when I add, all the properties of the Person classes are added between properties tag.

So I am adding my <properties> tag after generating the hbm.
Do you have any idea how to fix this problem in xdoclet2?

Thanks
Shyam


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 2:56 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Saw the following on the net... should work for you.
Code:
/**
* @hibernate.class table="TableName" schema="schemaName"
* @hibernate.properties name="unique_name" unique="true"
*/
public class ClassName
{
    private Integer id;

    private String prop1;

    private String prop2;;

    /**
     * @hibernate.property column="column1" not-null="true"      properties-name="unique_name"
     */
    public String getProp1()
    {
        return this.prop1;
    }

    /**
     * @hibernate.property column="column2" not-null="true"      properties-name="unique_name"
     */
    public String getProp2()
    {
        return this.prop2;
    }

    /**
     * @hibernate.id column="id" generator-class = "assigned"
     */
    public Integer getId()
    {
        return this.id;
    }

    // ... setters...
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 11:12 am 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
Mike,
I have already tried that solution with XDoclet. And my xdoclet parser is generating a invalid hbm file. Then I referred to hibernate-mapping-3.0.dtd
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd
It has the following defination for properties
*******************************************
<!-- properties declares that the contained properties form an alternate key. The name
attribute allows an alternate key to be used as the target of a property-ref. -->

<!ELEMENT properties (
(property|many-to-one|component|dynamic-component)*
)>
<!ATTLIST properties name CDATA #REQUIRED>
<!ATTLIST properties unique (true|false) "false">
<!ATTLIST properties insert (true|false) "true">
<!ATTLIST properties update (true|false) "true">
<!ATTLIST properties optimistic-lock (true|false) "true">
<!ATTLIST properties node CDATA #IMPLIED>
**************************************************

And here what I have to write in Person.hbm.xml

<properties name="subject_batch_unique" unique="true">
<many-to-one
name="subject"
not-null="true"
cascade="save-update"
lazy="false"
>
</many-to-one>

<many-to-one
name="batch"
not-null="true"
>
</properties>

So after generating my hbm using xdoclet, I have to add the above text for properties. I wish xdoclet could have parsed this properly.

P.S. It is always better to refer to the hibernate-mapping-xxx.xx.dtd to resolve the issues. We can't trust the tools like xdoclet as it not fully mature and well developed.

Thanks
Shyam


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.