-->
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.  [ 6 posts ] 
Author Message
 Post subject: Missing unique constraint for many-to-many with unique=true?
PostPosted: Tue Sep 13, 2016 4:21 am 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
Please consider the following HBM mapping and Java class:

Code:
<?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 package="org.hibernate.test" default-access="field">
   <class entity-name="TestWorkflowItem" name="WorkflowItem2" table="test_workflowitem">
      <id name="id" type="long">
         <generator class="native"/>
      </id>      
      <property name="name" column="name" not-null="true" type="string"/>
       
        <list name="childs" table="test_workflowitem_childs" cascade="all" access="field" lazy="false">
            <key column="item" foreign-key="fk_workflowitem"/>
            <index column="display_order"/>         
            <many-to-many column="child" entity-name="TestWorkflowItem" unique="true" foreign-key="fk_workflowitem_childs" lazy="false"/>                 
        </list>
   </class>
</hibernate-mapping>


Code:
package org.hibernate.test;

import java.util.List;

public final class WorkflowItem2 {
    private static final long serialVersionUID = 1L;

    private Long id;

    private String name;
    private List<WorkflowItem2> childs;
    public WorkflowItem2() {
    }

    public void setChilds(List<WorkflowItem2> newChilds) {
        childs = newChilds;
    }

    public List<WorkflowItem2> getChilds() {
        return childs;
    }
    public String getName() {
        return name;
    }
    public void setName(String newName) {
        name = newName;
    }
}


The WorkflowItem contains a list mapping with a unique many-to-many element.

Hibernate 4.3.11 generates the following table statements:

Code:
    create table test_workflowitem (
        id bigint not null,
        name varchar(255) not null,
        primary key (id)
    )
    create table test_workflowitem_childs (
        item bigint not null,
        child bigint not null,
        display_order integer not null,
        primary key (item, display_order)
    )
    alter table test_workflowitem_childs
        add constraint UK_ojsoe2oi4ydfh13jnq8cv1k3 unique (child)

    alter table test_workflowitem_childs
        add constraint fk_workflowitem_childs
        foreign key (child)
        references test_workflowitem

    alter table test_workflowitem_childs
        add constraint fk_workflowitem
        foreign key (item)
        references test_workflowitem


Hibernate 5.0.10 and 5.2.2 generate the following:
Code:
    create table test_workflowitem (
        id bigint not null,
        name varchar(255) not null,
        primary key (id)
    )
    create table test_workflowitem_childs (
        item bigint not null,
        display_order integer not null,
        child bigint not null,
        primary key (item, display_order)
    )
    alter table test_workflowitem_childs
        add constraint fk_workflowitem_childs
        foreign key (child)
        references test_workflowitem

    alter table test_workflowitem_childs
        add constraint fk_workflowitem
        foreign key (item)
        references test_workflowitem

As you see, the unique constraint for the column 'child' is not generated anymore with Hibernate 5.
Is this intentional or a bug?

Thanks,
Holger


Top
 Profile  
 
 Post subject: Re: Missing unique constraint for many-to-many with unique=true?
PostPosted: Tue Sep 13, 2016 4:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Isn't a one-to-many association more suitable in this case?


Top
 Profile  
 
 Post subject: Re: Missing unique constraint for many-to-many with unique=true?
PostPosted: Tue Sep 13, 2016 5:39 am 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
No, the additional join table should be preserved. (I'm migrating an existing application from Hibernate 3 to 5)

This also doesn't really answer my questions ;-)

Thanks for your time.

Holger


Top
 Profile  
 
 Post subject: Re: Missing unique constraint for many-to-many with unique=true?
PostPosted: Tue Sep 13, 2016 5:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can open a Jira issue for this. Anyway, as I already mentioned, annotations have better support than HBM in recent versions of Hibernate.
You might want to consider migrating to annotations as well. In a future version of Hibernate, HBM support might be even dropped in favor for the JPA XML mappings + some extension mechanism.


Top
 Profile  
 
 Post subject: Re: Missing unique constraint for many-to-many with unique=true?
PostPosted: Wed Sep 14, 2016 2:48 am 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
Ok, here is the issue:
https://hibernate.atlassian.net/browse/HHH-11101

Migrating big "legacy" apps is not trivial.
Migrating to a new Hibernate version and at the same time migrating from native mappings to annotations seems to be very challenging.
So we decided for a multi-step process.


Top
 Profile  
 
 Post subject: Re: Missing unique constraint for many-to-many with unique=true?
PostPosted: Wed Sep 14, 2016 3:02 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Doing the migration in multiple smaller steps is the way to go.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.