Topic
  • 3 replies
  • Latest Post - ‏2012-06-13T21:38:44Z by sutter
OldTIm
OldTIm
2 Posts

Pinned topic was7 jpa2 data missing when Cascade

‏2012-06-12T08:07:02Z |
Hi, there;

I have been struggling on this problem for weeks, can someone please help me working out?

I have an entity with a field like below:

public class DepositTAEntity {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator ="DepositTA_SEQ")
private String ID;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy=
"DepositTA")
private Collection<AccountRecEntity> accountrecs = new
ArrayList<AccountRecEntity>();
.....
}

In a session bean, I invoke above entity:

private EntityManager em;
DepositTAEntity deposit = findByID(newDeposit.getID());
AccountRecEntity accountrec = new AccountRecEntity();
deposit.getAccountrecs().add(accountrec);
AccountRecEntity accountrec2 = new AccountRecEntity();
deposit.getLttaccountrecs().add(accountrec2);

em.flush();
em.clear();

After execution, the accountrec2 is persisted, but accountrec disappears in database without any exception throwed away.

The same code was running no problem with WAS 6.1 and JPA 1.0, but results data missing with WAS 7.0.0.23 and JPA 2.0.

Any response would be appreciated!

Cheers,

Tim
Updated on 2012-06-13T21:38:44Z at 2012-06-13T21:38:44Z by sutter
  • sutter
    sutter
    94 Posts

    Re: was7 jpa2 data missing when Cascade

    ‏2012-06-12T13:10:19Z  
    Hi Tim,
    If you've been struggling with this problem for weeks, you really should open a PMR and follow through with Support. If this discussion ends up identifying a real bug, you'll have to go that route anyway to get a fix.

    But, we can start the discussion with some more questions...

    o You say that "accountrec disappears in database". From your code snippet, accountrec is a new entity, so it never existed in the database. If you meant that accountrec is not persisted to the database, then your scenario makes more sense. Just trying to clarify your expectations.

    o What is "lttaccountrecs"? Your accountrec2 entity gets added to this lttaccountrecs collection, but your code snippet doesn't define this collection/relationship.

    o Not sure if this is pertinent to the problem or not, but your Sequence usage for the DepositTAEntity looks different. I'm not sure what database you are using, but database sequences are normally ints or longs. Your ID type is a String... That conversion could be a performance concern, if not a functional one...

    o And, related to that... How does newDeposit.getID() determine a value for a generated field? Something is not meshing with the scenario as described.

    Just a bunch of questions for now, but maybe we can narrow in on the problem...

    Thanks,
    Kevin Sutter, JPA Architect, IBM WebSphere
  • OldTIm
    OldTIm
    2 Posts

    Re: was7 jpa2 data missing when Cascade

    ‏2012-06-13T09:14:06Z  
    • sutter
    • ‏2012-06-12T13:10:19Z
    Hi Tim,
    If you've been struggling with this problem for weeks, you really should open a PMR and follow through with Support. If this discussion ends up identifying a real bug, you'll have to go that route anyway to get a fix.

    But, we can start the discussion with some more questions...

    o You say that "accountrec disappears in database". From your code snippet, accountrec is a new entity, so it never existed in the database. If you meant that accountrec is not persisted to the database, then your scenario makes more sense. Just trying to clarify your expectations.

    o What is "lttaccountrecs"? Your accountrec2 entity gets added to this lttaccountrecs collection, but your code snippet doesn't define this collection/relationship.

    o Not sure if this is pertinent to the problem or not, but your Sequence usage for the DepositTAEntity looks different. I'm not sure what database you are using, but database sequences are normally ints or longs. Your ID type is a String... That conversion could be a performance concern, if not a functional one...

    o And, related to that... How does newDeposit.getID() determine a value for a generated field? Something is not meshing with the scenario as described.

    Just a bunch of questions for now, but maybe we can narrow in on the problem...

    Thanks,
    Kevin Sutter, JPA Architect, IBM WebSphere
    Hi, Sutter;

    Thank you so much for reply and my apologies for the ambiguous language. Please allow me to explain all your concerns:

    o You say that "accountrec disappears in database". From your code snippet, accountrec is a new entity, so it never existed in the database. If you meant that accountrec is not persisted to the database, then your scenario makes more sense. Just trying to clarify your expectations.

    Yes, you are right, I meant that "accountrec" is not persisted to the database.

    o What is "lttaccountrecs"? Your accountrec2 entity gets added to this lttaccountrecs collection, but your code snippet doesn't define this collection/relationship.

    That's my input errors, it should be just "accountrecs" in the source code.

    Tim
    o Not sure if this is pertinent to the problem or not, but your Sequence usage for the DepositTAEntity looks different. I'm not sure what database you are using, but database sequences are normally ints or longs. Your ID type is a String... That conversion could be a performance concern, if not a functional one...

    We are using Oracle. Thanks for your suggestion regarding performance, I will update this later.

    o And, related to that... How does newDeposit.getID() determine a value for a generated field? Something is not meshing with the scenario as described.

    My apologies again! For somehow, I can't paste source code to my browser, so I only manual input few lines. Actually the newDeposit has been persisted before invoking. At the end of this method, I've also missed to type em.merge(deposit).

    I attached updated codes below, hopefully it can clarify my mistakes:

    private EntityManager em;

    public void approve(DepositTAEntity oldDeposit){

    DepositTAEntity deposit = findByID(oldDeposit.getID());
    AccountRecEntity accountrec = new AccountRecEntity();
    deposit.getAccountrecs().add(accountrec);
    AccountRecEntity accountrec2 = new AccountRecEntity();
    deposit.getAccountrecs().add(accountrec2);

    em.merge(deposit)
    em.flush();
    em.clear();

    }

    Sorry for my mess, Sutter, looking forward to hear from you soon......
  • sutter
    sutter
    94 Posts

    Re: was7 jpa2 data missing when Cascade

    ‏2012-06-13T21:38:44Z  
    • OldTIm
    • ‏2012-06-13T09:14:06Z
    Hi, Sutter;

    Thank you so much for reply and my apologies for the ambiguous language. Please allow me to explain all your concerns:

    o You say that "accountrec disappears in database". From your code snippet, accountrec is a new entity, so it never existed in the database. If you meant that accountrec is not persisted to the database, then your scenario makes more sense. Just trying to clarify your expectations.

    Yes, you are right, I meant that "accountrec" is not persisted to the database.

    o What is "lttaccountrecs"? Your accountrec2 entity gets added to this lttaccountrecs collection, but your code snippet doesn't define this collection/relationship.

    That's my input errors, it should be just "accountrecs" in the source code.

    Tim
    o Not sure if this is pertinent to the problem or not, but your Sequence usage for the DepositTAEntity looks different. I'm not sure what database you are using, but database sequences are normally ints or longs. Your ID type is a String... That conversion could be a performance concern, if not a functional one...

    We are using Oracle. Thanks for your suggestion regarding performance, I will update this later.

    o And, related to that... How does newDeposit.getID() determine a value for a generated field? Something is not meshing with the scenario as described.

    My apologies again! For somehow, I can't paste source code to my browser, so I only manual input few lines. Actually the newDeposit has been persisted before invoking. At the end of this method, I've also missed to type em.merge(deposit).

    I attached updated codes below, hopefully it can clarify my mistakes:

    private EntityManager em;

    public void approve(DepositTAEntity oldDeposit){

    DepositTAEntity deposit = findByID(oldDeposit.getID());
    AccountRecEntity accountrec = new AccountRecEntity();
    deposit.getAccountrecs().add(accountrec);
    AccountRecEntity accountrec2 = new AccountRecEntity();
    deposit.getAccountrecs().add(accountrec2);

    em.merge(deposit)
    em.flush();
    em.clear();

    }

    Sorry for my mess, Sutter, looking forward to hear from you soon......
    Thanks for the additional information, OldTim.

    One item that changed during the timeframe you are describing (EJB3 Fep to OSGi/JPA 2.0 Fep) is that the UpdateManager default changed from OperationOrder to Constraint. The difference is that the SQL statements are sent to the database either in the order of the JPA operations (OperationOrder) or based on the PK/FK constraints that are known (Constraint). Although the use of the ConstraintUpdateManager doesn't explain the strange behavior you are experiencing, it is a difference.

    To test this out, you could change the UpdateManager in use by setting this property in your persistence.xml:

    <property name="openjpa.jdbc.UpdateManager" value="operation-order"/>

    Regardless of the outcome of this experimentation, I would recommend opening a PMR to address this issue. You will eventually get better support than with my occasional access to this forum... :-)

    As an aside... The merge() invocation you inserted should not make a difference, and actually should not be used in this case. The merge() is used to merge detached entities into your current persistence context (managed by the EM instance). Since you found the "deposit" object while within your persistence context, it is already attached to your persistence context. So, there is no need for the merge().

    Good luck,
    Kevin Sutter, JPA Architect, IBM WebSphere