Hello. I am new to Hibernate but not new to ORM. We have been using JDO as the primary means of POJO persistence in an application that is over 2 years old. For various reasons, we are refactoring the application and replacing all the JDO implementation classes with Hibernate counterparts. Needless to say, it's been a breath of fresh air and I am happy we made the decision. However, I am currently having a problem with schema generation of the object model.
With the current mappings, if I let Hibernate generate the schema, it tries to create the CP_PC_CATALOG table with two instances of the foreign key column CATALOG_COMPANY for the one to many reference between the Company and Catalog instances. However, if I create the schema myself and leave out the duplicate column, everything works as expected. I can persist or query the model just fine.
It seems Hiberante tries to create the table as follows:
Code:
create table CP_PC_CATALOG
(
PERSISTENT_OID int8 not null,
PERSISTENT_OPT_LOCK int4 not null,
PERSISTENT_MODIFIED timestamp not null,
PERSISTENT_CREATED timestamp not null,
CATALOG_NAME varchar(255),
CATALOG_DESCRIPTION varchar(255),
CATALOG_COMPANY int8,
CATALOG_COMPANY int8 not null,
primary key (PERSISTENT_OID))
All I did was remove the
Quote:
CATALOG_COMPANY int8
statement so the table has been creatd manually as follows:
Code:
create table CP_PC_CATALOG
(
PERSISTENT_OID int8 not null,
PERSISTENT_OPT_LOCK int4 not null,
PERSISTENT_MODIFIED timestamp not null,
PERSISTENT_CREATED timestamp not null,
CATALOG_NAME varchar(255),
CATALOG_DESCRIPTION varchar(255),
CATALOG_COMPANY int8 not null,
primary key (PERSISTENT_OID))
I've read all the documentation and purchased the Hibernate in Action book. I can't figure out if I'm doing something wrong or if this is a bug. I'm leaning towards me doing something wrong, however, I can't figure out why the mapping generates an errouneous schema.
We are using Hibernate version: 3.2-cr2. In our architecture, we have an abstract class called DefaultBasePersistentObject that implements IDefaultBasePersistentObject. All objects that don't have composite keys extend this object. So, for example you may have:
Code:
public interface ICompany
extends IDefaultBasePersistentObject{
...
}
public class CompanyImpl
extends DefaultBasePersistentObject
implements ICompany {
private Set catalogs = new HashSet();
...
}
public class CatalogImpl
extends DefaultBasePersistentObject
implements ICatalog{
private ICompany company;
...
}
Each concrete class is mapped to it's own table. The mapping documents are as follows:
Mapping documents:Code:
<hibernate-mapping>
<class name="com.temnien.cardinalpoint.base.om.IDefaultBasePersistentObject"
abstract="true">
<id
name="oid"
column="PERSISTENT_OID">
<generator class="hilo">
<param name="table">CP_HI_LO_SEQ</param>
<param name="column">next_hi</param>
<param name="max_lo">100</param>
</generator>
</id>
<version
name="optimisticLock"
column="PERSISTENT_OPT_LOCK" />
<property
name="modified"
column="PERSISTENT_MODIFIED"
generated="never"
type="java.util.Date"
not-null="true" />
<property
name="created"
column="PERSISTENT_CREATED"
generated="never"
type="java.util.Date"
not-null="true" />
</class>
<union-subclass
name="com.temnien.cardinalpoint.base.om.IAddress"
extends="com.temnien.cardinalpoint.base.om.IDefaultBasePersistentObject"
abstract="true">
<property
name="line1"
column="LINE1"
not-null="true" />
<property
name="line2"
column="LINE2" />
<property
name="city"
column="CITY"
not-null="true" />
<property
name="zip"
column="ZIP"
not-null="true" />
<property
name="zipplus4"
column="ZIP4"/>
<union-subclass
table="CP_PC_ADDRESS"
name="com.temnien.cardinalpoint.base.om.impl.AddressImpl"
extends="com.temnien.cardinalpoint.base.om.IAddress">
<property
name="state"
column="STATE"
type="com.temnien.cardinalpoint.base.hibernate.US_STATEUserType"
not-null="true" />
</union-subclass>
</union-subclass>
<union-subclass
name="com.temnien.cardinalpoint.pc.om.ICompany"
extends="com.temnien.cardinalpoint.base.om.IDefaultBasePersistentObject"
abstract="true">
<union-subclass
name="com.temnien.cardinalpoint.pc.om.impl.CompanyImpl"
extends="com.temnien.cardinalpoint.pc.om.ICompany"
table="CP_PC_COMPANY">
<property
name="name"
column="COMPANY_NAME" />
<many-to-one
name="address"
column="COMPANY_ADDRESS"
unique="true"
cascade="all"
class="com.temnien.cardinalpoint.base.om.IAddress" />
<set
name="catalogs"
inverse="true"
cascade="save-update">
<key column="CATALOG_COMPANY"/>
<one-to-many
class="com.temnien.cardinalpoint.pc.om.ICatalog"/>
</set>
</union-subclass>
</union-subclass>
<union-subclass
name="com.temnien.cardinalpoint.pc.om.ICatalog"
extends="com.temnien.cardinalpoint.base.om.IDefaultBasePersistentObject"
abstract="true">
<property
name="name"
column="CATALOG_NAME"/>
<property
name="description"
column="CATALOG_DESCRIPTION"/>
<union-subclass
name="com.temnien.cardinalpoint.pc.om.impl.CatalogImpl"
extends="com.temnien.cardinalpont.pc.om.ICatalog"
table="CP_PC_CATALOG">
<!-- COMPANY_CATALOG column is a foreign key to the
primary key of the mapped ICompany table.
Can't have a catalog without a company, so not-null="true". -->
<many-to-one
name="company"
column="CATALOG_COMPANY"
class="com.temnien.cardinalpoint.pc.om.ICompany"
not-null="true"/>
</union-subclass>
</union-subclass>
</hibernate-mapping>
Name and version of the database you are using:We are using Postgres 8.1.
The generated SQL (show_sql=true):SQL shown above and in debug log excerpt below.
Debug level Hibernate log excerpt:Code:
16:49:12,479 DEBUG SchemaExport:301 - create table CP_PC_CATALOG (PERSISTENT_OID int8 not null, PERSISTENT_OPT_LOCK int4 not null, PERSISTENT_MODIFIED timestamp not null, PERSISTENT_CREATED timestamp not null, CATALOG_NAME varchar(255), CATALOG_DESCRIPTION varchar(255), CATALOG_COMPANY int8, CATALOG_COMPANY int8 not null, primary key (PERSISTENT_OID))
16:49:12,479 ERROR SchemaExport:272 - Unsuccessful: create table CP_PC_CATALOG (PERSISTENT_OID int8 not null, PERSISTENT_OPT_LOCK int4 not null, PERSISTENT_MODIFIED timestamp not null, PERSISTENT_CREATED timestamp not null, CATALOG_NAME varchar(255), CATALOG_DESCRIPTION varchar(255), CATALOG_COMPANY int8, CATALOG_COMPANY int8 not null, primary key (PERSISTENT_OID))
16:49:12,479 ERROR SchemaExport:273 - ERROR: column "catalog_company" duplicated
16:49:12,494 DEBUG SchemaExport:301 - alter table CP_PC_CATALOG add constraint FK238DC390F3CC59341203b0bf foreign key (CATALOG_COMPANY) references CP_PC_COMPANY
16:49:12,494 ERROR SchemaExport:272 - Unsuccessful: alter table CP_PC_CATALOG add constraint FK238DC390F3CC59341203b0bf foreign key (CATALOG_COMPANY) references CP_PC_COMPANY
16:49:12,494 ERROR SchemaExport:273 - ERROR: relation "cp_pc_catalog" does not exist
Am I being to zelous with polymorphic references and inheritence? Could that be causing problems? Any help would be greatly appreciated!
Thank you in advance.
Grant