persistence@glassfish.java.net

Re: Problem with OneToMany

From: James Sutherland <jamesssss_at_yahoo.com>
Date: Wed, 23 Apr 2008 07:10:22 -0700 (PDT)

In JPA as in Java in general, you are responsible for maintaining your
relationships. We you set the transaction for a card, you must also add the
card to the transaction's cards.

i.e.
public void setTransaction(Transaction transaction) {
    transaction.getCards().add(this);
    this.transaction = transaction;
}

You could also do this with an addCard() method on transaction, if you put
the relationship maintenance in both sides, don't forget to check if it has
already been set first.

See also:
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#One_side_of_the_relationship_is_not_updated_after_updating_the_other_side
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#One_side_of_the_relationship_is_not_updated_after_updating_the_other_side


MrFishKill wrote:
>
> Hi,
>
> I have a problem when 'persist' a new entity and 'update' his childs
> (OneToMany).
> It seems like first entity don't refresh!!!
>
> These are my 2 entities with a OneToMany and ManyToOne relation:
>
> ----------------------------------------------------
> Entity
> @Table(name = "cards")
> public class Card implements Serializable
> {
> @Id
> @GeneratedValue(strategy=GenerationType.SEQUENCE)
> @Column(name="id_card")
> private int idCard;
>
> @OneToMany(fetch = FetchType.LAZY, mappedBy = "card", cascade =
> CascadeType.ALL)
> @JoinColumn(name = "id_card")
> List<Transaction> transactions;
> ...
> }
>
> @Entity
> @Table(name = "transactions")
> public class Transaction
> {
> @Id
> @GeneratedValue(strategy=GenerationType.SEQUENCE)
> @Column(name = "id_transaction")
> private int idTransaction;
>
> @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
> @JoinColumn(name = "id_card")
> private Card card;
> ....
> }
>
> ----------------------------------------------------
> And this is my code:
> ----------------------------------------------------
> ....
> em.getTransaction().begin();
> Card card = new Card();
> ....
> card.setName('fff');
> ....
> ....
>
> em.persist(card);
> ....
> ....
> Colection<Transaction> transactions = .....
> for (Transaction transaction : transactions )
> {
> transaction.setCard(card);
> em.update(transaction);
> }
> int num_transactions = card.getTransactions().size();
> em.getTransaction().commit();
>
> System.out.println("Num Transactions:"+num_transactions);
> <--------------- SHOW ME ZERO RESULTS!!!!
> ----------------------------------------------------
>
> Last line show me 0 results!!!! Why????????
>
> Thanks in advance,
>
>


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Problem-with-OneToMany-tp16827031p16834623.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.