I created the following mapping:
Code:
package org.test;
public class InstitutionBase
{
private int id = -1;
private String institutionNumber;
private String productType;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getInstitutionNumber()
{
return institutionNumber;
}
public void setInstitutionNumber(String institutionNumber)
{
this.institutionNumber = institutionNumber;
}
public String getProductType()
{
return productType;
}
public void setProductType(String productType)
{
this.productType = productType;
}
}
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.test.InstitutionBase" table="InstitutionBase">
<id name="id" type="int" column="institutionBaseId" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="institutionNumber" type="string" column="institutionNumber" not-null="true"/>
<property name="productType" type="string" column="productType" not-null="true"/>
</class>
</hibernate-mapping>
Which was successful (output using log4j.logger.org.hibernate=debug):
Quote:
[exportschema] [2006-12-18 10:12:54] INFO Environment - Hibernate 3.1.3
[exportschema] [2006-12-18 10:12:54] INFO Environment - hibernate.properties not found
[exportschema] [2006-12-18 10:12:54] INFO Environment - using CGLIB reflection optimizer
[exportschema] [2006-12-18 10:12:54] INFO Environment - using JDK 1.4 java.sql.Timestamp handling
[exportschema] [2006-12-18 10:12:55] INFO Configuration - Reading mappings from file: C:\projects\test\build\classes\org\test\InstitutionBase.hbm.xml
[exportschema] [2006-12-18 10:12:55] DEBUG DTDEntityResolver - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
[exportschema] [2006-12-18 10:12:55] DEBUG DTDEntityResolver - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
[exportschema] [2006-12-18 10:12:55] DEBUG DTDEntityResolver - located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
[exportschema] [2006-12-18 10:12:55] INFO HbmBinder - Mapping class: org.test.InstitutionBase -> InstitutionBase
[exportschema] [2006-12-18 10:12:55] DEBUG HbmBinder - Mapped property: id -> institutionBaseId
[exportschema] [2006-12-18 10:12:55] DEBUG HbmBinder - Mapped property: institutionNumber -> institutionNumber
[exportschema] [2006-12-18 10:12:55] DEBUG HbmBinder - Mapped property: productType -> productType
[exportschema] [2006-12-18 10:12:56] INFO Dialect - Using dialect: org.hibernate.dialect.SQLServerDialect
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing extends queue
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing collection mappings
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing native query and ResultSetMapping mappings
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing association property references
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing foreign key constraints
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing extends queue
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing collection mappings
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing native query and ResultSetMapping mappings
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing association property references
[exportschema] [2006-12-18 10:12:56] DEBUG Configuration - processing foreign key constraints
[exportschema] [2006-12-18 10:12:56] INFO SchemaExport - Running hbm2ddl schema export
[exportschema] [2006-12-18 10:12:56] DEBUG SchemaExport - import file not found: /import.sql
[exportschema] [2006-12-18 10:12:56] INFO SchemaExport - writing generated schema to file: C:\projects\test\aftBase.sql
[exportschema] [2006-12-18 10:12:56] DEBUG SchemaExport - drop table InstitutionBase;
[exportschema] [2006-12-18 10:12:56] DEBUG SchemaExport - create table InstitutionBase (institutionBaseId int identity not null, institutionNumber varchar(255) not null, productType varchar(255) not null, primary key (institutionBaseId));
[exportschema] [2006-12-18 10:12:56] INFO SchemaExport - schema export complete
BUILD SUCCESSFUL
And generated the following schema for SQLServer:
Code:
create table InstitutionBase
(
institutionBaseId int identity not null,
institutionNumber varchar(255) not null,
productType varchar(255) not null,
primary key (institutionBaseId)
);
Unfortunately, the local DBA was not too happy with a generated ID for a table that will be replicated.
So, I read section 5.1.5. (composite-id), which suggested using the solution in section 8.4. (Components as composite identifiers).
I wasn't too sure how to implement a simple case since the example referred to Order, which was not defined anywhere, but here's what I came up with:
Code:
package org.test;
import java.io.Serializable;
public class InstitutionBaseId implements Serializable
{
private static final long serialVersionUID = 1L;
private String institutionNumber;
private String productType;
public String getInstitutionNumber()
{
return institutionNumber;
}
public void setInstitutionNumber(String institutionNumber)
{
this.institutionNumber = institutionNumber;
}
public String getProductType()
{
return productType;
}
public void setProductType(String productType)
{
this.productType = productType;
}
public boolean equals(Object obj)
{
if(! (obj instanceof InstitutionBaseId) )
{
return false;
}
InstitutionBaseId that = (InstitutionBaseId)obj;
return this.institutionNumber.equals(that.institutionNumber)
&& this.productType.equals(that.productType);
}
public int hashCode()
{
return institutionNumber.hashCode() + productType.hashCode();
}
}
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.test.InstitutionBase" table="InstitutionBase">
<composite-id name="id" class="org.test.InstitutionBaseId">
<key-property name="institutionNumber"/>
<key-property name="productType"/>
</composite-id>
<property name="institutionNumber" type="string" column="institutionNumber" not-null="true"/>
<property name="productType" type="string" column="productType" not-null="true"/>
</class>
</hibernate-mapping>
Building the schema with this mapping file results in:
Quote:
[exportschema] [2006-12-18 10:13:31] INFO Environment - Hibernate 3.1.3
[exportschema] [2006-12-18 10:13:31] INFO Environment - hibernate.properties not found
[exportschema] [2006-12-18 10:13:31] INFO Environment - using CGLIB reflection optimizer
[exportschema] [2006-12-18 10:13:31] INFO Environment - using JDK 1.4 java.sql.Timestamp handling
[exportschema] [2006-12-18 10:13:31] INFO Configuration - Reading mappings from file: C:\projects\test\build\classes\org\test\InstitutionBase.hbm.xml
[exportschema] [2006-12-18 10:13:31] DEBUG DTDEntityResolver - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
[exportschema] [2006-12-18 10:13:31] DEBUG DTDEntityResolver - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
[exportschema] [2006-12-18 10:13:31] DEBUG DTDEntityResolver - located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
[exportschema] [2006-12-18 10:13:32] INFO HbmBinder - Mapping class: org.test.InstitutionBase -> InstitutionBase
BUILD FAILED
C:\projects\test\build-dbschema.xml:56: Schema text failed: Could not read mapping document from file: C:\projects\test\build\classes\org\test\InstitutionBase.hbm.xml
At first I thought I'd deleted the mapping file somehow, but when I reverted the file back to its previous state, it worked fine. So I just assumed this is a generic error message that pops up when something's wrong.
My second attempt was this:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.test.InstitutionBase" table="InstitutionBase">
<composite-id name="id" class="org.test.InstitutionBaseId">
<key-property name="institutionNumber" type="string" column="institutionNumber"/>
<key-property name="productType" type="string" column="productType"/>
</composite-id>
</class>
</hibernate-mapping>
Which resulted in:
Quote:
[exportschema] [2006-12-18 10:16:19] INFO Environment - Hibernate 3.1.3
[exportschema] [2006-12-18 10:16:19] INFO Environment - hibernate.properties not found
[exportschema] [2006-12-18 10:16:19] INFO Environment - using CGLIB reflection optimizer
[exportschema] [2006-12-18 10:16:19] INFO Environment - using JDK 1.4 java.sql.Timestamp handling
[exportschema] [2006-12-18 10:16:20] INFO Configuration - Reading mappings from file: C:\projects\test\build\classes\org\test\InstitutionBase.hbm.xml
[exportschema] [2006-12-18 10:16:20] DEBUG DTDEntityResolver - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
[exportschema] [2006-12-18 10:16:20] DEBUG DTDEntityResolver - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
[exportschema] [2006-12-18 10:16:20] DEBUG DTDEntityResolver - located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
[exportschema] [2006-12-18 10:16:20] INFO HbmBinder - Mapping class: org.test.InstitutionBase -> InstitutionBase
[exportschema] [2006-12-18 10:16:21] DEBUG HbmBinder - Mapped property: institutionNumber -> institutionNumber
[exportschema] [2006-12-18 10:16:21] DEBUG HbmBinder - Mapped property: productType -> productType
[exportschema] [2006-12-18 10:16:21] DEBUG HbmBinder - Mapped property: id -> institutionNumber, productType
[exportschema] [2006-12-18 10:16:21] INFO Dialect - Using dialect: org.hibernate.dialect.SQLServerDialect
[exportschema] [2006-12-18 10:16:21] DEBUG Configuration - processing extends queue
[exportschema] [2006-12-18 10:16:21] DEBUG Configuration - processing collection mappings
[exportschema] [2006-12-18 10:16:21] DEBUG Configuration - processing native query and ResultSetMapping mappings
[exportschema] [2006-12-18 10:16:21] DEBUG Configuration - processing association property references
[exportschema] [2006-12-18 10:16:21] DEBUG Configuration - processing foreign key constraints
BUILD FAILED
C:\projects\test\build-dbschema.xml:56: Schema text failed: component class not found: org.test.InstitutionBaseId
This message didn't make sense, since the file IS there:
Quote:
$ ls src/org/test/
CVS InstitutionBase.hbm.xml InstitutionBase.java InstitutionBaseId.java
$ ls build/classes/org/test/
InstitutionBase.class InstitutionBase.hbm.xml InstitutionBaseId.class
It's in the same directory as both the mapping file and InstitutionBase, which works just fine. As well, if I revert the mapping file, everything runs fine again. Once again, I just assumed this is a generic message when things aren't right in the mapping file.
My third attempt was this:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.test.InstitutionBase" table="InstitutionBase">
<composite-id name="id" class="org.test.InstitutionBaseId">
<key-property name="institutionNumber"/>
<key-property name="productType"/>
</composite-id>
<component name="testIdClass" class="org.test.InstitutionBaseId">
<property name="institutionNumber" type="string" column="institutionNumber" not-null="true"/>
<property name="productType" type="string" column="productType" not-null="true"/>
</component>
</class>
</hibernate-mapping>
Which resulted in:
Quote:
[exportschema] [2006-12-18 10:20:40] INFO Environment - Hibernate 3.1.3
[exportschema] [2006-12-18 10:20:40] INFO Environment - hibernate.properties not found
[exportschema] [2006-12-18 10:20:40] INFO Environment - using CGLIB reflection optimizer
[exportschema] [2006-12-18 10:20:40] INFO Environment - using JDK 1.4 java.sql.Timestamp handling
[exportschema] [2006-12-18 10:20:41] INFO Configuration - Reading mappings from file: C:\projects\test\build\classes\org\test\InstitutionBase.hbm.xml
[exportschema] [2006-12-18 10:20:41] DEBUG DTDEntityResolver - trying to resolve system-id [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]
[exportschema] [2006-12-18 10:20:41] DEBUG DTDEntityResolver - recognized hibernate namespace; attempting to resolve on classpath under org/hibernate/
[exportschema] [2006-12-18 10:20:41] DEBUG DTDEntityResolver - located [http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd] in classpath
[exportschema] [2006-12-18 10:20:41] INFO HbmBinder - Mapping class: org.test.InstitutionBase -> InstitutionBase
BUILD FAILED
C:\projects\test\build-dbschema.xml:56: Schema text failed: Could not read mapping document from file: C:\projects\test\build\classes\org\test\InstitutionBase.hbm.xml
At this point I'm not sure what to do. I simply want to create a table that has a composite key made from institutionNumber and productType.
These fields map to entities outside of the system, so there are no classes to map them to. They simply are there, and must be used to build the key for InstitutionBase, generating a schema like:
Code:
create table InstitutionBase
(
institutionNumber varchar(255) not null,
productType varchar(255) not null,
primary key (institutionNumber, productType)
);
Can anyone help?