-->
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: Problem with transitive persistence
PostPosted: Sat Jul 28, 2007 7:56 pm 
Newbie

Joined: Thu Jul 26, 2007 5:08 pm
Posts: 6
I'm trying to get both sides of the relationship updated before the commit. Reading the manual I discovered that Transitive persistence does that for me and all I had to do is put the annotation of cascade. But it didn't work.
I have a Basket and Product which I add to the Basket. When I add it to the Basket I need the list of Baskets from product to be updated too. The third code is the an example of what I'm trying to do. The "numberOfBaskets()" should return 1, but it is returning 0.

Is there any other annotations that I should put ? I tried to put cascade at Product too but it didn't work as well...

thx

Code:
@Entity
@Table(name = "PRODUCT")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Product implements java.io.Serializable
{
  @Id
  @Column(name = "CD_PRODUCT")
  @Length(max=10)
  @NotNull
  private String m_Code;

  @Column(name = "CD_UNIT")
  @Length(max=3)
  private String m_UnitCode;

  @Column(name= "NM_PRODUCT")
  @Length(max=50)
  private String m_Name;

  @ManyToMany(mappedBy = "m_Products")
  private List<Basket> m_Baskets;
}



Code:
@Entity
@Table(name = "BASKET")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Basket implements java.io.Serializable
{
  @Column(name = "QT_MAX")
  private int m_MaxQty;
 
  @ManyToMany(cascade=CascadeType.ALL)
  @JoinTable(name="BASKET_PRODUCT",
  joinColumns={@JoinColumn(name="CD_BASKET", nullable=false)},
  inverseJoinColumns={@JoinColumn(name="CD_PRODUCT", nullable=false)})
  private List<Product> m_Products;
}



Code:
HibernateUtil.GetSession().beginTransaction();
Product v_Product = Product.Get(p_ProductCode);
Basket v_Basket = Basket.Get(p_BasketCode);
v_Basket.add(v_Product);
System.out.println("Baskets: " + v_Product.numberOfBaskets());
HibernateUtil.GetSession().getTransaction().commit();
System.out.println("Baskets: " + v_Product.numberOfBaskets());


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 3:40 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
transitive persistence is not about maintaining both sides of the relationship. it's about applying the operation (save for example) to the associated objects

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Ok
PostPosted: Tue Jul 31, 2007 3:44 pm 
Newbie

Joined: Thu Jul 26, 2007 5:08 pm
Posts: 6
Thx emmanuel, reading the book I understood that... Is there a way to make hibernate maintain both sides of the relationship automatically ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 6:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
no but people put a special method in their object to do it properly
product.addToBasket() {
//update both sides
}

_________________
Emmanuel


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.