You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Michael Spiro <m....@dcs-caesar.de> on 2011/09/22 09:56:24 UTC

Diamond model and foreign keys

Hi,

 

doing my current project I've got an error on persisting objects with
"diamond" relationship configuration. I tried it with a simplest example -
the problem occurs there too and is probably general.

 

Attached is an archive with an Eclipse project for the example. To save
space I deleted mysql-connector-java-5.0.8-bin.jar and openjpa-all-2.1.0.jar
from the "lib" folder; when you expand the archive put these jars into this
folder for the project to be complete.

 

Now to the example. It implements following model:

 



 

In the example I create a single object of every class and link them with
each other. My main object is Hotel and when I persist it I get the error:

 

Cannot add or update a child row: a foreign key constraint fails
(`diamond`.`price`, CONSTRAINT `fk_price_seasontimes1` FOREIGN KEY
(`seasontimes_idseasontimes`, `room_hotel_idhotel`) REFERENCES `seasontime`
(`idseasontime`, `hotel_idhotel`)

 

It looks like OpenJPA omits inserting the Seasontime record for some reason.


 

Originally as I got the error, the Prices object was linked to both Room and
Seasontime:

 

            seasontime.getPrices().add(price);

            room.getPrices().add(price);

 

But if I only add the price object to the seasontime object's list:

 

            seasontime.getPrices().add(price);

            //room.getPrices().add(price);

 

then everything works fine and the objects are persisted! The opposite:

 

            //seasontime.getPrices().add(price);

            room.getPrices().add(price);

 

brings the same error.

 

Am I doing something wrong or is it really a general problem? Could someone
help?

 

Kind regards,

Michael Spiro


AW: AW: Diamond model and foreign keys

Posted by Michael Spiro <m....@dcs-caesar.de>.
> Was it @ForeignKey(implicit=true)?

Just @ForeignKey without attributes.

Regards,
Michael



Re: AW: Diamond model and foreign keys

Posted by Pinaki Poddar <pp...@apache.org>.
Was it @ForeignKey(implicit=true)?

-----
Pinaki Poddar
Chair, Apache OpenJPA Project
--
View this message in context: http://openjpa.208410.n2.nabble.com/Diamond-model-and-foreign-keys-tp6819272p6824439.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

AW: Diamond model and foreign keys

Posted by Michael Spiro <m....@dcs-caesar.de>.
Hi,

the annotation @ForeignKey solved the problem in my original project and in
the sample project as well. Thanks a lot!

Kind regards,
Michael Spiro


AW: AW: Diamond model and foreign keys

Posted by Michael Spiro <m....@dcs-caesar.de>.
Hi,

>   > a solution similar to b) (with Object as a part of EmbeddedKey) was
> implemented in my original project as the error first occured.
> 
>   Why made you change that model?

[MS] You probably misunderstood me, I didn't change anything in my original
project. I just created this small sample project from scratch to see if the
error has to do with the diamond model or with my implementation in the
original project.




Re: AW: Diamond model and foreign keys

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  > a solution similar to b) (with Object as a part of EmbeddedKey) was
implemented in my original project as the error first occured.

  Why made you change that model?

> rather strange that OpenJPA makes everything correct having less
> information
  It is not strange. When it has duplicate info and no knowledge of that
they are duplicate, it gets confused. If there is only one piece of
information and no duplicate -- no confusion.

> the duplicate mapping comes from the OpenJPA reverse mapping tool.
I noticed that. But here is a deeper chasm. In relational database
references are inherently bi-directional i.e. a foreign key represents a
bi-directional relation. In Java world, all references are uni-directional.
The code has to ensure the semantics of referential consistency. That deep
chasm is what ORM "tries" to bridge, and fails at times as you may have
noticed :)

-----
Pinaki Poddar
Chair, Apache OpenJPA Project
--
View this message in context: http://openjpa.208410.n2.nabble.com/Diamond-model-and-foreign-keys-tp6819272p6820960.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

AW: Diamond model and foreign keys

