Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Teneo] Unique Column Constraint by EAnnotation
[Teneo] Unique Column Constraint by EAnnotation [message #424932] Tue, 11 November 2008 05:02 Go to next message
Ole Ersoy is currently offline Ole ErsoyFriend
Messages: 205
Registered: July 2009
Senior Member
Hi,

I was wondering whether it's possible to add to an EAttribute a EAnnotation specifying that the corresponding SQL table column should have a unique constraint added to it? I tried setting the EAttribute's unique property to true, but the corresponding column in the database did not end up with a unique constraint on it. Should it have?

Thanks,
- Ole
Re: [Teneo] Unique Column Constraint by EAnnotation [message #424933 is a reply to message #424932] Tue, 11 November 2008 06:15 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Ole,
There are two ways:
- for single-column unique constraints: the @Column annotation has a unique attribute (a boolean)
- for multi-column unique constraints there is the @UniqueConstraint annotation which has an array
of columnNames

gr. Martin

Ole Ersoy wrote:
> Hi,
>
> I was wondering whether it's possible to add to an EAttribute a
> EAnnotation specifying that the corresponding SQL table column should
> have a unique constraint added to it? I tried setting the EAttribute's
> unique property to true, but the corresponding column in the database
> did not end up with a unique constraint on it. Should it have?
>
> Thanks,
> - Ole


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] Unique Column Constraint by EAnnotation [message #424958 is a reply to message #424933] Tue, 11 November 2008 19:47 Go to previous messageGo to next message
Ole Ersoy is currently offline Ole ErsoyFriend
Messages: 205
Registered: July 2009
Senior Member
Hi Martin,

I tried it like this:

<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Column(unique=&quot;true&quot;)"/>
</eAnnotations>

However I'm still able to insert records that are duplicates per the EAttribute that the annotation is attached to. Any ideas on how to troubleshoot this?

Thanks,
- Ole



Martin Taal wrote:
> Hi Ole,
> There are two ways:
> - for single-column unique constraints: the @Column annotation has a
> unique attribute (a boolean)
> - for multi-column unique constraints there is the @UniqueConstraint
> annotation which has an array of columnNames
>
> gr. Martin
>
> Ole Ersoy wrote:
>> Hi,
>>
>> I was wondering whether it's possible to add to an EAttribute a
>> EAnnotation specifying that the corresponding SQL table column should
>> have a unique constraint added to it? I tried setting the
>> EAttribute's unique property to true, but the corresponding column in
>> the database did not end up with a unique constraint on it. Should it
>> have?
>>
>> Thanks,
>> - Ole
>
>
Re: [Teneo] Unique Column Constraint by EAnnotation [message #424961 is a reply to message #424958] Tue, 11 November 2008 23:38 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Ole,
I have a testcase which covers this and your annotation seems fine. Can you check that your
generated code contains the annotation? What is the hibernate mapping?
If you have the Teneo source code then you can place a breakpoint AbstractMapper.addColumn to see if
the unique attribute is applied.

gr. Martin

Ole Ersoy wrote:
> Hi Martin,
>
> I tried it like this:
>
> <eAnnotations source="teneo.jpa">
> <details key="appinfo" value="@Column(unique=&quot;true&quot;)"/>
> </eAnnotations>
>
> However I'm still able to insert records that are duplicates per the
> EAttribute that the annotation is attached to. Any ideas on how to
> troubleshoot this?
>
> Thanks,
> - Ole
>
>
>
> Martin Taal wrote:
>> Hi Ole,
>> There are two ways:
>> - for single-column unique constraints: the @Column annotation has a
>> unique attribute (a boolean)
>> - for multi-column unique constraints there is the @UniqueConstraint
>> annotation which has an array of columnNames
>>
>> gr. Martin
>>
>> Ole Ersoy wrote:
>>> Hi,
>>>
>>> I was wondering whether it's possible to add to an EAttribute a
>>> EAnnotation specifying that the corresponding SQL table column should
>>> have a unique constraint added to it? I tried setting the
>>> EAttribute's unique property to true, but the corresponding column in
>>> the database did not end up with a unique constraint on it. Should
>>> it have?
>>>
>>> Thanks,
>>> - Ole
>>
>>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] Unique Column Constraint by EAnnotation [message #424962 is a reply to message #424961] Wed, 12 November 2008 02:04 Go to previous messageGo to next message
Ole Ersoy is currently offline Ole ErsoyFriend
Messages: 205
Registered: July 2009
Senior Member
Hi Martin,

Wow - I just found the troubleshooting section on the elver site. Lots of good stuff in there :-).

I see the annotation generated in the interface like this:

* @model required="true"
* annotation="teneo.jpa appinfo='@Column(unique=\"true\")'"

I also printed the mapping from the test I'm running and it looks like it took the annotation:

============================================================ ======
<property
name="email"
lazy="false"
insert="true"
update="true"
not-null="false"
unique="true"
type="java.lang.String">
<column not-null="false" unique="true" name="`email`"/>
</property>

--------------------------------------------------
SIDE NOTE: One thing I noticed is that even though
required="true", not-null is still false. Is that correct?
--------------------------------------------------

The SchemaUpdate logger has the following:

2008-11-11 19:50:53,212 [main] INFO org.hibernate.tool.hbm2ddl.DatabaseMetadata - table not found: hibernate_unique_key
2008-11-11 19:50:53,214 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - create table "account" ("id" bigint not null, "dtype" varchar(255) not null, e_version integer not null, "confirmed" smallint, "enabled" smallint, "email" varchar(255), "firstname" varchar(255), "lastname" varchar(255), "registrationdate" timestamp, primary key ("id"))
2008-11-11 19:50:53,454 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - create table hibernate_unique_key ( next_hi integer )
2008-11-11 19:50:53,464 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate - insert into hibernate_unique_key values ( 0 )
2008-11-11 19:50:53,513 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - schema update complete

So it seems like Hibernate is not picking up the unique constraint, even though it's in the mapping.

I'm going to have a look at AbstractMapper.addColumn() next. Please let me know if you think of anything else.

Thanks again,
- Ole
Re: [Teneo] Unique Column Constraint by EAnnotation [message #424968 is a reply to message #424962] Wed, 12 November 2008 08:26 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Ole,
Not-null can be false in case of single_table mapping, in that case all properties of subclasses
always have not-null=false.

If the mapping is correct then you don't need to debug the AbstractMapper anymore. This is something
else possibly. It has been my experience that hibernate in update schema mode does not update/add
unique constraints or indexes. So if in your case you are updating an existing schema then this can
be the reason that no unique constraints are created.

gr. Martin

Ole Ersoy wrote:
> Hi Martin,
>
> Wow - I just found the troubleshooting section on the elver site. Lots
> of good stuff in there :-).
>
> I see the annotation generated in the interface like this:
>
> * @model required="true"
> * annotation="teneo.jpa appinfo='@Column(unique=\"true\")'"
>
> I also printed the mapping from the test I'm running and it looks like
> it took the annotation:
>
> ============================================================ ======
> <property name="email" lazy="false" insert="true" update="true"
> not-null="false" unique="true" type="java.lang.String">
> <column not-null="false" unique="true" name="`email`"/>
> </property>
>
> --------------------------------------------------
> SIDE NOTE: One thing I noticed is that even though required="true",
> not-null is still false. Is that correct?
> --------------------------------------------------
>
> The SchemaUpdate logger has the following:
>
> 2008-11-11 19:50:53,212 [main] INFO
> org.hibernate.tool.hbm2ddl.DatabaseMetadata - table not found:
> hibernate_unique_key
> 2008-11-11 19:50:53,214 [main] DEBUG
> org.hibernate.tool.hbm2ddl.SchemaUpdate - create table "account" ("id"
> bigint not null, "dtype" varchar(255) not null, e_version integer not
> null, "confirmed" smallint, "enabled" smallint, "email" varchar(255),
> "firstname" varchar(255), "lastname" varchar(255), "registrationdate"
> timestamp, primary key ("id"))
> 2008-11-11 19:50:53,454 [main] DEBUG
> org.hibernate.tool.hbm2ddl.SchemaUpdate - create table
> hibernate_unique_key ( next_hi integer )
> 2008-11-11 19:50:53,464 [main] DEBUG
> org.hibernate.tool.hbm2ddl.SchemaUpdate - insert into
> hibernate_unique_key values ( 0 )
> 2008-11-11 19:50:53,513 [main] INFO
> org.hibernate.tool.hbm2ddl.SchemaUpdate - schema update complete
>
> So it seems like Hibernate is not picking up the unique constraint, even
> though it's in the mapping.
> I'm going to have a look at AbstractMapper.addColumn() next. Please let
> me know if you think of anything else.
>
> Thanks again,
> - Ole


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] Unique Column Constraint by EAnnotation [message #424986 is a reply to message #424968] Wed, 12 November 2008 16:11 Go to previous messageGo to next message
Ole Ersoy is currently offline Ole ErsoyFriend
Messages: 205
Registered: July 2009
Senior Member
Hi Martin,

Comments inline:

Martin Taal wrote:
> Not-null can be false in case of single_table mapping,

I think I may be missing something. I'll try to explain my thinking further, to see if it makes any sense. My initial assumption with respect to not-null was that if I set EAttribute.lowerBound = 1 and EAttribute.upperBound = 1, then this means that the column that is mapped to the EAttribute can only have non-null values, so the not-null attribute has to be true?

I also did notice in the mapping file that the unique attribute of the property element is set to true (I imagine this comes from the EAttribute.unique property), which seems like it should tell hibernate add a unique constraint, but I need to look it up. Just listing my first impressions at the moment.

> If the mapping is correct then you don't need to debug the
> AbstractMapper anymore. This is something else possibly. It has been my
> experience that hibernate in update schema mode does not update/add
> unique constraints or indexes. So if in your case you are updating an
> existing schema then this can be the reason that no unique constraints
> are created.

I do delete and recreate the database on each test run. Could it be that the @Column configuration is mapped, as indicated by the mapping xml, but perhaps not transferred to the Hibernate configuration instance?

Thanks again,
- Ole
Re: [Teneo] Unique Column Constraint by EAnnotation [message #424991 is a reply to message #424986] Wed, 12 November 2008 16:33 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Ole,
The mapping what you see is exactly the same mapping passed on to hibernate. So I am not sure why
hibernate does not pick it up...

Regarding not-null. Here is a more complete description. In case of single-table inheritance mapping
instances of the rootclass as well as the subclass are stored in the same table. Assume now that the
subclass has a member name which is mandatory (but not present in the rootclass) then when storing
an instance of the rootclass the name column can not be filled (as the instance of the rootclass
does not have it), so therefore this column is set to nullable (eventhough it may be mandatory in
the subclass).

But my description above only applies if you use single_table and the property you refer to is in
the subclass and mandatory.

gr. Martin

Ole Ersoy wrote:
> Hi Martin,
>
> Comments inline:
>
> Martin Taal wrote:
>> Not-null can be false in case of single_table mapping,
>
> I think I may be missing something. I'll try to explain my thinking
> further, to see if it makes any sense. My initial assumption with
> respect to not-null was that if I set EAttribute.lowerBound = 1 and
> EAttribute.upperBound = 1, then this means that the column that is
> mapped to the EAttribute can only have non-null values, so the not-null
> attribute has to be true?
>
> I also did notice in the mapping file that the unique attribute of the
> property element is set to true (I imagine this comes from the
> EAttribute.unique property), which seems like it should tell hibernate
> add a unique constraint, but I need to look it up. Just listing my
> first impressions at the moment.
>
>> If the mapping is correct then you don't need to debug the
>> AbstractMapper anymore. This is something else possibly. It has been
>> my experience that hibernate in update schema mode does not update/add
>> unique constraints or indexes. So if in your case you are updating an
>> existing schema then this can be the reason that no unique constraints
>> are created.
>
> I do delete and recreate the database on each test run. Could it be
> that the @Column configuration is mapped, as indicated by the mapping
> xml, but perhaps not transferred to the Hibernate configuration instance?
> Thanks again,
> - Ole


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Teneo] Unique Column Constraint by EAnnotation [message #424996 is a reply to message #424991] Wed, 12 November 2008 17:36 Go to previous messageGo to next message
Ole Ersoy is currently offline Ole ErsoyFriend
Messages: 205
Registered: July 2009
Senior Member
Hi Martin,

Martin Taal wrote:
> Hi Ole,
> The mapping what you see is exactly the same mapping passed on to
> hibernate. So I am not sure why hibernate does not pick it up...

OK - I'll start reading through code to see whether I can understand it better. What's the best place to start? Is there a test(s) that I could play with?

>
> Regarding not-null. Here is a more complete description. In case of
> single-table inheritance mapping instances of the rootclass as well as
> the subclass are stored in the same table. Assume now that the subclass
> has a member name which is mandatory (but not present in the rootclass)
> then when storing an instance of the rootclass the name column can not
> be filled (as the instance of the rootclass does not have it), so
> therefore this column is set to nullable (eventhough it may be mandatory
> in the subclass).
>
> But my description above only applies if you use single_table and the
> property you refer to is in the subclass and mandatory.

Ah - Cool - I get it now - Thanks!

- Ole
Re: [Teneo] Unique Column Constraint by EAnnotation [message #425002 is a reply to message #424996] Wed, 12 November 2008 19:56 Go to previous message
Ole Ersoy is currently offline Ole ErsoyFriend
Messages: 205
Registered: July 2009
Senior Member
Hi Martin,

Scratch the test case question. It's derby. I found something in the Hibernate wiki about Derby not supporting unique constraints on nullable fields. So I changed the annotation to look like this:

<eAnnotations source="teneo.jpa">
<details key="appinfo" value="@Column(unique=&quot;true&quot;, nullable=&quot;false&quot;)"/>
</eAnnotations>

Now it works.

Thanks again for all the help!
- Ole
Previous Topic:Ecore based meta model evolution
Next Topic:dsiable FeatureNotFoundExeption in EMF based editor
Goto Forum:
  


Current Time: Fri Apr 26 05:59:12 GMT 2024

Powered by FUDForum. Page generated in 0.03300 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top