Solved

JPA Master-Detail Rollback Problem

Posted on 2012-03-21
1
929 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

730 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