Em.merge does not work while em.persist does?

Hello,
I have a stateless session bean with code snippets below.
    @PersistenceContext
    private EntityManager em;
    public void setList(ContainerList list, int key) {
        ContainerList stored = (ContainerList)em.find(ContainerList.class, key);
        list.setId(key);
        if (stored == null) { em.persist(list); }
        else {
            em.merge(list); // 1st alternative
//            stored.copy(list); // 2nd alternative
...When I call the method setList for a non existing key, the entity is stored in the database. When I call the method for an existing key, the old value is not updated by the new, i.e. the merge method has no effect. If I comment out the 2nd alternative, it has no effect either. Does anyone has an idea why this does not work? I am using Netbeans 5.5 and 5.5.1 release candidate 1. (With the included application server.)

Hello,
I will try to summarise my knowledge of the JAVA persistence API as the documentation is not always clear and may be misleading.
In SQL you have 4 main statements: INSERT, SELECT, UPDATE and DELETE.
with
@PersistenceContext
private EntityManager em;some documentation appears to make the following associations:
em.persist(Object entity) -> INSERT
em.find(Class entityClass, Object primaryKey) -> SELECT
em.merge(Object entity) -> UPDATE
em.remove(Object entity) -> REMOVE
However, practice shows that this is NOT the case!
em.merge(Object entity) does exactly the same thing as em.find(Class entityClass, Object primaryKey)! The only difference is that the em.merge(Object entity) method takes a detached object of the entity class as argument with the same primary key as the attached object you are loading.
Detached objects are objects of the entity class the database manager is not aware of. Attached objects are objects in the database and are managed by the persistence context. Every newly created object of an entity class is detached. You can attach them to the database with the em.persist(Object entity) method call. Attached objects can be retrieved with the em.find(Class entityClass, Object primaryKey) or with the em.merge(Object entity) method. All changes made to attached objects are automatically stored in the database. There thus is no explicit method for the UPDATE statement. You just make changes to attached objects and the entity manager copies the changes to the database (at the end of the persistence context). An easy way to make all the changes you need at once is to have a detached object that contains the right values and then copy all these values to the associated attached object with a self defined copy method in the entity class as in
   Entity detached;
// --> begin persistence context
   Entity attached = em.merge(detached);
   attached.copy(detached);
// <-- end persistence contextIf you are working with session beans, the persistence context standard begins with the beginning of a transaction and ends with the end of a transaction. A transaction standard begins with the start of a session bean method and ends with the session bean method. If you choose to, you can make the persistence context of type extended with
@PersistenceContext(type=PersistenceContextType.EXTENDED)
private EntityManager em;In this case, the persistence context ends when the session bean is removed from the EJB container. (Can be useful to keep attached entities in the state of an stateful session bean.)
Without session beans, you have to declare explicitely the start and end of a persistence context or transaction, but I refer to existing documentation for this, as I do not actively know how to handle things without session beans.
Finally, the methods em.persist(Object entity) and em.merge(Object entity) use a detached object as argument, while em.remove(Object entity) uses an attached object as argument (using a detached object results in an Exception thrown).
I hope this makes some things clear

Similar Messages

  • Hello I am using Ipad 4 and its charger is not working while I was using it few days before and it was working properly now it says that the cable does not support this Ipad Please Help me out.

    Hello I am using Ipad 4 and its charger is not working while I was using it few days before and it was working properly now it says that the cable does not support this Ipad Please Help me out.

    Try a reset:
    - Hold the home button and sleep/wake button down at the same time. When the Apple logo appears release both buttons then try and charge again.
    If issue persists try:
    - Another known working cable
    - Check the charging port for any damage or debris
    - You could also use a dry soft bristle toothbrush and gently brush it on the port to clean it of any debris that could be in there

  • Iphone 5c clock does not work while asleep

    done hard reset updated on and off set automatically time but it works while the device is in use

    iphone 5c clock does not work while asleep
    I have to ask how you know... does it show the right time before you go to sleep? When you wake up?

  • My autofill does not work while using Safari. Have checked both Safari and Apple settings.

    My Autofill does not work while using Safari. I have checked both my Safari settings and my Apple settings but don't know what else to do.

    Hi Eustace....yes, I did reset Safari but it didn't help. I'm running Mac OS X Lion 10.7.5...which might be the problem...my Safari is Version 6.1.1.

  • Iphone 6 plus fingerprint is not working while charging

    My iphone 6 plus fingerprint scan is not working while charging. Is this normal?
    I tried my iphone 5s and it works just fine even while charging.

    I think I got an answer to my own question
    If you're using generic lightning cable (non-original lighting cable), the fingerprint scan doesn't work while charging.
    I just find it weird as my iphone 5s works even if i'm not using the original lightning cable though.

  • HT1665 i have an apple IPhone4. I am experiencing certain problem with my iphone4. The problems are as follows a. my ear piece and proximity sensor is not working while making a call. 2. It cannot reproduce sound without earphone but rings if gets any cal

    Hi folks,
    i have an apple IPhone4. I am experiencing certain problem with my iphone4. The problems are as follows a. my ear piece and proximity sensor is not working while making a call. 2. It cannot reproduce sound without earphone but rings normally if gets any call.
    Can any one help me in this regard??

    Try to reset the phone by holding the sleep and home button for about 10sec, until the Apple logo comes back again. You will not lose data by resetting, but it can cure some glitches after installing new software or apps.

  • Merge call not working in ios7 on Iphone5

    Merge call not working in ios7 on Iphone5 ?

    Same here. We have this call center in our company where after i have added a call to merge i have to push no. 4 and dial from the keypad. This has never been possible in ios but it have been possible to create a contact called "Merge" and add number 4 as it's phone number. Now by merging first my clients call to my collegegue and then to this "Merge" contact it has been dropping me out of line and my client and collegue are merged.
    Now ios 7 wont let me call to this "Merge" contact's number "4"

  • I am having trouble with my speakers not working while online in particular on windows.My audio works when playing audio files

    I am having trouble with my speakers not working while online particular on facebook. My audio works when playing audio files.

    Hi,
    Did it happen all the time or sometime?
    Please check online browser status:
    Click Volume icon in the taskbar, click Mixer link button as below:
    If it's fine, follow this guide to run troubleshooter to detect and fix the issue:
    Tips for fixing common sound problems
    http://windows.microsoft.com/en-in/windows/tips-fixing-common-sound-problems#tips-fixing-common-sound-problems=windows-7
    Meanwhile, this similar thread also could be referred:
    https://social.technet.microsoft.com/forums/ie/en-US/a4a1cfe5-93a5-4c0b-9bf6-f7db0304f2ba/no-sound-on-youtube-or-any-other-webpage
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact [email protected]

  • Microphone not working while using handset

    microphone not working while using handset
    But it is working when loud speaker is On
    Y
    Wat to do??
    Kindly revert with your comments

     Repair the phone with PCC and report back  PC Companion (PCC)  Bridge (for Mac)   Alternatives on How to backup Xperias http://talk.sonymobile.com/thread/36355  

  • Hp split x2 touchscreen not working while un-docking

    hello hp support, i've recently bought a hp split x2 13-110dx. my touchscreen stops working whenever i detach the screen (tablet) from the keyboard (docking station). if i try to safely remove "Docking Station", first nothing happens and then after about 10 seconds all controls (touchscreen and touchpad) othen the keyboard itself stops responding. a reset allways solves this problem but that means that now i allways have to reset after every undocking. also, if the tablet is working fine, and i dock the tablet, again the touckscreen stops responding (again, reset required) i have done a hard (full) shutdown.i have installed all microsoft updates (i am running win8, not 8.1). what to do?sincerely,matan leilien

    Hi  ,
    Thank you for visiting the HP Forums! A great place where you can find solutions for your issues, with help from the community!
    I came across your post about the Notebook, and wanted to assist you! I have looked into your issue about your HP Split x2 Touchscreen and issues with the touchscreen not working while removed from the dock. Here is a document to troubleshoot the TouchScreen. The section you need is the TouchScreen responds inaccurately.
    Here is a link to the HP Support Assistant if you need it. Just download and run the application and it will help with the software and drivers on your system that need updating. Hope this helps. Thanks.

  • Iphone4 speakers not working while calling!!

    Hi
    Can somebody help me?
    My Iphone4 speakers not working while calling only and they work elswhere!!
    I tried to reset the devise with sleep and home buttens but nothing changed?!
    Thx

    Hi
    Can somebody help me?
    My Iphone4 speakers not working while calling only and they work elswhere!!
    I tried to reset the devise with sleep and home buttens but nothing changed?!
    Thx

  • Soundtrack Pro, with Logic Studio. app will not work, while Garage Band is present on my iMac?

    Still new to Logic Pro. Got the Soundtrack Pro when i bought the Logic Studio. am i to understand that this app will not work, while Garage Band is present on my iMac?
    if this is true, i'd welcome any suggestions.  do i have to delete GarageBand on current machine which has Logic Pro?
    appreciate any help
    thanks,

    My MacPro w/ OSX 10.6.8 runs GarageBand, SoundtrackPro and Logic 9 (Express) without issues.
    Everything was installed fresh - ie no overwrite OS upgrades and no app version overwrite upgrades.
    Just so we are clear - an UPGRADE would be from OSX 5 to 6 or from Logic 8 to 9 and involves a purchase. UPDATES are those decimal increments - 10.6.7 to 10.6.8  or Logic 9.1.3 to 9.1.6 that are free.
    Sometimes the sequence of installs can make a difference.
    Good luck,
    x

  • Cancel button functionality does not work--- while searching for G/l Acc

    HI SAP SRM Gurus,
    This is regarding an issue faced by the end user.
    For information my client is on WAS 6.4 Patch Level 155, SRM 4.0, EBP 5.0, SRM Server 5.0.
    In account assignment details..while searching for G/l account for the Account Assignment used at the time of creation of the shopping cart, if no G/l account is found, the ‘Cancel’ button functionality provided, does not work, however when
    G/l account(s) is/are found, then the ‘Cancel’ button functionality
    works properly.
    Kindly assist.
    Awaiting your reply in this regard.
    Thanks in advance
    Ankur

    Hi,
    Sound like a bug.
    Did you open an OSS ticket ?
    Kind regards,
    Yann

  • Automatic update of RemoteApp and Desktop Connections does not work (while manual updates with "update now" is working)

    Hello,
    on several Windows 7 Clients the update/refresh of the RemoteApp and Desktop Connections stopped not working automatically. There is a Update failed error. When we do an "Update Now" manually, it gets synchronized without problems.
    I am asking me if there is an issue with a stored Password as we Need to Change the Passwords regularily. The error is occuring for Connections to all RDS Servers (we have severals).
    Does Windows store the user Password in the Task Scheduler? If yes, does the schedule Task Password Change automatically after the Domain Password Change?
    Or what else could be the issue? The RDS Server and the certificate (Name the same as the URL) seems to be fine as not all Clients have this issue.
    Thank you for your help

    Hi,
    Thank you for posting in Windows Server Forum.
    By default it will automatically update the RemoteApp and Desktop Connections but if in any particular case it’s not happening then for a try you can restart the server and check the result again.  In addition you can try running below command and check
    the result. 
    Start-Process rundll32 -ArgumentList "tsworkspace,TaskUpdateWorkspaces2
    More information:
    Powershell to update "RemoteApp and Desktop Connections"
    If a task is registered using the Administrators group for the security context of the task, then you must also make sure the Run with highest privileges check box is checked if you want to run the task. Please check “Task
    Security Context” for more details.
    Hope it helps!
    Thanks,
    Dharmesh

  • My Adobe Flash CS5.5 did not work while it is opening a .fla with big size

    This is a very serious trouble...
    I have a .fla with 101MB.
    I always use this file and there is no problem on opening this file before.
    This morning , I found that while I am opening the file, Flash did not work. It said Adobe Flash CS5.5 stop running <--- (I forgot what is the whole message)
    I have used my computer for only 1 and a half year only,  my I think I have enough RAM and memory to run my Flash CS5.5 , but this problem did not appear before and appear in this morning
    Can somebody tell me what the problem is? Thank you.
    P.S. here is some information of my computer
    CPU : Intel(R) Core(TM) i5 CPU      650 @ 3.20GHz   3.20 GHz
    RAM : 4.00GB (3.18GB available)
    System type: 64-bit

    Set the library as "merge into code" and the problem doesn't happen.

Maybe you are looking for

  • Transfer data from one table to another

    Hi I have a table which has number of columns. I fetch the rows and convert it in an XML format and insert it in anther table. There are couple of billions records, it will take days to transfer with a simple java program which I have already written

  • Can a folio be used or downloaded by others in a regular website?

    Im working on a magazine project and the goal is to have it Published in AppStore, android and regular website. DPS is the key to make it in iPad and android, but what about doing it on a website? Would it work? Or the answer would be a PDF interacti

  • WLC Web Auth Redirect URL point to an ISE Policy NODE only?

    Hi all, I was wondering if the Web Auth Redirect URL configured in the WLC can only point to an ISE Policy Persona Node so the Web Portal feature (see below) in the ISE is only active when the ISE device has that Policy Persona activated.

  • Sybase ODBC driver for Linux - handle of numeric data type is not correctly

    Linux RHEL 4 + Oracle 10.2.0.3 - all work fine. But numeric data type does not coorectly show values. I gor driver from ASE 15. Who has already resolve this issue???

  • VOD in VO instead of french

    Guess what ? I'm kind of lazy...I hate to watch movies in any foreign language before I see them in French...I've just rent a movie on Apple TV which was supposed to be in French, and it's not ! No line saying it was to be in English...Is there any w