【发布时间】:2019-07-02 21:11:10
【问题描述】:
我收到奇怪的错误原因:
java.sql.SQLIntegrityConstraintViolationException: ORA-00001: 唯一约束
在执行我下面的代码时:
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.
当我运行此代码时,我在字段“否”上收到唯一约束错误。任何人都可以帮助我确定我在代码中做错了什么。当我已经为表中的“否”字段创建序列时,是否需要对配置文件或代码进行任何更改?由于它是生产数据库,我也不知道序列名称。
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>
【问题讨论】:
-
仅仅创建一个序列没有任何作用。如果您有 - 通过触发器或作为列的默认值,您是如何将其与表关联的?