In JPA, I am using @GeneratedValue:
///////////
@TableGenerator(name = "idGenerator", table = "generator", pkColumnName = "Indecator" , valueColumnName = "value", pkColumnValue = "man")
@Entity
@Table(name="Man")
public class Man implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "idGenerator")
@Column(name="ID")
private long id;
public void setId(Long i){
this.id=i;
}
public Long getId(){
return id;
}
}
///////////
I initially set the ID to some arbitrary value (used as a test condition later on):
///////////
public class Sear {
public static void main(String[] args) throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("testID");
EntityManager em = emf.createEntityManager();
Man man = new Man();
man.setId(-1L);
try {
em.getTransaction().begin();
em.persist(man);
em.getTransaction().commit();
} catch (Exception e) { }
if(man.getId() == -1)
;
}
}
///////////
What is the expected value of man.id after executing commit()? Should it be (-1), a newly generated value, or I should expect an exception?
I want to use that check to detect any exceptions while persisting.