Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0
Mapping documents:
<hibernate-mapping>
<class name="Base" table="BASE" discriminator-value="0">
<id name="id" column="ID" type="long">
<generator class="sequence">
<param name="sequence">S_TEST_01</param>
</generator>
</id>
<discriminator column="TYPE" type="long" not-null="true"/>
<subclass name="P" discriminator-value="1">
<join table="SECOND">
<key column="BASE_ID"/>
<property name="name" column="COLUMN_FOR_P" type="string"/>
</join>
<subclass name="Q" discriminator-value="2">
<join table="SECOND">
<key column="BASE_ID"/>
<property name="price" column="COLUMN_FOR_Q" type="big_decimal"/>
</join>
</subclass>
</subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Q q = new Q();
q.setName("testName");
q.setPrice(new BigDecimal(3.14));
session.save(q);
Description of the question/problem:
When inserting an object of class Q, this mapping definition generates two SQL inserts into table SECOND instead of one, causing a PK uniqueness constraint violation. Is there a different way of mapping to make this work?
The DDL produced by Hibernate is what I expected.
Full stack trace of any exception that occurs:
09:27:35,172 INFO Environment:460).null(Unknown Source - Hibernate 3.0
09:27:35,188 INFO Environment:478).null(Unknown Source - loaded properties from resource hibernate.properties: {hibernate.order_updates=true, hibernate.default_batch_fetch_size=8, hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider, hibernate.max_fetch_depth=1, hibernate.dialect=org.hibernate.dialect.OracleDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=fake, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:oracle:thin:@blabla:1521:XYZ , hibernate.connection.password=****, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=1}
09:27:35,188 INFO Environment:505).null(Unknown Source - using java.io streams to persist binary types
09:27:35,188 INFO Environment:506).null(Unknown Source - using CGLIB reflection optimizer
09:27:35,204 INFO Environment:536).null(Unknown Source - using JDK 1.4 java.sql.Timestamp handling
09:27:35,298 INFO Configuration:460 - Mapping resource: Base.hbm.xml
09:27:36,016 INFO HbmBinder:259 - Mapping class: Base -> BASE
09:27:36,079 INFO HbmBinder:736 - Mapping subclass: P -> BASE
09:27:36,079 INFO HbmBinder:835 - Mapping class join: P -> SECOND
09:27:36,079 INFO HbmBinder:736 - Mapping subclass: Q -> BASE
09:27:36,079 INFO HbmBinder:835 - Mapping class join: Q -> SECOND
09:27:36,094 INFO Configuration:851 - processing extends queue
09:27:36,094 INFO Configuration:855 - processing collection mappings
09:27:36,094 INFO Configuration:864 - processing association property references
09:27:36,094 INFO Configuration:893 - processing foreign key constraints
09:27:36,219 INFO Dialect:91 - Using dialect: org.hibernate.dialect.OracleDialect
09:27:36,485 INFO SettingsFactory:87 - Maximum outer join fetch depth: 1
09:27:36,485 INFO SettingsFactory:90 - Default batch fetch size: 8
09:27:36,485 INFO SettingsFactory:94 - Generate SQL with comments: disabled
09:27:36,485 INFO SettingsFactory:98 - Order SQL updates by primary key: enabled
09:27:36,485 INFO SettingsFactory:273 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
09:27:36,485 INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
09:27:36,485 INFO SettingsFactory:106 - Query language substitutions: {no='N', true=1, yes='Y', false=0}
09:27:36,501 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
09:27:36,501 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
09:27:36,501 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
09:27:36,501 INFO DriverManagerConnectionProvider:80 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@blabla:1521:XYZ
09:27:36,501 INFO DriverManagerConnectionProvider:86 - connection properties: {user=fake, password=****}
09:27:37,063 INFO SettingsFactory:148 - JDBC batch size: 15
09:27:37,063 INFO SettingsFactory:151 - JDBC batch updates for versioned data: enabled
09:27:37,063 INFO SettingsFactory:156 - Scrollable result sets: enabled
09:27:37,063 INFO SettingsFactory:164 - JDBC3 getGeneratedKeys(): disabled
09:27:37,079 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
09:27:37,079 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
09:27:37,079 INFO SettingsFactory:176 - Automatic flush during beforeCompletion(): disabled
09:27:37,079 INFO SettingsFactory:179 - Automatic session close at end of transaction: disabled
09:27:37,079 INFO SettingsFactory:260 - Cache provider: org.hibernate.cache.HashtableCacheProvider
09:27:37,079 INFO SettingsFactory:187 - Second-level cache: enabled
09:27:37,079 INFO SettingsFactory:192 - Optimize cache for minimal puts: disabled
09:27:37,079 INFO SettingsFactory:196 - Cache region prefix: hibernate.test
09:27:37,079 INFO SettingsFactory:199 - Structured second-level cache entries: enabled
09:27:37,079 INFO SettingsFactory:203 - Query cache: disabled
09:27:37,079 INFO SettingsFactory:210 - Echoing all SQL to stdout
09:27:37,094 INFO SettingsFactory:214 - Statistics: disabled
09:27:37,094 INFO SettingsFactory:218 - Deleted entity synthetic identifier rollback: disabled
09:27:37,094 INFO SettingsFactory:232 - Default entity-mode: pojo
09:27:37,266 INFO SessionFactoryImpl:140 - building session factory
09:27:37,938 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
09:27:37,938 INFO Dialect:91 - Using dialect: org.hibernate.dialect.OracleDialect
09:27:37,938 INFO Configuration:851 - processing extends queue
09:27:37,938 INFO Configuration:855 - processing collection mappings
09:27:37,938 INFO Configuration:864 - processing association property references
09:27:37,938 INFO Configuration:893 - processing foreign key constraints
09:27:37,938 INFO Configuration:851 - processing extends queue
09:27:37,938 INFO Configuration:855 - processing collection mappings
09:27:37,954 INFO Configuration:864 - processing association property references
09:27:37,954 INFO Configuration:893 - processing foreign key constraints
09:27:37,954 INFO SchemaExport:114 - Running hbm2ddl schema export
09:27:37,954 INFO SchemaExport:130 - exporting generated schema to database
09:27:37,954 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
09:27:37,954 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
09:27:37,954 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
09:27:37,954 INFO DriverManagerConnectionProvider:80 - using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@blabla:1521:XYZ
09:27:37,954 INFO DriverManagerConnectionProvider:86 - connection properties: {user=fake, password=****}
09:27:38,188 DEBUG SchemaExport:144 - drop table BASE cascade constraints
09:27:38,251 DEBUG SchemaExport:144 - drop table SECOND cascade constraints
09:27:38,313 DEBUG SchemaExport:144 - drop sequence S_TEST_01
09:27:38,329 DEBUG SchemaExport:162 - create table BASE (
ID number(19,0) not null,
TYPE number(19,0) not null,
primary key (ID)
)
09:27:38,391 DEBUG SchemaExport:162 - create table SECOND (
BASE_ID number(19,0) not null,
COLUMN_FOR_P varchar2(255),
COLUMN_FOR_Q number(19,2),
primary key (BASE_ID)
)
09:27:38,454 DEBUG SchemaExport:162 - alter table SECOND add constraint FK918E3874EC2CAD9A foreign key (BASE_ID) references BASE
09:27:38,469 DEBUG SchemaExport:162 - alter table SECOND add constraint FK918E3874EC2CAD99 foreign key (BASE_ID) references BASE
09:27:38,501 ERROR SchemaExport:168 - Unsuccessful: alter table SECOND add constraint FK918E3874EC2CAD99 foreign key (BASE_ID) references BASE
09:27:38,501 ERROR SchemaExport:169 - ORA-02275: such a referential constraint already exists in the table
09:27:38,501 DEBUG SchemaExport:162 - create sequence S_TEST_01
09:27:38,501 INFO SchemaExport:174 - schema export complete
09:27:38,501 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:oracle:thin:@blabla:1521:XYZ
09:27:38,516 INFO SessionFactoryImpl:366 - Checking 0 named queries
Hibernate: select S_ICM_ACM_01.nextval from dual
09:27:38,626 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:oracle:thin:@blabla:1521:XYZ
Hibernate: insert into BASE (TYPE, ID) values (2, ?)
Hibernate: insert into SECOND (COLUMN_FOR_P, BASE_ID) values (?, ?)
Hibernate: insert into SECOND (COLUMN_FOR_Q, BASE_ID) values (?, ?)
09:27:38,876 WARN JDBCExceptionReporter:57 - SQL Error: 1, SQLState: 23000
09:27:38,876 ERROR JDBCExceptionReporter:58 - ORA-00001: unique constraint (D_ENSNDE01.SYS_C0035886) violated
09:27:38,876 ERROR AbstractFlushingEventListener:277 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [Q]
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1882)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2214)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at JoinTest.main(JoinTest.java:24)
Caused by: java.sql.SQLException: ORA-00001: unique constraint (D_ENSNDE01.SYS_C0035886) violated
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:305)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:272)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:623)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:181)
at oracle.jdbc.driver.T4CPreparedStatement.execute_for_rows(T4CPreparedStatement.java:543)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1028)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:2960)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1869)
... 11 more
org.hibernate.exception.ConstraintViolationException: could not insert: [Q]
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1882)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2214)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at JoinTest.main(JoinTest.java:24)
Caused by: java.sql.SQLException: ORA-00001: unique constraint (D_ENSNDE01.SYS_C0035886) violated
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:305)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:272)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:623)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:181)
at oracle.jdbc.driver.T4CPreparedStatement.execute_for_rows(T4CPreparedStatement.java:543)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1028)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:2960)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1869)
... 11 more
Name and version of the database you are using:
Oracle 8i 8.1.7.4.0
The generated SQL (show_sql=true):
see bold lines above
Debug level Hibernate log excerpt:
info