aspose file tools*
The moose likes Object Relational Mapping and the fly likes Mysql auto generated PK used for another table and JPA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Mysql auto generated PK used for another table and JPA" Watch "Mysql auto generated PK used for another table and JPA" New topic
Author

Mysql auto generated PK used for another table and JPA

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Hello,

My use case is as follows: I use MySQL and JPA 2 and I have two entities: Order and LineItem that are associated by a OneToMany relationship.

I would like for the GUI layer to persist the Order together with the LineItems at the same time (using the Order PK).

Bearing in mind that the PK of Order is auto-generated by MySQL and is part of the PK of LineItem, I don't know how to retrieve the generated Order PK in order to use it for the LineItem PK within the same transaction.

Is there any special JPA configuration I could use to achieve this?

Can anyone help please?
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Well, you should need this id to persist the line items.

Just persist the Order that has its lineItems set to new LineItem and enable cascade persist on the relationship (or call persist on each line item).

As long as you set the ManyToOne back from LineItem to order, and map this as @Id, then it will just work.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0

If you don't want to do this, then things become more complicated. You need to first persist and flush Order, then use the id to persist the lineItems.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
Thanks a lot for your reply James,

I am actually realizing that my domain is a bit more complicated that what I described initially.

My domain is as follows: I have an Advertisement entity engaged in a many to many relationship with a TimeSlot entity with a join entity sitting in between the two entities: AdvertisementToTimeSlotToDayJoin. There is actually a third entity Day which I have omitted.

The only value that is not know in advance, as far as the constructor of AdvertisementToTimeSlotToDayJoinPK is concerned is the Avertisement PK, wich is autogenerated. How then can I persist my Avertisement together with its related join entities?

Regards,

Julien.

Here are my entities:







James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Do not use an EmbeddedId use and IdClass.

Do not map the foreign keys as Basic, just add @Id to the ManyToOnes.
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
James Sutherland wrote:Do not use an EmbeddedId use and IdClass.

Do not map the foreign keys as Basic, just add @Id to the ManyToOnes.


Hello James,

Thanks for the reply.

Should I keep both classes:
and
?

I keep getting the following compilation error:
- There is no primary key attribute to match the ID class attribute timeslotID


Here are my entities after I modified according to your advice:
PK class:




Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
I am still struggling with my JPA entities.

Here is what I have tried:




In my Advertisement entity class:



Here is what I now get:

org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field advertisementToTimeSlotToDayJoinCollection] from the entity class [class com.bignibou.domain.Advertisement] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
 
Consider Paul's rocket mass heater.
 
subject: Mysql auto generated PK used for another table and JPA