8 Replies Latest reply: Jun 27, 2005 7:32 PM by Emmanuel Bernard RSS

Editing and Deleting Entity

Murtuza Vohra Newbie

Hello,

Further to the thread http://www.jboss.org/index.html?module=bb&op=viewtopic&t=64960, can anybody show me or point me to any resource that show how to edit an entity or delete an entity from a 1:M relation like for example

Order (1).....................(M) LineItem.

For a given LineItems of an Order, how to later change the lineitem values (like quantity) or delete a lineitem from the Order.

I want to demonstrate the complete functionality of EJB3.0

THanks in advance

Murtuza

  • 1. Re: Editing and Deleting Entity
    Leos Urban Newbie

    Hello,
    at first your client must load instance of Order by your findByKey() method. Now you have detached Order instance with collection of LineItems (assume Eager loading). Now you change something in LineItems collection, for example product name. Next call update() method of your session bean but with your detached local client Order as parameter.

    Called Session bean method:

    public void update(Order orderFromClient)
    {
     if (orderFromClient != null)
     {
     manager.merge(orderFromClient);
     }
    }
    


    Now it works fine.

    You can of course rewrite code to using Order instantiated in session bean and write SB method for change specified items .

    With delete is some problem because when you remove item from Collection on detached object then merge() does not remove any item from database.
    I dont know why for now.

    Leonell

  • 2. Re: Editing and Deleting Entity
    Murtuza Vohra Newbie

    Is there any other way we can remove the appropriate LineItem from the Database

  • 3. Re: Editing and Deleting Entity
    Leos Urban Newbie

    I hope the version with merge() is correct. When I can by this way add and change item I must also these items remove, isnt it? :-)

    I tested also following way (method of SB):

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void removeItem(int orderId, String product)
    {
     Query q = manager.createQuery("from LineItem o where o.order.id=:orderId and o.product like :product");
     q.setParameter("orderId", orderId );
     q.setParameter("product", product );
     java.util.Collection<LineItem> c = q.getResultList();
     for (LineItem li: c)
     {
     manager.remove(li);
     }
    }
    


    There is as result exception about resave, I dont remember exactly.

    I will wait after some time to resolve it...

    L.

  • 4. Re: Editing and Deleting Entity
    Murtuza Vohra Newbie

    This is very inefficient way of managing your entities with relations as introduces extra layer of complexity in the code. Anyways, the persistence specification section 2.1.7 says following:

    "The container handles the object-relational mapping of the relationships, including their loading and storing to the database as specified in the metadata of the bean class, and the referential integrity of the relationships as specified in the database (e.g., by foreign key constraints)."

    With this i suppose the current limitation is only temporary untill someone fixes it.

    Let me try your approach and will post my results later.

  • 5. Re: Editing and Deleting Entity
    Leos Urban Newbie

     

    "murtuza52" wrote:
    This is very inefficient way of managing your entities with relations as introduces extra layer of complexity in the code.


    Yes, but you requested some workaround ;-)

    "murtuza52" wrote:

    With this i suppose the current limitation is only temporary untill someone fixes it.


    See new topic: Problem with removing item from collection in detached object
    I sent example - I hope someone from Jboss Team will answer our question.

    Leonell

  • 6. Re: Editing and Deleting Entity
    manica Newbie

    I haven't had time to try it yet, but it appears that the new OnDelete annotation will accomplish this.

    Darin

  • 7. Re: Editing and Deleting Entity
    Murtuza Vohra Newbie

    Hello Manica

    I could not find OnDelete annotation in the EJB Specification. Can you point me to right resources so i can try it out.

    Murtuza

  • 8. Re: Editing and Deleting Entity
    Emmanuel Bernard Master

    Partly answered in a previous thread about the orphan delete cascading.

    @OnDelete is an hiberate specific annotation.