-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate doesn't generate a unique constraint for OneToOne
PostPosted: Mon Sep 10, 2012 5:31 am 
Newbie

Joined: Thu Feb 16, 2012 7:45 am
Posts: 3
Hi,
declaring an unidirectional one-to-one relation using JPA @OneToOne annotation does NOT result in a unique constraint generation. Interesting is that Hibernate does generate the constraint when using @OneToOne(optional=false). This behavior is wrong. According to the JPA spec a unique constraint must always be generated for an unidirectional OneToOne. And it does not matter whether optional is false or true. one-to-one is one-to-one.
Sure there are still a few databases, which do not support null values in unique constrained columns, but Oracle, MySQL, HSQL, Microsoft SQL Server, etc. all of them do support that.
So is this a bug?
In the code, the logic, which might to be wrong, is in AnnotationBinder class (line 1574 in 4.1.4.Final):
final boolean mandatory = !ann.optional() || forcePersist;

Best regards,
Sergiy


Top
 Profile  
 
 Post subject: Re: Hibernate doesn't generate a unique constraint for OneToOne
PostPosted: Wed Sep 12, 2012 5:19 am 
Newbie

Joined: Thu Feb 16, 2012 7:45 am
Posts: 3
Just tried with the latest Hibernate release 4.1.7.Final.
The same problem. Setting optional of @OneToOne to true (which is also the default value) leads to no unique constraint on the column, which definitely breaks the semantic of OneToOne and does not match the spec.
So, I see this as a bug.


Top
 Profile  
 
 Post subject: Re: Hibernate doesn't generate a unique constraint for OneToOne
PostPosted: Sat Sep 15, 2012 12:48 pm 
Beginner
Beginner

Joined: Fri Sep 14, 2012 10:41 am
Posts: 20
When you use one-to-one, you are sharing the key with the source object. So there might not be a need for a unique constraint because it is already declared in the source object. Ids won't duplicate!


Top
 Profile  
 
 Post subject: Re: Hibernate doesn't generate a unique constraint for OneToOne
PostPosted: Sat Sep 22, 2012 5:49 pm 
Newbie

Joined: Thu Feb 16, 2012 7:45 am
Posts: 3
You are speaking about the situation where two entities share the same primary key. In this case of course there is no need for unique constraint, since the fk column is a primary column. But if the entities use their own keys, there MUST be a unique constraint.
If A points to B and the relation between is unidirectional OneToOne. In the database there will be a foreign key column (different than primary key column of TableA) in TableA pointing to the primary key column of TableB.
So, if I do
Code:
B b = new B();
A a1 = new A();
a1.setB(b);
A a2 = new A();
a2.setB(b);

and give them to the entity manager, there will be two records in TableA containing the same value of the B primary key from TableB, which will contain only one record corresponding to b.
If there is a unique constraint on the fk column in TableA, the situation above will not be possible. And this is exactly what OneToOne relation is: for every record in TableA there may be one or no record in TableB and otherwise.
So, there MUST be a unique constraint, otherwise it is not OneToOne.
And the JPA specification REQUIRES a unique constraint. And Hibernate does not generate any. Hibernate does not enforce a unique constraint if optional attribute of @OneToOne is true. So it is definitely a bug.
Extract from the spec:
2.10.3.1 Unidirectional OneToOne Relationships
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
Table A contains a foreign key to table B. The foreign key column name is formed as the con- catenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B. The foreign key column has the same type as the primary key of table B and there is a unique key constraint on it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.