This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

EclipseLink OneToOne

 
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !
I am preparing OCEJPA exam. I have problem with @OneToOne annotation.

There are two classes;
Employee(16) and ParkingSpace(1) which are connected with unidirectional @OneToOne annottation.




but i can add same "parkingSpace1" to different Employee objects.



when i run the example , there is no exception. i check the tables. the same parkingSpace id is added for employees.
so why this annotaion is meaningless ? or where is my fault ? =)

thank you.
Levent
not-one-to-one.png
[Thumbnail for not-one-to-one.png]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Levent Erguder,

The mapping doesn't act like a restriction, instead it just limit the number of objects an source entity might refer to the target entity. The following is a snipped from Pro JPA 2

As it turns out, one-to-one mappings are almost the same as many-to-one mappings except that
only one instance of the source entity can refer to the same target entity instance.



Although you can set the "unique" attribute from @JoinColumn to true, which creates a column with uniqueness, achieving the desired behavior:


 
Creator of Enthuware JWS+ V6
Posts: 3290
296
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Levent (Welcome to Coderanch!) and Rafael,

This is a good question! Let me try to add some words to what Rafael has said.

The phrase in the Pro JPA book "except that only one instance of the source entity can refer to the same target entity instance." implies that there is a unique constraint on the foreign key column. If you read a little bit further in the Pro JPA book (page 439):

Unique Constraints
A unique constraint can be created on a generated column or join column by using the unique element in the @Column, @JoinColumn, @MapKeyColumn, or @MapKeyJoinColumn annotations. There are not actually very many cases where this will be necessary because most vendors will generate a unique constraint when it is appropriate, such as on the join column of one-to-one relationships.



This is weird: most vendors, but not all.... Let's look at the JPA specifications if their should be an unique constraint or not.

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 concatenation 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.



  • According to the JPA specs their should be a unique constraint. This means that the JPA implementation that you have used in your example does not conform to the JPA specs.

    Regards,
    Frits
     
    Levent Erguder
    Greenhorn
    Posts: 12
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    Thank you for replies.

    I am reading this book too but just started.
    in addition, i have downloaded jpa spec.

    as you say the eclipselink implementation doesnt conform the jpa spec.

    the jpa spec says;
    # The entity class must not be final. No methods or persistent instance variables of the entity class may be final

    # The no-arg constructor must be public or protected.

    when i try private no-arg constructor with final class there is no problem.

    so in exam we care jpa specs or eclipselink implementation ?

     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3290
    296
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Levent Erguder wrote:
    so in exam we care jpa specs or eclipselink implementation ?


    The exam is only about the JPA specs.

    I have noticed the EclipseLink JPA implementation has more issues. You might want to use another JPA implementation to do some coding (e.g. OpenJPA)
     
    Levent Erguder
    Greenhorn
    Posts: 12
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you.
    i will obey the jpa spec

     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!