Posted by Michael Spiro <m....@dcs-caesar.de>.
Hi Pinaki,

thanks for the advice. Actually, a solution similar to b) (with Object as a
part of EmbeddedKey) was implemented in my original project as the error
first occured. I'll try the annotation with @ForeignKey first. In every
case, it's rather strange that OpenJPA makes everything correct having less
information (no link between Price and Room), but writes an error when it
has extra information that actually makes the whole thing consistent.

By the way, the duplicate mapping comes from the OpenJPA reverse mapping
tool. Wouldn't it be reasonable if the tool would generate the classes
without this double mapping?

Kind regards,
Michael Spiro


> -----Ursprüngliche Nachricht-----
> Von: Pinaki Poddar [mailto:ppoddar@apache.org]
> Gesendet: Donnerstag, 22. September 2011 17:09
> An: users@openjpa.apache.org
> Betreff: Re: Diamond model and foreign keys
> 
> Hi Michael,
>   The problem could be in domain model/mapping definition. The model
> follows
> a "duplicate mapping" approach. Let me explain what I mean by
> "duplicate
> mapping" in the context of your application.
> 
> public class Seasontime {
>     @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
>     @JoinColumn(name="hotel_idhotel", columnDefinition="INT")
>     private Hotel hotel;
> 
>     @Id
>     @Column(name="hotel_idhotel", columnDefinition="INT")
>     private int hotelIdhotel;
> 
> Now in this model, both fields "Hotel hotel" and "int hotelIdhotel"
> refer to
> the same "logical" thing i.e. a Hotel object, but in two different
> ways. A
> JPA runtime, however, has little clue that they mean the same thing.
> To make JPA understand this "implicit" sameness of these two fields,
> there
> are two possibilities
> a) annotate "int hotelIdhotel" with @ForeignKey(implicit=true) (see
> @ForeignKey documentation for details)
> b) remove the ""int hotelIdhotel" field altogether. Instead annotate
> the
> "Hotel hotel" as @Id. JPA 2.0 supports a object reference as a primary
> key
> either as a simple key or part of a compound key. I prefer this
> approach
> because it avoids the "duplicate mapping" problem and results into
> cleaner
> domain model.
> Examples of such modeling technique can be found in OpenBooks example
> distributed with OpenJPA.
> 
> -----
> Pinaki Poddar
> Chair, Apache OpenJPA Project
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Diamond-model-and-foreign-keys-
> tp6819272p6820601.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Diamond model and foreign keys

Posted by Pinaki Poddar <pp...@apache.org>.
Hi Michael,
  The problem could be in domain model/mapping definition. The model follows
a "duplicate mapping" approach. Let me explain what I mean by "duplicate 
mapping" in the context of your application.

public class Seasontime {
    @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
    @JoinColumn(name="hotel_idhotel", columnDefinition="INT")
    private Hotel hotel;

    @Id
    @Column(name="hotel_idhotel", columnDefinition="INT")
    private int hotelIdhotel;

Now in this model, both fields "Hotel hotel" and "int hotelIdhotel" refer to
the same "logical" thing i.e. a Hotel object, but in two different ways. A
JPA runtime, however, has little clue that they mean the same thing. 
To make JPA understand this "implicit" sameness of these two fields, there
are two possibilities
a) annotate "int hotelIdhotel" with @ForeignKey(implicit=true) (see
@ForeignKey documentation for details)
b) remove the ""int hotelIdhotel" field altogether. Instead annotate the
"Hotel hotel" as @Id. JPA 2.0 supports a object reference as a primary key
either as a simple key or part of a compound key. I prefer this approach
because it avoids the "duplicate mapping" problem and results into cleaner
domain model. 
Examples of such modeling technique can be found in OpenBooks example
distributed with OpenJPA.

-----
Pinaki Poddar
Chair, Apache OpenJPA Project
--
View this message in context: http://openjpa.208410.n2.nabble.com/Diamond-model-and-foreign-keys-tp6819272p6820601.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.