Solved

JPA Master-Detail Rollback Problem

Posted on 2012-03-21
1
920 Views
Last Modified: 2012-07-09
Hello everbody,

Normally, inserting master-detail object to database follow these steps.
1- insert master to database cache.
When error occurred, do rollback.
2- insert detail to database cache.
When error occurred, do rollback.
3-if no error, commit transaction.

But in jpa transaction rollback is not working same, I think.
I have two objects to persist. Like these:

public class ConditionKey implements Serializable {
      private static final long serialVersionUID = 1L;

      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      @Column(name = "KEY_ID")
      private Integer keyId;

      @OneToMany(mappedBy = "conditionKey", fetch = FetchType.EAGER, cascade =             CascadeType.ALL, orphanRemoval = true)
      private List<KeyOption> keyOptions;


@Entity
@Table(name = "KEY_OPTION")
public class KeyOption implements Serializable {
      private static final long serialVersionUID = 1L;

      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      @Column(name = "KEY_OPTION_ID")
      private Integer keyOptionId;

      @Column(name = "OPTION_VALUE")
      private String optionValue;

      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "KEY_ID")
      private ConditionKey conditionKey;

KeyOption optionValue is unique in database.
When I call jpa persist I have trouble with these object. JPA insert ConditionKey to database cache. KeyOption optionValue is unique. When I insert already inserted optionValue
JPA throw en UniqunessException but ConditionKey is already changed. But database not.
How can I solve these rollback problem in JPA? (EclipseLink)

      public void insertConditionKey(ConditionKey conditionKey) throws Exception {
            EntityManager em = entityManagerFactory.createEntityManager();
            try {
                  em.getTransaction().begin();
                  em.persist(conditionKey);
                  em.flush();
                  em.getTransaction().commit();
            } catch (Exception e) {
                  if (em.getTransaction().isActive()) {
                        em.getTransaction().rollback();
                  }
                  throw e;
            } finally {
                  em.close();
            }
      }
0
Comment
Question by:akoyuncu
1 Comment
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 500 total points
ID: 37750964
if exception is going into this block;put some debug there;

catch (Exception e) {
                  if (em.getTransaction().isActive()) {
log.debug("coming to exception");

                        em.getTransaction().rollback();
                  }
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now