I my application I have used JPA with Hibernate vendor and Oracle 11G DB.
Here I am using the native query as follow on my MST_EMP table ..
Query query = this.entityManager.createNativeQuery("INSERT INTO MST_EMP emp (" +
"EMP_NAME,EMP_MAIL_ID) VALUES ('dasdas',?)");
query.setParameter(1,"dhrumil");
query.executeUpdate();
Here is my MST_EMP entity details..
@Table(name = "MST_EMP")
public class MstEmp implements Serializable, IsEntity {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "EMP_CODE")
@SequenceGenerator( name = "EMP_CODE_SEQ", sequenceName = "EMP_CODE_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMP_CODE_SEQ")
private String empCode;
@Column(name="EMP_MAIL_ID")
private String empMailId;
@Column(name="EMP_NAME")
private String empName;
public MstEmp() {
}
public String getEmpCode() {
return this.empCode;
}
public void setEmpCode(String empCode) {
this.empCode = empCode;
}
public String getCreatedBy() {
return this.createdBy;
}
public void setEmpMailId(String empMailId) {
this.empMailId = empMailId;
}
public String getEmpName() {
return this.empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
As per my understanding, we dont need to give value to EMP_CODE in the native query. Because sequence is associated with it.
But this query gives me error like this ..
SEVERE: ORA-01400: cannot insert NULL into ("PERK"."MST_EMP"."EMP_CODE")
SEVERE: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute native bulk manipulation query
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1179)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1112)
Can any one tell me, Do we need to provide EMP_CODE in the native query ?
Will native query , do not refer the sequence automatically declared in the Entity?
Thanks.