users@glassfish.java.net

RE: Composite PK for Child Entity

From: Martin Gainty <mgainty_at_hotmail.com>
Date: Fri, 10 Sep 2010 17:18:04 -0400

change lazy="true" to lazy="false" in Order.hbm.xml
 
    <class name="Order" table="CustomerOrder">
     <synchronize table="LineItem"/>
     
     <composite-id name="id"
      class="Order$Id">
      <key-property name="customerId" length="10"/>
      <key-property name="orderNumber"/>
     </composite-id>
     
</snip>
   
     <bag name="lineItems"
      fetch="join"
      lazy="false"
      inverse="true"
      cascade="save-update">
      <key>
       <column name="customerId"/>
       <column name="orderNumber"/>
      </key>
      <one-to-many class="LineItem"/>
     </bag>
    </class>

By the time you're supplying the values to hibernate you need to have all non-null values which comprise the key which is a problem because the WebApp Session active period is not coordinated to the DB transaction and vice versa
If your like most people and accumulate/aggregate values as you go along you want your hibernate session to stay open and not flush your values to disk with can be accomplished with use of the OpenSessionInViewFilter

This filter will by default not flush the Hibernate Session, * as it assumes to be used in combination with service layer transactions that take care of the flushing*, or via HibernateAccessors with flushMode FLUSH_EAGER.
If you want this * filter to flush after completed request processing, override <code>closeSession</code> * and invoke <code>flush</code> on the Session before closing it.

Read more: http://kickjava.com/src/org/springframework/orm/hibernate/support/OpenSessionInViewFilter.java.htm#ixzz0zACG9bZA
http://kickjava.com/src/org/springframework/orm/hibernate/support/OpenSessionInViewFilter.java.htm
 
hth
Martin Gainty
______________________________________________
Jogi és Bizalmassági kinyilatkoztatás/Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

 
Ez az üzenet bizalmas. Ha nem ön az akinek szánva volt, akkor kérjük, hogy jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának készítése nem megengedett. Ez az üzenet csak ismeret cserét szolgál és semmiféle jogi alkalmazhatósága sincs. Mivel az electronikus üzenetek könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet ezen üzenet tartalma miatt.

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut ętre privilégié. Si vous n'ętes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert ŕ l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement ętre sujets ŕ la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.



 

> Date: Fri, 10 Sep 2010 12:37:30 -0700
> From: glassfish_at_javadesktop.org
> To: users_at_glassfish.dev.java.net
> Subject: Composite PK for Child Entity
>
> GFv3.1, JPA 2.0, EJB 3.1, JSF + Primefaces 2.2
>
> Hi
> I have a problem that i am struggling with. I have a master detail model (order header - order lines) and
>
> Order Header - has a PK Order_ID which is a sequence generated ID
> Order Lines - has a composite PK comprised of Order ID, Product_code.
> The relationship b/w Order Header and Order Lines is OneToMany
>
> @Entity
> public class Order implements Serializable
> private static final long serialVersionUID = 1L;
> @Id
> @SequenceGenerator(name="Order_Id_Gen", sequenceName="order_id_seq", allocationSize=1)
> @GeneratedValue(generator="Order_Id_Gen")
> @Basic(optional = false)
> @Column(name = "order_id")
> private Integer orderId;
> @OneToMany(cascade = CascadeType.ALL, mappedBy = "orderLine")
> private List<OrderLine> orderLineList;
>
> @Entity
> public class OrderLine implements Serializable {
> private static final long serialVersionUID = 1L;
> @EmbeddedId
> protected OrderLinePK orderLinePK;
>
> @Embeddable
> public class OrderLinePK implements Serializable {
> @Basic(optional = false)
> @Column(name = "order_id")
> private Integer orderId;
> @Basic(optional = false)
> @Column(name = "product_code")
> private String productCode;
>
> In the JSF UI I browse thru the catalog and add/drag items to the order. Once all the items are added, I click save to persist the order at which time the PK order_id is generated and cascaded to all the child entities.
>
> The user could at any time discard and cancel the process and walk way - so I am waiting till she clicks save to generate the ID.
>
> This is presenting a problem to me - since the order_id is not there when I add the lines on the UI - the composite PK cannot be set. While I can create the OrderLine objects and set them to the OrderLineList - the UI does not seem to recognize it.
>
> The <h:dataTable> that I use to display for some reason does not seem to recognize it.
>
> But the same seems to work if I first saved the header (and let the PK order_id get generated) and then go back and add lines it seem to work just fine. I tried generating the PK - order_id ahead of time when the first line is added to the order - still it does not seem to work.
>
> Any help is appreciated.
> [Message sent by forum member 'smallya']
>
> http://forums.java.net/jive/thread.jspa?messageID=482378
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>