Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [CDO/Hibernate] NOT NULL constraint on EReference with EOpposite
[CDO/Hibernate] NOT NULL constraint on EReference with EOpposite [message #940047] Thu, 11 October 2012 09:08 Go to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Hi Martin

thanks for taking the time to have a look at bug 391134.

I have a question extending the topic:

Using this test model:
class TestElement {
	String name
	refers OtherElement[] others opposite ref
}
class OtherElement {
	String name
	refers TestElement[1] ref opposite others
}

If I change the annotation as suggested to
<eclass name="OtherElement">
	<ereference name="ref">
		<many-to-one optional="false"/>
		<join-column nullable="true" name="testelement_e_id"/>
	</ereference>
</eclass>

The <many-to-one optional="false"/> does not have an effect. By this I mean, that I can persist an instance a of OtherElement where a.ref is null.

The column testelement_e_id does not have a not null constraint, as expected do to the join-column definition. What I did expect was for Hibernate to throw it's own exception do to the many-to-one definition. Would you expect this to be the case?

Is there a way to enforce a reference with an opposite to be non null using either Hibernate or Database mechanisms?

Greetings
Christoph

P.S. I know I can use Ecore Validation to check this on the client side, but I would like to have an additional server side back up check enforcing this similar to other not null or unique constraints.
Re: [CDO/Hibernate] NOT NULL constraint on EReference with EOpposite [message #940257 is a reply to message #940047] Thu, 11 October 2012 13:14 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Christoph,
To check where the 'error' is, is the optional="false" not picked up by the generated mapping?

gr. Martin

On 10/11/2012 11:08 AM, Christoph Keimel wrote:
> Hi Martin
>
> thanks for taking the time to have a look at bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=391134.
>
> I have a question extending the topic:
>
> Using this test model:
> class TestElement {
> String name
> refers OtherElement[] others opposite ref
> }
> class OtherElement {
> String name
> refers TestElement[1] ref opposite others
> }
> If I change the annotation as suggested to
> <eclass name="OtherElement">
> <ereference name="ref">
> <many-to-one optional="false"/>
> <join-column nullable="true" name="testelement_e_id"/>
> </ereference>
> </eclass>
> The <many-to-one optional="false"/> does not have an effect. By this I mean, that I can persist an instance a of
> OtherElement where a.ref is null.
>
> The column testelement_e_id does not have a not null constraint, as expected do to the join-column definition. What I
> did expect was for Hibernate to throw it's own exception do to the many-to-one definition. Would you expect this to be
> the case?
>
> Is there a way to enforce a reference with an opposite to be non null using either Hibernate or Database mechanisms?
>
> Greetings
> Christoph
>
> P.S. I know I can use Ecore Validation to check this on the client side, but I would like to have an additional server
> side back up check enforcing this similar to other not null or unique constraints.


--

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@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [CDO/Hibernate] NOT NULL constraint on EReference with EOpposite [message #940420 is a reply to message #940257] Thu, 11 October 2012 16:11 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Hi Martin,

I suppose it is. I'm getting the following log output:

18:05:30.503 DEBUG o.e.e.t.a.mapper.EClassAnnotator - Creating mapping for eclass OtherElement
18:05:30.503 DEBUG o.e.e.t.a.mapper.EClassAnnotator -  Adding default annotations for EClass: OtherElement
18:05:30.503 DEBUG o.e.e.t.a.mapper.EClassAnnotator - Setting the superentity of the eclass
18:05:30.503 DEBUG o.e.e.t.a.m.SingleAttributeAnnotator -  Adding default annotations for EAttribute name
18:05:30.503 DEBUG o.e.e.t.a.m.ManyToOneReferenceAnnotator - EReference + ref/OtherElement does have a manytoone annotation, using it


Thanks for your help!

Greetings
Christoph
Re: [CDO/Hibernate] NOT NULL constraint on EReference with EOpposite [message #940991 is a reply to message #940420] Fri, 12 October 2012 05:52 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Christoph,
I mean can you show me the hbm mapping to be sure :-)

gr. Martin

On 10/11/2012 06:11 PM, Christoph Keimel wrote:
> Hi Martin,
>
> I suppose it is. I'm getting the following log output:
>
> 18:05:30.503 DEBUG o.e.e.t.a.mapper.EClassAnnotator - Creating mapping for eclass OtherElement
> 18:05:30.503 DEBUG o.e.e.t.a.mapper.EClassAnnotator - Adding default annotations for EClass: OtherElement
> 18:05:30.503 DEBUG o.e.e.t.a.mapper.EClassAnnotator - Setting the superentity of the eclass
> 18:05:30.503 DEBUG o.e.e.t.a.m.SingleAttributeAnnotator - Adding default annotations for EAttribute name
> 18:05:30.503 DEBUG o.e.e.t.a.m.ManyToOneReferenceAnnotator - EReference + ref/OtherElement does have a manytoone
> annotation, using it
>
>
> Thanks for your help!
>
> Greetings
> Christoph


--

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@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [CDO/Hibernate] NOT NULL constraint on EReference with EOpposite [message #941049 is a reply to message #940991] Fri, 12 October 2012 07:10 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Hi Martin

this is the relevant part of the generated mapping for the two classes:

<class entity-name="TestElement" abstract="false" lazy="true" discriminator-value="TestElement" table="`testelement`">
	<meta attribute="eclassName" inherit="false">TestElement</meta>
	...
	<property name="name" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
		<column not-null="false" unique="false" name="`name`"/>
	</property>
	<list name="others" table="`testelement_others`" lazy="true" cascade="persist,merge,refresh,save-update,lock">
		<key update="true">
			<column name="`testelement_e_id`" unique="false"/>
		</key>
		<list-index column="`testelement_others_idx`"/>
		<many-to-many entity-name="OtherElement" unique="true">
			<column name="`otherelement_e_id`" unique="false"/>
		</many-to-many>
	</list>
</class>
<class entity-name="OtherElement" abstract="false" lazy="true" discriminator-value="OtherElement" table="`otherelement`">
	<meta attribute="eclassName" inherit="false">OtherElement</meta>
	...
	<property name="name" lazy="false" insert="true" update="true" not-null="true" unique="false" type="java.lang.String">
		<column not-null="true" unique="false" name="`name`"/>
	</property>
	<many-to-one name="ref" entity-name="TestElement" lazy="false" foreign-key="otherelement_ref" insert="true" update="true" not-null="false"/>
</class>


Seams to me as if <many-to-one optional="false"/> and <join-column nullable="true"/> from the configuration boil down to a single attribute in the hbm mapping: <many-to-one not-null="false"/> where the joincolum definition wins over the many-to-one definition.

Greetings
Christoph
Re: [CDO/Hibernate] NOT NULL constraint on EReference with EOpposite [message #941112 is a reply to message #941049] Fri, 12 October 2012 07:51 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Christoph,
Strange, I don't fully understand the mapping, why the many-to-many... Can you send me the ecore model?

gr. Martin


On 10/12/2012 09:10 AM, Christoph Keimel wrote:
> Hi Martin
>
> this is the relevant part of the generated mapping for the two classes:
>
> <class entity-name="TestElement" abstract="false" lazy="true" discriminator-value="TestElement" table="`testelement`">
> <meta attribute="eclassName" inherit="false">TestElement</meta>
> ...
> <property name="name" lazy="false" insert="true" update="true" not-null="false" unique="false"
> type="java.lang.String">
> <column not-null="false" unique="false" name="`name`"/>
> </property>
> <list name="others" table="`testelement_others`" lazy="true" cascade="persist,merge,refresh,save-update,lock">
> <key update="true">
> <column name="`testelement_e_id`" unique="false"/>
> </key>
> <list-index column="`testelement_others_idx`"/>
> <many-to-many entity-name="OtherElement" unique="true">
> <column name="`otherelement_e_id`" unique="false"/>
> </many-to-many>
> </list>
> </class>
> <class entity-name="OtherElement" abstract="false" lazy="true" discriminator-value="OtherElement" table="`otherelement`">
> <meta attribute="eclassName" inherit="false">OtherElement</meta>
> ...
> <property name="name" lazy="false" insert="true" update="true" not-null="true" unique="false" type="java.lang.String">
> <column not-null="true" unique="false" name="`name`"/>
> </property>
> <many-to-one name="ref" entity-name="TestElement" lazy="false" foreign-key="otherelement_ref" insert="true"
> update="true" not-null="false"/>
> </class>
>
> Seams to me as if <many-to-one optional="false"/> and <join-column nullable="true"/> from the configuration boil down to
> a single attribute in the hbm mapping: <many-to-one not-null="false"/> where the joincolum definition wins over the
> many-to-one definition.
>
> Greetings
> Christoph


--

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@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [CDO/Hibernate] NOT NULL constraint on EReference with EOpposite [message #941297 is a reply to message #941112] Fri, 12 October 2012 11:22 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5468
Registered: July 2009
Senior Member
Hi Christoph,
I did some testing and the conclusion is indeed that hibernate does not enforce the constraint, it is left to the
database, which in this case allows nulls...

So no other option than to check on the EMF side..

gr. Martin

On 10/12/2012 09:51 AM, Martin Taal wrote:
> Hi Christoph,
> Strange, I don't fully understand the mapping, why the many-to-many... Can you send me the ecore model?
>
> gr. Martin
>
>
> On 10/12/2012 09:10 AM, Christoph Keimel wrote:
>> Hi Martin
>>
>> this is the relevant part of the generated mapping for the two classes:
>>
>> <class entity-name="TestElement" abstract="false" lazy="true" discriminator-value="TestElement" table="`testelement`">
>> <meta attribute="eclassName" inherit="false">TestElement</meta>
>> ...
>> <property name="name" lazy="false" insert="true" update="true" not-null="false" unique="false"
>> type="java.lang.String">
>> <column not-null="false" unique="false" name="`name`"/>
>> </property>
>> <list name="others" table="`testelement_others`" lazy="true" cascade="persist,merge,refresh,save-update,lock">
>> <key update="true">
>> <column name="`testelement_e_id`" unique="false"/>
>> </key>
>> <list-index column="`testelement_others_idx`"/>
>> <many-to-many entity-name="OtherElement" unique="true">
>> <column name="`otherelement_e_id`" unique="false"/>
>> </many-to-many>
>> </list>
>> </class>
>> <class entity-name="OtherElement" abstract="false" lazy="true" discriminator-value="OtherElement" table="`otherelement`">
>> <meta attribute="eclassName" inherit="false">OtherElement</meta>
>> ...
>> <property name="name" lazy="false" insert="true" update="true" not-null="true" unique="false"
>> type="java.lang.String">
>> <column not-null="true" unique="false" name="`name`"/>
>> </property>
>> <many-to-one name="ref" entity-name="TestElement" lazy="false" foreign-key="otherelement_ref" insert="true"
>> update="true" not-null="false"/>
>> </class>
>>
>> Seams to me as if <many-to-one optional="false"/> and <join-column nullable="true"/> from the configuration boil down to
>> a single attribute in the hbm mapping: <many-to-one not-null="false"/> where the joincolum definition wins over the
>> many-to-one definition.
>>
>> Greetings
>> Christoph
>
>


--

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@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [CDO/Hibernate] NOT NULL constraint on EReference with EOpposite [message #941304 is a reply to message #941297] Fri, 12 October 2012 11:36 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Hi Martin

thank you very much for looking into this!
It's good to know, that I'm not missing any options.

Greetings
Christoph
Re: [CDO/Hibernate] NOT NULL constraint on EReference with EOpposite [message #941324 is a reply to message #941304] Fri, 12 October 2012 12:04 Go to previous message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Hi All

If anyone is reading this thread having similar issues. My workaround is as follows:

1) Define the EReference without the opposite
class TestElement {
	String name
}
class OtherElement {
	String name
	refers TestElement[1] ref
}

Because otherwise the commit will always fail and throw an Exception (even when ref has been set correctly):
org.hibernate.HibernateException: null index column for collection: TestElement.others

2) Classify the many-to-one as not optional
<eclass name="OtherElement">
	<ereference name="ref">
		<many-to-one optional="false"/>
		<join-column name="testelement_e_id"/>
	</ereference>
</eclass>

This way the column will get created with a NOT NULL constraint, so it is not possible the accidentally create an OtherElement without a reference to a TestElement.
(BTW: <join-column nullable="false"/> instead of <many-to-one optional="false"/> will not work, since the many-to-one annotation takes precedence.)

3) If needed: Use HQL to select all OtherElement instances referenced by a specific TestElement instance with a CDOQuery (instead of using the opposite list managed by EMF).

Greetings
Christoph
Previous Topic:[CDO] Changes between the 4.1 releases?
Next Topic:EMF Validation in Eclipse 3.8
Goto Forum:
  


Current Time: Fri Apr 26 08:51:46 GMT 2024

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

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

Back to the top