I have an entity with a primary key and a unique constraint as below
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@GeneratedValue
@Column(name = "ID", nullable = false)
private Integer id;
@Column(name = "NAME")
private String name;
@Column(name = "USER_ID", unique = true, nullable = false)
private String userId;
@Column(name = "PASSWORD")
private String password;
public Employee() {
this("", "", "");
}
public Employee(String name, String userId, String password) {
this(null, name, userId, password);
}
public Employee(Integer id, String name, String userId, String password) {
if (id != null)
this.id = id;
this.name = name;
this.userId = userId;
this.password = password;
}
}
- At various places in my code for unit tests I call my new Employee(...) and I dont persist these objects.
- After a few such instances, I am trying to do session.save(new Employee()) for the first time which gives me a ConstraintViolationException on USER_ID with an Id value greater than 1. And instead if i try persisting (session.save(new Employee("","userID","")) works fine.
Now I am sure that there are objects of Employee classes in the heap and which are transient(as known to my knowledge). The question is will session.save try to save all these objects.
If yes - what should I do avoid from saving all the instances.
If no - Can anybody tell me why am I getting a ConstraintViolationException.
PS: after the exception the no data appears in the database (not even a single entity) hence I am suspecting the save method might be doing something with transient objects and doesnt commit it. I have tried setting autocommit = true and no luck with that too.