Disclaimer:
Sorry for the long post, I was merely trying to give all the information required. Please read through the end, I put in a lot of effort and marked up important stuff with colors and such. Thanks!
Scenario:
I run an ant script with an ant task of schema export.
The task fails due to an SQLException: ORA-02261: such unique or primary key already exists in the table.
I tried to isolate the problem (my schema should include 4 classes, 4 tables, but I tried to run it on isolated classes that are independent) and here are my findings:
My classes to be mapped are: Level, LogRecord, LogFramework, LogFrameworkLEvel.
The last 2 depend on each other and on Level.
Level and LogRecord are both independent.
So off I went and ran the script with only LogRecord and then again with only Level and it was successful (YEAH!)
When I run it with both Level and LogRecord I get that exception mentioned above.
ant task
<target name="schemaexport-oracle" description="Generates the database schema with hbm2ddl to a file" depends="compile">
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="compile.path" />
<hibernatetool destdir="${build}">
<annotationconfiguration configurationfile="${config}/test/hibernate-testOraclePersister.cfg.xml" />
<hbm2ddl haltonerror="true" create="true" drop="true" delimiter=";" outputfilename="oracle_sql.ddl" />
</hibernatetool>
</target>
hibernate config file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="LoggingServiceAnnotationFactory">
<property name="hibernate.connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="hibernate.connection.url">
jdbc:oracle:thin:username/password@machine.domain.name.edu:1522:db_name
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</property>
<property name="username">username</property>
<property name="password">password</property>
<mapping class="nnl.util.logging.domain.Level" />
<mapping class="nnl.util.logging.domain.LogRecord" />
</session-factory>
</hibernate-configuration>
Hibernate version:
3.2.6
Mapping documents:
I am using JPA annotations instead of xml mapping.
here is the code of LogRecord:
@Immutable
@Entity
@Table(name = "LOG_RECORDS")
public class LogRecord implements Serializable, Cloneable {
@XStreamOmitField
@Transient
private static final long serialVersionUID = 1L;
@XStreamAlias("thread-id")
@Column(name = "THREAD_ID", updatable = false, length = 100)
private String threadId;
@XStreamAlias("record-sequence-id")
@Column(name = "RECORD_SEQUENCE_ID",
updatable = false, scale=19, precision=0)
private Long recordSequenceId;
@XStreamOmitField @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) @Column(name = "LOG_RECORD_ID") private Integer logRecordId;
@Column(name = "LOGGER_NAME", updatable = false, length=200)
@XStreamAlias("logger-name")
private String loggerName;
@XStreamAlias("message")
@Column(name = "MESSAGE", nullable = false, updatable = false, length=4000)
private String message;
@XStreamAlias("level-id")
@Column(name="LEVEL_ID",
nullable = false, updatable=false, scale=3, precision=0)
private Integer levelId;
@XStreamAlias("source-framework-level-id")
@Column(name="SOURCE_FRAMEWORK_LEVEL_ID",
nullable = false, updatable=false, scale=2, precision=0)
private Integer sourceFrameworkLevelId;
@Column(name = "SOURCE_COMPONENT", updatable = false, length=100)
@XStreamAlias("source-component")
private String sourceComponent;
@XStreamAlias("source-location")
@Column(name = "SOURCE_LOCATION", updatable = false, length=300)
private String sourceLocation;
@XStreamAlias("record-timestamp")
@Column(name = "RECORD_TIMESTAMP", nullable = false, updatable = false)
private Date recordTimestamp;
@XStreamOmitField
@Column(name = "INSERT_TIMESTAMP", nullable = false,
updatable = false)
private Date insertTimestamp;
//getter methods here....
}//end of LogRecord
code of Level
@Entity
@Table(name = "LOG_LEVELS")
@Immutable
public class Level implements Serializable {
private static final long serialVersionUID = -8089632326298346664L;
@Id
@Column(name = "LEVEL_ID", nullable = false, scale = 5, precision = 0, unique=true)
private Integer id;
@Column(name = "NAME", nullable = false, length = 50)
private String name;
//more constructor and getters here....
}//end of Level
Full stack trace of any exception that occurs:
[hibernatetool] 547 [main] DEBUG org.hibernate.connection.DriverManagerConnectionProvider - opening new JDBC connection
[hibernatetool] 860 [main] DEBUG org.hibernate.connection.DriverManagerConnectionProvider - created connection to: jdbc:oracle:thin:username/password@machine.domain.name.edu:1522:db_name, Isolation Level: 2
[hibernatetool] drop table LOG_LEVELS cascade constraints;
[hibernatetool] 938 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table LOG_LEVELS cascade constraints;
[hibernatetool] 969 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: drop table LOG_LEVELS cascade constraints
[hibernatetool] 969 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - ORA-00942: table or view does not exist
[hibernatetool] drop table LOG_RECORDS cascade constraints;
[hibernatetool] 969 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop table LOG_RECORDS cascade constraints;
[hibernatetool] 969 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: drop table LOG_RECORDS cascade constraints
[hibernatetool] 969 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - ORA-00942: table or view does not exist
[hibernatetool] drop sequence hibernate_sequence;
[hibernatetool] 969 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - drop sequence hibernate_sequence;
[hibernatetool] 969 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - Unsuccessful: drop sequence hibernate_sequence
[hibernatetool] 969 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - ORA-02289: sequence does not exist
[hibernatetool] create table LOG_LEVELS (LEVEL_ID number(10,0) not null unique, NAME varchar2(50) not null, primary key (LEVEL_ID));
[hibernatetool] 969 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - create table LOG_LEVELS (LEVEL_ID number(10,0) not null unique, NAME varchar2(50) not null, primary key (LEVEL_ID));
[hibernatetool] 1016 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - schema export unsuccessful
[hibernatetool] org.hibernate.JDBCException: Error during DDL export
[hibernatetool] at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:271)
[hibernatetool] at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:190)
[hibernatetool] at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:133)
[hibernatetool] at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTask.java:55)
[hibernatetool] at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
[hibernatetool] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
[hibernatetool] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[hibernatetool] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[hibernatetool] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[hibernatetool] at java.lang.reflect.Method.invoke(Method.java:597)
[hibernatetool] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:105)
[hibernatetool] at org.apache.tools.ant.Task.perform(Task.java:348)
[hibernatetool] at org.apache.tools.ant.Target.execute(Target.java:357)
[hibernatetool] at org.apache.tools.ant.Target.performTasks(Target.java:385)
[hibernatetool] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
[hibernatetool] at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
[hibernatetool] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[hibernatetool] at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
[hibernatetool] at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
[hibernatetool] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
[hibernatetool] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)
[hibernatetool] Caused by: java.sql.SQLException: ORA-02261: such unique or primary key already exists in the table
[hibernatetool] at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
[hibernatetool] at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
[hibernatetool] at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
[hibernatetool] at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
[hibernatetool] at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:210)
[hibernatetool] at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:961)
[hibernatetool] at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1190)
[hibernatetool] at oracle.jdbc.driver.OracleStatement.executeUpdateInternal(OracleStatement.java:1657)
[hibernatetool] at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1626)
[hibernatetool] at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:308)
[hibernatetool] at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:267)
[hibernatetool] ... 20 more
[hibernatetool] 1016 [main] DEBUG org.hibernate.connection.DriverManagerConnectionProvider - returning connection to pool, pool size: 1
[hibernatetool] 1016 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool:
Name and version of the database you are using:
Oracle 10.2.0.1
The generated SQL (show_sql=true):
This is the sql generated when running the script with both classes. As one my see - the sql generated contain only the table for the Level class.
drop table LOG_LEVELS cascade constraints;
drop table LOG_RECORDS cascade constraints;
drop sequence hibernate_sequence;
create table LOG_LEVELS (
LEVEL_ID number(10,0) not null unique,
NAME varchar2(50) not null,
primary key (LEVEL_ID));
If I run the script with the LogRecord class alone the generated sql is:
drop table LOG_RECORDS cascade constraints;
drop sequence hibernate_sequence;
create table LOG_RECORDS (
LOG_RECORD_ID number(10,0) not null,
INSERT_TIMESTAMP date not null,
LEVEL_ID number(10,0) not null,
LOGGER_NAME varchar2(200),
MESSAGE varchar2(4000) not null, RECORD_SEQUENCE_ID number(19,0),
RECORD_TIMESTAMP date not null,
SOURCE_COMPONENT varchar2(100),
SOURCE_FRAMEWORK_LEVEL_ID number(10,0) not null, SOURCE_LOCATION varchar2(300),
THREAD_ID varchar2(100),
USER_ID number(10,0),
primary key (LOG_RECORD_ID)
);
create sequence hibernate_sequence;
|