facebook

unique-key constraints aren’t generated in hibernate rev-eng

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #245590 Reply

    Steve Prior
    Member

    MyEclipse 4.1GA. MySQL 5.0, latest connector and the following table:

    CREATE TABLE `tvepisode_rtab` (
    `id` int(11) NOT NULL auto_increment,
    `show_id` int(11) NOT NULL default ‘0’,
    `description_id` int(11) NOT NULL default ‘0’,
    `subtitle` varchar(255) default NULL,
    `release_date` varchar(10) default NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `show_id` (`show_id`,`subtitle`,`release_date`,`description_id`),
    KEY `episode_description` (`description_id`),
    CONSTRAINT `episode_description` FOREIGN KEY (`description_id`) REFERENCES `tv description_rtab` (`id`),
    CONSTRAINT `episode_show` FOREIGN KEY (`show_id`) REFERENCES `tvshow_rtab` (`i d`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1

    When I browse a MySQL 5.0 InnoDB table in the database explorer and then show create statement I get:

    create table `tv`.`tvepisode_rtab`(
    `id` int(11) PRIMARY KEY not null,
    `show_id` int(11) default ‘0’ not null,
    `description_id` int(11) default ‘0’ not null,
    `subtitle` varchar(255),
    `release_date` varchar(10))

    When I reverse engineer a hibernate mapping the foreign keys are correctly represented, but the
    UNIQUE KEY `show_id` (`show_id`,`subtitle`,`release_date`,`description_id`) is not – I
    believe that unique-key=”show_id” attributes should be showing up in the generated xml file.

    #245623 Reply

    Brian Fernandes
    Moderator

    Sprior,

    Could you save us some time and explain exactly how you would like the generated mapping file to be modified to reflect the unique-key attribute?

    Thanks,
    Brian.

    #245712 Reply

    Steve Prior
    Member

    I’m just a beginner at Hibernate so there may be better advice out there, but here is an example I just confirmed.

    Notice the unique key in the following table creation:

    CREATE TABLE `attendees` (
    `uid` bigint(20) NOT NULL auto_increment,
    `firstName` varchar(20) default NULL,
    `lastName` varchar(20) default NULL,
    PRIMARY KEY (`uid`),
    UNIQUE KEY `firstName` (`firstName`,`lastName`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

    This was created by the following hibernate mapping file and
    schema-export:

    <hibernate-mapping package=”com.manning.hq.ch03″>
    <class name=”Attendee” table=”attendees”>
    <id name=”id” column=”uid” type=”long”>
    <generator class=”native”/>
    </id>
    <property name=”firstName” type=”string” length=”20″ unique-key=”foo”/>
    <property name=”lastName” type=”string” length=”20″ unique-key=”foo”/>
    </class>
    </hibernate-mapping>

    So the identifier foo doesn’t seem to actually show up in the end, it’s just an
    identifier that binds together firstName and lastName into a joint unique key constraint.

    So you’ve got a chicken and the egg situation – If you start with that hibernate mapping
    file you get the expected unique key constraint in the table, so if you did a reverse engineer
    ona table which had such a unique key constraint then I’d expect the reverse engineer on that
    table to produce that mapping file (except the identifier foo can be something random).

    Again, I might be missing something – the documentation on the unique-key directive in
    hibernate is REALLY scant – google searches don’t even produce much.

    Does that help?

    #246077 Reply

    Haris Peco
    Member

    unique-key have wrong definition in hibernate – unique-key is feature of class/table, no property
    you can have two unique-keys for same property/column (for example property a,b,c and unique-keys (a,b) and
    (b,c) – it is impossible for implementation – in ejb 3 (hibernate annotations) you have unique constraint annotation and this is correct definition.
    I think that bad documentation for unique-key is because unique-ke’s definition is wrong and it is used a few
    However, when you have primary key/hiebrnate id unique key is important on database level only

    #246086 Reply

    Steve Prior
    Member

    I’m sorry, but I didn’t understand a fair amount of what you wrote. What I was looking for is a way to specify that for the attendees example above that in addition to an attendee having a uniqie primary key, they are also unique based on the combination of first and last name (slightly cheesy example, but lets pretend fore the moment that there is only ONE “Johnny Carson”. In Oracle this would be added with a UNIQUE constraint on the table. I don’t know if this is actually true or not, but I would hope that Hibernate has a different exception that gets thrown if you try to save a second object which violates the uniqueness constraint from the exception thrown if for example you tried to save a null in a non null column.

    #246088 Reply

    Haris Peco
    Member

    hibernate doesn’t use unique key – it use only for schema export utility, but it isn’t good definition and it use nobody
    (except in annotations)
    it isn’t important who throw exception: hibernate or database, but it is better do on database level – hibernate can be used in client-server mode (more JVMs) and then hibernate can’t trown exception when key exists in two JVMs (two clients)
    unique-key is defined on level property in hibernate – however it is table level concept – you define unique key for table, no for column in relational database – therefore it isn’t used too much and there isn’t documentation for this feature.i try explain example
    table with column a.b and c can have unique-keys
    1. column a, b
    2. column b,c

    this is imposible define in hibernate – you can add two unique_key attributes for b property
    However – it is better that unique keys and other integrity rules be in database

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: unique-key constraints aren’t generated in hibernate rev-eng

You must be logged in to post in the forum log in