0

I dont know why my List of embeddable objects persists to database despite transaction rollback.

I have my Entity User with List of Embeddable Role.

When I'm persisting user with the same username as already existing in database, i see an exception : "RollbackException: Unable to commit: transaction marked for rollback", which is perfectly fine as i have the unique username column. But I dont know why, despite the rollback, list of roles of that user persists to database.

It's like transaction is working only for entity class and persists list of embedded role to database every time even when it shouldnt ( becouse of rollback).

What am I doing wrong ? Thanks for any hints (I'm using OpenJPA 2.3.0)

User Code:

@Entity
public class User {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int id;

   @Column(unique = true, nullable = false)
   private String username;

   @ElementCollection(fetch = FetchType.LAZY)
   @CollectionTable(
           name = "ROLES",
           joinColumns = @JoinColumn(name = "OWNER_ID")
   )
   private List<Role> roles;

   private String password;
   private boolean enabled;

   public User(String username, String password) {
       this.username = username;
       this.password = password;
       this.enabled = true;
       roles = new ArrayList<Role>();

       Role userrole = new Role();
       userrole.setRole("ROLE_USER");
       userrole.setUsername(username);
       roles.add(userrole);
   }
   //getters and setters
}

Role code:

@Embeddable
public class Role {


    @Column(name="username")
    String username;
    @Column(name="userrole")
    String role;
     //getters and setters
}

0 Answers0