Take the 2-minute tour ×

I have a billing page which consist of information of several category along with their price rate ,quantity and total

The Billing screen look something like

Customer Name : Dummy Customer Customer Place : Dummy Place

Category Name Quantity Price Total TestCategory 10 10 100 TestCategory1 15 10 150

                                         Total 250

Add New Category OK Update Remove

There are 3 buttons on the screen "OK","Update" and "Remove"

Once the user clicks OK button the information gets saved into Database as a new tranx

The Tranx Model is something like

Master_Tranx Entity which consist of TranxID Tranx_Total Customer Name Customer Place

Then Tranx entity which consist of SerialNumber/category/Quantity/Price/Tranx_id

Relation between Master_Tranx entity and Tranx Entity is like

Master_Traxn primary key is Tranx_id which is connected to tranx_id which is foreign key in Tranx entity.

So when user press ok a new record is generated in Tranx and Master_Tranx entity

For every new bill multiple rows goes into Tranx table but a single row is inserted into Master_Tranx entity.

Now the issue is with Update.

Pressing OK button successfully creates data into the above mentioned two entities but

Suppose user wants to update any of the existing tranx like adding some more categories into the existing traxn or updating may be the quantity of the existing row of the billing table , User needs to press update button after updating the required things into the screen.

The way I have implemented the code in this update button is like

I take the tranx_id against which I need to make the update . Then take all the rows in the billing screen . Then populate the Master_Tranx entity which consist of Tranx entity and this tranx entity consist of all the billing table rows.

and then do the update using merge() function of entity manager.

The problem is when I press update button instead of updating existing tranx infomration . it adds new rows against this tranx ID

Ex :I want to create a new infomration . I add new rows like


Then I press OK button after which a tranx ID is generated

Now I want to add one more tranx into the existing tranxID like

Category Name Quantity Price Total TestCategory 20 10 200 TestCategory1 15 10 150 TestCategory3 20 15 300

Also I make changes in the quantity of 1st row I do it 20 from 10. then i press update button

In the DB against TranxEntity I could see rows like

1 TestCategory 10 10 100 2 TestCategory1 15 10 150 3 TestCategory 20 10 200 4 TestCategory1 15 10 150 5 TestCategory3 20 15 300

Although It should be something like

1 TestCategory 20 10 200 2 TestCategory1 15 10 150 3 TestCategory3 20 15 300

Can anybody tell How to achieve this

share|improve this question

1 Answer 1

  • First, as part of "OK" operation, create new entities via new MasterTranx() and new Tranx(), persist them via entityManager.persist(). You are already doing this.
  • Next, you if you wan't to carry out "Update" : you must never create new entities again. Don't do: new MasterTranx() or new Tranx().
  • Instead, either reuse the same entity instances in application memory from "OK". OR retrieve same entities again from the DB using entityManager.find() or entityManager.createQuery().
  • It is ok if the entities become detached (e.g. via. em.detach(ent) or em.clear() or end of transaction for transaction-scoped entityManager or by passing serialised objects over network).
  • Your User Interface code should set data in the retrieved (& a possibly detached) entities
  • Then call

    MasterTranx masterTranx2 = em.merge(masterTranx);

           // masterTranx could be detached
           // masterTranx2 is not detached - it is guaranteed to be managed.


    Tranx tranx2 = em.merge(tranx);

    • ensure you start a entity manager (or EJB) transaction and commit, so that the updated data will be automatically flushed to the DB

Done. = :-)

share|improve this answer

Your Answer


