-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Entity merge is not rolled back
PostPosted: Mon Nov 03, 2014 8:13 am 
Newbie

Joined: Mon Nov 03, 2014 8:10 am
Posts: 1
So I have a one to many and many to one relation between two classes. When I try to update an entity, the parent is updated and child throws an error. In that case I expect the parent update to be rolled back but it is not. Since I have a one to many relation, an update on the parent is expected to insert a child but when the child throws an error shouldnt the parent's update be rolled back? If it is of any relation the child's error is thrown due to the unique constraints on the child/account entity.

Below are my two models:

Code:
/** User model **/
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
private int id;

@Column(name = "type")
private String type;

@Column(name = "username", nullable = false)
private String username;

...

// define one to many relation between User and Account

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private Set<Account> accounts; 

public User() {
}

@PrePersist
void preInsert() throws ParseException {
    ...
}

// field getters and setters

...

// returns Account list associated with User
public Set<Account> getAccount() { 
    return accounts; 


// set Account list associated with User
public void setAccount(Set<Account> accounts) { 
    this.accounts = accounts; 
}
}


Model 2:

Code:
/** Account model **/
@Entity
@Table(name = "account", uniqueConstraints =
@UniqueConstraint(columnNames = {"user_id", "entity_id", "branch_id", "type"}))
public class Account {
private int id;

@Column(name = "user_id", nullable = false)
private int user_id;

...

private User user; 

// constructor
public Account() {

}

@PrePersist
void preInsert() throws ParseException {       
    ...
}

// field getters and setters

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

...

// define many to one relation between Account and User

// get User associated with Account

@ManyToOne
@JoinColumn(name = "user_id", insertable = false, updatable = false)
public User getUser() { 
    return user; 


// set User associated with Account
public void setUser(User user) { 
    this.user = user; 
}
}


UserDAO:

Code:
@Repository("userDao")
@Transactional(propagation = Propagation.REQUIRED)
public class UserDAO {
@PersistenceContext
private EntityManager entityManager;

public EntityManager getEntityManager() {
    return entityManager;
}

public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

public void insert(User user) {
    entityManager.persist(user);
}

public void update(User user) {
    entityManager.merge(user);
}
....
}


User service (where i am calling the update)

Code:
@Service
public class UserService {
private UserDAO userDAO;

public UserDAO getUserDao() {
    return userDAO;
}

@Autowired
public void setUserDao(UserDAO userDAO) {
    this.userDAO = userDAO;
}

public boolean addUser(SignupComponent signupComponent) {
    ....
    else {
        // case (4)

        // get user object
        User userObj = getUserDao().findUser(user.getPhone());

        // update user object, adding account and account details
        Set<Account> accounts = userObj.getAccount();

        Account a = new Account();
        a.setBranch_id(signupComponent.branch_id);
        a.setEntity_id(signupComponent.entity_id);
        if (signupComponent.type != -1) {
            a.setType(signupComponent.type);
        }
        a.setUser(userObj);

        userObj.setAccount(accounts);
        userObj.setEmail(signupComponent.user.getEmail());

        AccountDetails ad = new AccountDetails(); //never mind this line, i have another one to one relation with another entity
        ad.setAccount(a);

        a.setAccountDetails(ad);

        accounts.add(a);

        try {
            getUserDao().update(userObj);
            return true;
        }
        catch(Exception e) {
            signupComponent.error = e.toString();
            return false;
        }
    }
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.