0

I am getting strange error Caused by:

java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint

While executing my below code:

Product DAO.java

@Id
@Column(name = "no", columnDefinition = "NUMBER")
private int serial_number;
//No getter and setter for this field

@Column(name = "fname", columnDefinition = "VARCHAR2(50)")
private int fname;

@Column(name = "lname", columnDefinition = "VARCHAR2(50)")
private int lname;
// Getter and setter for fname and lname


ProductService.java

Product po = new Product();
po.setfname = "Tom";
po.setlname = "John";
//I am not setting 'no' field value since I have created sequence in my oracle table to auto increment the value. 

When I am running this code, I am getting unique constraint error on field 'no'. Can anyone help me in identifying what I am doing wrong in my code. When I have already created sequence for 'no' field in my table, do I need to make any change in config file or code? Since its the production database, I do not know the sequence name also.

hibernate-cgf.xml
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="hibernate.connection.password">pass</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="dao.Product"></mapping>
    </session-factory>
</hibernate-configuration>
1
  • Just creating a sequence doesn't do anything. How have you associated it with the table, if you have - via a trigger, or as the default value for the column? Commented Feb 20, 2017 at 16:03

1 Answer 1

2

Your id field serial_number is an int which is initialized to zero, and your mapping for @Id does not include a @GeneratedValue annotation, so hibernate will assume you are assigning the id manually and save it as zero every time you persist an object, causing the SQLIntegrityConstraintViolationException. You need to add a @GeneratedValue annotation and you can also choose a strategy, like this:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "no", columnDefinition = "NUMBER")
private int serial_number;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It works! I have used @GeneratedValue(strategy = GenerationType.SEQUENCE)
Will @GeneratedValue(strategy = GenerationType.AUTO) use the sequence created in my table ? Beacuse I am always seeing this line Hibernate: select hibernate_sequence.nextval from dual

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.