-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 
Author Message
 Post subject: Middlegen: single pk column, mapping (ont-to-many), javatype
PostPosted: Wed Feb 18, 2004 3:23 pm 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
Hello!

I use PostgreSQL 7.4, hibernate cvs head and middlegen 2.0-vo.

I have three tables created and would like to create the java-object using middlegen and hbm2java as ant-tasks.
I have replace HibernateJavaTypeMapper to return primitives only.
I use a custom DbNameConverter to strip the prefix (Wxx_) from the column name.

But i have some problems:

1) if a table do have a primary key with only one column, middlegen
generates only a id field in the resulting hbm, but i would like to have a compound-key always.
I have looked deep into the source of middlegen and for me it looks like there is not change to force this behaviour.
Any hint?

2) As i wrote, I have replace the HibernateJavaTypeMapper with my own version to force primitives even if the column is nullable.
This works with simple columns, but fails on key-columns.
Maybe it is not a good strategy to use primitives only, but i am forced to this to be compatible with another application.
Any hint?

3) The constraints are mapped to one-to-one and one-to-many.
Please have a look on the "Credit" class, there you will see that hbm2java generates two fields with name "client".
For sure, this results from the custom DbNameConverter however, as far as i understand, the first "Long client" should not exist due to it is a constraint to the table "Client".
It should be like "PlayJournal" where the column "Long Client" in "PlayJournalPK" is correctly replace by "db.Client".
So maybe this has something to do with 1)

Sorry for this large post, however, is one could give me a direction where to go, i am willing to post some patches to middlegen if necessary.

Thank you,
Mario



CREATE TABLE public.w00_credit
(
w00_client int8 NOT NULL,
w00_credit int8,
w00_no_crd_s int8,
w00_no_crd_l int8,
w00_last_transaction timestamp,
w00_first_transaction timestamp,
CONSTRAINT w00_credits_pkey PRIMARY KEY (w00_client),
CONSTRAINT w00_w02_client FOREIGN KEY (w00_client) REFERENCES public.w02_client (w02_client) ON UPDATE RESTRICT ON DELETE RESTRICT
) WITH OIDS;

CREATE TABLE public.w01_play_journal
(
w01_client int8 NOT NULL,
w01_time timestamp NOT NULL,
w01_song_id varchar(255),
CONSTRAINT w01_play_journal_pkey PRIMARY KEY (w01_client, w01_time),
CONSTRAINT w01_w02_client FOREIGN KEY (w01_client) REFERENCES public.w02_client (w02_client) ON UPDATE RESTRICT ON DELETE RESTRICT
) WITH OIDS;


CREATE TABLE public.w02_client
(
w02_client int8 NOT NULL,
w02_name varchar(255),
CONSTRAINT w02_client_pkey PRIMARY KEY (w02_client)
) WITHOUT OIDS;



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/

hibernate.vm extended:
dynamic-insert
-->

<class
name="db.Client"
table="w02_client"
dynamic-update="true"
dynamic-insert="true"
>

<id
name="client"
type="long"
column="w02_client"
>
<generator class="assigned" />
</id>

<property
name="name"
type="java.lang.String"
column="w02_name"
length="255"
/>

<!-- associations -->
<!-- bi-directional one-to-many association to PlayJournal -->
<set
name="playJournals"
lazy="true"
inverse="true"
>
<key>
<column name="w01_client" />
</key>
<one-to-many
class="db.PlayJournal"
/>
</set>
<!-- bi-directional one-to-one association to Credit -->
<one-to-one
name="credit"
class="db.Credit"
outer-join="auto"
/>

</class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/

hibernate.vm extended:
dynamic-insert
-->

<class
name="db.Credit"
table="w00_credit"
dynamic-update="true"
dynamic-insert="true"
>

<id
name="client"
type="long"
column="w00_client"
>
<generator class="assigned" />
</id>

<property
name="credit"
type="long"
column="w00_credit"
length="8"
/>
<property
name="noCrdS"
type="long"
column="w00_no_crd_s"
length="8"
/>
<property
name="noCrdL"
type="long"
column="w00_no_crd_l"
length="8"
/>
<property
name="lastTransaction"
type="java.sql.Timestamp"
column="w00_last_transaction"
length="8"
/>
<property
name="firstTransaction"
type="java.sql.Timestamp"
column="w00_first_transaction"
length="8"
/>

<!-- associations -->
<!-- bi-directional one-to-one association to Client -->
<one-to-one
name="client"
class="db.Client"
outer-join="auto"
constrained="true"
/>

</class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/

hibernate.vm extended:
dynamic-insert
-->

<class
name="db.PlayJournal"
table="w01_play_journal"
dynamic-update="true"
dynamic-insert="true"
>

<composite-id name="comp_id" class="db.PlayJournalPK">
<key-property
name="time"
column="w01_time"
type="java.sql.Timestamp"
length="8"
/>
<!-- bi-directional many-to-one association to Client -->
<key-many-to-one
name="client"
class="db.Client"
>
<column name="w01_client" />
</key-many-to-one>
</composite-id>

<property
name="songId"
type="java.lang.String"
column="w01_song_id"
length="255"
/>

<!-- associations -->

</class>
</hibernate-mapping>


package db;

import java.io.Serializable;
import java.util.Set;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class Client implements java.io.Serializable {

/** identifier field */
private Long client;

/** nullable persistent field */
private String name;

/** nullable persistent field */
private db.Credit credit;

/** persistent field */
private Set playJournals;

/** full constructor */
public Client(Long client, String name, db.Credit credit, Set playJournals) {
this.client = client;
this.name = name;
this.credit = credit;
this.playJournals = playJournals;
}

/** default constructor */
public Client() {
}

/** minimal constructor */
public Client(Long client, Set playJournals) {
this.client = client;
this.playJournals = playJournals;
}

public Long getClient() {
return this.client;
}

public void setClient(Long client) {
this.client = client;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public db.Credit getCredit() {
return this.credit;
}

public void setCredit(db.Credit credit) {
this.credit = credit;
}

public Set getPlayJournals() {
return this.playJournals;
}

public void setPlayJournals(Set playJournals) {
this.playJournals = playJournals;
}

public String toString() {
return new ToStringBuilder(this)
.append("client", getClient())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof Client) ) return false;
Client castOther = (Client) other;
return new EqualsBuilder()
.append(this.getClient(), castOther.getClient())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getClient())
.toHashCode();
}

}


package db;

import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class Credit implements java.io.Serializable {

/** identifier field */
private Long client;

/** nullable persistent field */
private long credit;

/** nullable persistent field */
private long noCrdS;

/** nullable persistent field */
private long noCrdL;

/** nullable persistent field */
private Date lastTransaction;

/** nullable persistent field */
private Date firstTransaction;

/** nullable persistent field */
private db.Client client;

/** full constructor */
public Credit(Long client, long credit, long noCrdS, long noCrdL, Date lastTransaction, Date firstTransaction, db.Client client) {
this.client = client;
this.credit = credit;
this.noCrdS = noCrdS;
this.noCrdL = noCrdL;
this.lastTransaction = lastTransaction;
this.firstTransaction = firstTransaction;
this.client = client;
}

/** default constructor */
public Credit() {
}

/** minimal constructor */
public Credit(Long client) {
this.client = client;
}

public Long getClient() {
return this.client;
}

public void setClient(Long client) {
this.client = client;
}

public long getCredit() {
return this.credit;
}

public void setCredit(long credit) {
this.credit = credit;
}

public long getNoCrdS() {
return this.noCrdS;
}

public void setNoCrdS(long noCrdS) {
this.noCrdS = noCrdS;
}

public long getNoCrdL() {
return this.noCrdL;
}

public void setNoCrdL(long noCrdL) {
this.noCrdL = noCrdL;
}

public Date getLastTransaction() {
return this.lastTransaction;
}

public void setLastTransaction(Date lastTransaction) {
this.lastTransaction = lastTransaction;
}

public Date getFirstTransaction() {
return this.firstTransaction;
}

public void setFirstTransaction(Date firstTransaction) {
this.firstTransaction = firstTransaction;
}

public db.Client getClient() {
return this.client;
}

public void setClient(db.Client client) {
this.client = client;
}

public String toString() {
return new ToStringBuilder(this)
.append("client", getClient())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof Credit) ) return false;
Credit castOther = (Credit) other;
return new EqualsBuilder()
.append(this.getClient(), castOther.getClient())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getClient())
.toHashCode();
}

}



package db;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class PlayJournal implements java.io.Serializable {

/** identifier field */
private db.PlayJournalPK comp_id;

/** nullable persistent field */
private String songId;

/** full constructor */
public PlayJournal(db.PlayJournalPK comp_id, String songId) {
this.comp_id = comp_id;
this.songId = songId;
}

/** default constructor */
public PlayJournal() {
}

/** minimal constructor */
public PlayJournal(db.PlayJournalPK comp_id) {
this.comp_id = comp_id;
}

public db.PlayJournalPK getComp_id() {
return this.comp_id;
}

public void setComp_id(db.PlayJournalPK comp_id) {
this.comp_id = comp_id;
}

public String getSongId() {
return this.songId;
}

public void setSongId(String songId) {
this.songId = songId;
}

public String toString() {
return new ToStringBuilder(this)
.append("comp_id", getComp_id())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof PlayJournal) ) return false;
PlayJournal castOther = (PlayJournal) other;
return new EqualsBuilder()
.append(this.getComp_id(), castOther.getComp_id())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getComp_id())
.toHashCode();
}

}



package db;

import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class PlayJournalPK implements java.io.Serializable {

/** identifier field */
private Date time;

/** identifier field */
private db.Client client;

/** full constructor */
public PlayJournalPK(Date time, db.Client client) {
this.time = time;
this.client = client;
}

/** default constructor */
public PlayJournalPK() {
}

public Date getTime() {
return this.time;
}

public void setTime(Date time) {
this.time = time;
}

public db.Client getClient() {
return this.client;
}

public void setClient(db.Client client) {
this.client = client;
}

public String toString() {
return new ToStringBuilder(this)
.append("time", getTime())
.append("client", getClient())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof PlayJournalPK) ) return false;
PlayJournalPK castOther = (PlayJournalPK) other;
return new EqualsBuilder()
.append(this.getTime(), castOther.getTime())
.append(this.getClient(), castOther.getClient())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getTime())
.append(getClient())
.toHashCode();
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 3:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
What do you mean you are using CVS head? No-one should be using the main branch. The lastest version of Hibernate is in v21branch.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 3:29 pm 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
you are right, i use "v21branch", sorry for this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 9:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Hi,
Quote:
1) if a table do have a primary key with only one column, middlegen
generates only a id field in the resulting hbm, but i would like to have a compound-key always.


To generate compound keys even when the the key is singular you will need to edit the hibernate hbm velocity template that comes with Middlegen hibernate plugin. Its should not turn out to be hard to do.

Quote:
2) As i wrote, I have replace the HibernateJavaTypeMapper with my own version to force primitives even if the column is nullable.
This works with simple columns, but fails on key-columns.

Cannot have nullable primatives. Recommend you use Objects and have non-mapped methods that delegate to the pirmary key for legacy compatibility. You will then be able to include your own conversion logic of the null to a valid value.

Quote:
3) The constraints are mapped to one-to-one and one-to-many.
Please have a look on the "Credit" class, there you will see that hbm2java generates two fields with name "client".

I can only assume that your Middlegen Name generation changes has caused the ID and a property field to have the same name. You need to check your changes to the code.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 2:45 am 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
david wrote:
Quote:
3) The constraints are mapped to one-to-one and one-to-many.
Please have a look on the "Credit" class, there you will see that hbm2java generates two fields with name "client".

I can only assume that your Middlegen Name generation changes has caused the ID and a property field to have the same name. You need to check your changes to the code.


You are right and i know this, but the question is, why is in case of the class db.Credit a column "Long client" (=key) and a Column "db.Client client" (=foreign key) generated.
IMHO the column "Long client" should not be generated by hbm2java.

Please have a look at PlayJournal where there exists a PlayJournalPK. Here you will find the column client too, but only a "db.Client client" column as expected.

It looks like some (for me buggy) difference between the handling of id->foreign-key and compound-key-column->foreign-key.

Well, i try to change the template to fulfill 1) and maybe 3) might be gone then too.

Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 4:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The names generated (if you don't play with the naming algorithm) are based on table and property names. It does not look for duplicates within a table or in the associations. It is a good idea to have a naming convention that would avoid naming conflicts.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 4:49 am 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
It looks like i miss something fundamental.

What i would like to say is, that the only "Long client" column should exists in the table "client".

But hbm2java creates in Credit this column twice:

public Credit(Long client, long credit, long noCrdS, long noCrdL, Date lastTransaction, Date firstTransaction, db.Client client)

In PlayJournalPK this association is solved as expected:

public PlayJournalPK(Date time, db.Client client)


The difference is in Credit.hbm. Middlegen create an id column and an EXTRA one-to-one mapping. But this IS the same column.

<id name="client" type="long" column="w00_client" >
<generator class="assigned" />
</id>
<one-to-one name="client" class="db.Client" outer-join="auto" constrained="true" />


In PlayJournalPK the many-to-one mapping is correctly integrated in the composite-id

<composite-id name="comp_id" class="db.PlayJournalPK">
<key-property name="time" column="w01_time" type="java.sql.Timestamp" length="8"/>
<key-many-to-one name="client" class="db.Client">
<column name="w01_client" />
</key-many-to-one>
</composite-id>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 4:04 pm 
Regular
Regular

Joined: Thu Dec 11, 2003 4:14 pm
Posts: 86
Location: Hibernate 3 + annotations, Oracle 9i, PostgreSQL 8.0, Java 1.5.0
Well, it looks like if have found a workaround.
However i think there is a bug with foreign key one-to-one relationships between two primary keys (with a single column only).
This resulting java classes have added this column twice.
One time for the key and a second for the fk-constraint.


Now the workaround. Code changes are red.

*) force composite Key
*) disable: Pure One-to-one roles only total primary key to primary key
*) use dynamic-insert (not related to this thread)


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/


hibernate.vm extended:
dynamic-insert
force compound-key
disable one-to-one mapping for key-only-fields

-->

<class
name="${table.package}.${table.destinationClassName}"
table="$table.sqlName"
#if($table.schemaName)
schema="${table.databaseSchema}"
#end
#if(!$table.mutable)
mutable="false"
#end
#if($table.proxy)
proxy="${table.package}.${table.destinationClassName}"
#end
#if($table.dynamicUpdate)
dynamic-update="true"

dynamic-insert="true"

#end
#if($table.persisterName)
persister="${table.persister}"
#end
>
#if($table.classDescriptionName)
<meta attribute="class-description" inherit="false">
${table.classDescription}
</meta>
#end
#if ($plugin.genXDocletTags)
<meta attribute="class-description" inherit="false">
@hibernate.class
table="$table.sqlName"
#if($table.schemaName)
schema="${table.databaseSchema}"
#end
#if(!$table.mutable)
mutable="false"
#end
#if($table.dynamicUpdate)
dynamic-update="true"
#end
#if($table.proxy)
proxy="${table.package}.${table.destinationClassName}"
#end
</meta>
#end
#if($table.extendsName)
<meta attribute="extends" inherit="false">${table.extends}</meta>
#end
#if($table.implementLifecycle)
<meta attribute="implements" inherit="false">net.sf.hibernate.Lifecycle</meta>
#end
#if($table.implementValidatable)
<meta attribute="implements" inherit="false">net.sf.hibernate.Validatable</meta>
#end
####### Generate all the extra (non hibernate) implements meta data
#foreach ( $implClass in $table.implements )
<meta attribute="implements" inherit="false">${implClass}</meta>
#end
#if($table.classScopeName)
<meta attribute="scope-class" inherit="false">${table.classScope}</meta>
#end
#set ($VersionId = true)

## Case where we have a composite key

## FORCE COMPOSITE KEY
## #if ($table.compositeKey)
#if (1 == 1)

## Special case where there is no primary key (bad database design).
## In this case we put all the properties in a composite id.
#if ($table.primaryKeyColumns.size() == 0 )
<composite-id>
#if ($plugin.genXDocletTags)
<meta attribute="class-description" inherit="false">
@hibernate.id
generator-class="assigned"
</meta>
#end
#foreach ( $compKeyColumn in $table.basicColumns )
<key-property
name="${compKeyColumn.variableName}"
column="${compKeyColumn.sqlName}"
type="${compKeyColumn.javaType}"
#if ($compKeyColumn.sized)
length="$compKeyColumn.size"
#end
#if ($plugin.genXDocletTags)
>
<meta attribute="field-description">
@hibernate.property
column="${compKeyColumn.sqlName}"
#if ($column.sized)
length="$compKeyColumn.size"
#end
</meta>
</key-property>
#else
/>
#end
#end
#else
#if ($plugin.genIntergratedCompositeKeys)
<composite-id>
#else
<composite-id name="comp_id" class="${table.package}.${table.compoundKeyDestinationClassName}">
#end
#if ($plugin.genXDocletTags)
<meta attribute="class-description" inherit="false">
@hibernate.id
generator-class="assigned"
</meta>
#end
#foreach ( $compKeyColumn in $table.primaryNotForeignKeyColumns )
<key-property
name="${compKeyColumn.variableName}"
column="${compKeyColumn.sqlName}"
type="${compKeyColumn.javaType}"
#if ($compKeyColumn.sized)
length="$compKeyColumn.size"
#end
#if ($plugin.genXDocletTags)
>
<meta attribute="field-description">
@hibernate.property
column="${compKeyColumn.sqlName}"
#if ($column.sized)
length="$compKeyColumn.size"
#end
</meta>
</key-property>
#else
/>
#end
#end
#end
## Go across all relationships that are part of the primary key
#foreach($relationshipRole in $table.primaryKeyRoles)
#set( $target = $relationshipRole.getTarget($plugin))
#if ($relationshipRole.enabled)
<!-- $table.getDirectionality($relationshipRole) $table.getCardinalityValue($relationshipRole) association to $target.destinationClassName -->
<key-many-to-one
name="$table.getVariableName($relationshipRole)"
class="${target.package}.${target.destinationClassName}"
>
#if ($plugin.genXDocletTags)
#foreach( $columnMap in $relationshipRole.targetRole.columnMaps )
<meta attribute="field-description">
@hibernate.many-to-one
column="$columnMap.foreignKey""
</meta>
#end
#end
#foreach( $columnMap in $relationshipRole.targetRole.columnMaps )
<column name="$columnMap.foreignKey" />
#end
</key-many-to-one>
#else
## Inactive end of a uni-directional foreign key in as a part of the
## compound key.
#foreach ( $InactiveFKeyColumn in $table.getColsInactive($relationshipRole) )
<key-property
name="${InactiveFKeyColumn.variableName}"
column="${InactiveFKeyColumn.sqlName}"
type="${InactiveFKeyColumn.javaType}"
#if ($InactiveFKeyColumn.sized)
length="$InactiveFKeyColumn.size"
#end
#if ($plugin.genXDocletTags)
>
<meta attribute="field-description">
@hibernate.property
column="${InactiveFKeyColumn.sqlName}"
#if ($column.sized)
length="$InactiveFKeyColumn.size"
#end
</meta>
</key-property>
#else
/>
#end
#end
#end
#end
</composite-id>
#else
#set ($column = $table.pkColumn)
<id
name="${column.variableName}"
type="$column.javaType"
column="$column.sqlName"
#if ($table.generatorUnsavedValue)
unsaved-value="$table.generatorUnsavedValue"
#end
>
#if ($plugin.genXDocletTags)
<meta attribute="field-description">
@hibernate.id
generator-class="$table.keyGenerator"
type="$column.javaType"
column="$column.sqlName"
#if ($table.generatorUnsavedValue)
unsaved-value="$table.generatorUnsavedValue"
#end

#if ($table.generatorRequiresParams)
#if ($table.keyGenerator == "sequence")
@hibernate.generator-param
name="$table.keyGenerator"
value="$table.keyGeneratorArg"
#elseif ($table.keyGenerator == "hilo")
@hibernate.generator-param
name="table"
value="$table.keyGeneratorArg"
@hibernate.generator-param
name="column"
value="next_value"
@hibernate.generator-param
name="max_lo"
value="100"
#elseif ($table.keyGenerator == "seqhilo")
@hibernate.generator-param
name="sequence"
value="$table.keyGeneratorArg"
@hibernate.generator-param
name="max_lo"
value="100"
#end
#end
</meta>
#end
#if (!$table.generatorRequiresParams)
<generator class="$table.keyGenerator" />
#else
<generator class="$table.keyGenerator">
#if ($table.keyGenerator == "sequence")
<param name="sequence">$table.keyGeneratorArg</param>
#elseif ($table.keyGenerator == "hilo")
<param name="table">$table.keyGeneratorArg</param>
<param name="column">next_value</param>
<param name="max_lo">100</param>
#elseif ($table.keyGenerator == "seqhilo")
<param name="sequence">$table.keyGeneratorArg</param>
<param name="max_lo">100</param>
#end
</generator>
#end
</id>
#end
#if ($table.versioningPresent && $VersionId)
#set ($VersionId = false)
#set ($versionCol = $table.versioningField)

#if ($versionCol.specialtyVersionProperty)
<version
name="${versionCol.variableName}"
type="$versionCol.javaType"
column="$versionCol.sqlName"
#if ($plugin.genXDocletTags)
>
<meta attribute="field-description">
@hibernate.version
column="$versionCol.sqlName"
</meta>
</version>
#else
/>
#end
#elseif ($versionCol.specialtyTimestampProperty)
<timestamp
name="${versionCol.variableName}"
column="$versionCol.sqlName"
#if ($plugin.genXDocletTags)
>
<meta attribute="field-description">
@hibernate.timestamp
column="$versionCol.sqlName"
</meta>
</timestamp>
#else
/>
#end
#end
#end

## Special case where there is no primary key
## In this case all the properties have been included in the composite id so
## we do not generate them another time.
#if ($table.primaryKeyColumns.size() != 0 )
#foreach ($column in $table.notKeyColumns )
#if ($column.specialtyProperty)
<property
name="${column.variableName}"
type="$column.javaType"
column="$column.sqlName"
#if (!$column.updateable)
update="false"
#end
#if (!$column.insertable)
insert="false"
#end
#if (!$column.nullable)
not-null="true"
#end
#if ($column.unique)
unique="true"
#end
#if ($column.sized)
length="$column.size"
#end
#if ($column.metaData || $plugin.genXDocletTags)
>
#if($column.fieldDescriptionName)
<meta attribute="field-description">
${column.fieldDescription}
</meta>
#end
#if ($column.includeToString)
<meta attribute="use-in-tostring">true</meta>
#end
#if (!$column.genProperty)
<meta attribute="gen-property">false</meta>
#end
#if($column.fieldScopeName)
<meta attribute="scope-field">${column.fieldScope}</meta>
#end
#if($column.fieldGetScopeName)
<meta attribute="scope-get">${column.fieldGetScope}</meta>
#end
#if($column.fieldSetScopeName)
<meta attribute="scope-set">${column.fieldSetScope}</meta>
#end
#if($column.beanPropertyTypeName)
<meta attribute="beans-property-type">${column.beanPropertyType}</meta>
#end
#if ($plugin.genXDocletTags)
<meta attribute="field-description">
@hibernate.property
column="$column.sqlName"
#if ($column.sized)
length="$column.size"
#end
#if (!$column.nullable)
not-null="true"
#end
</meta>
#end
</property>
#else
/>
#end
#end
#end
#end

<!-- associations -->

##DISABLE KEY-BASE ONE-TO-ONE
#if (1 == 0)

####### Pure One-to-one roles only total primary key to primary key roles.
#foreach($relationshipRole in $table.childSidePrimaryKeyOnlyOneToOneRoles)
#set( $target = $relationshipRole.getTarget($plugin))
## The check only works from one direction so this is reversed.
#if ($target.isRoleOneToOnePrimaryKeys($relationshipRole.targetRole, $table))
<!-- $table.getDirectionality($relationshipRole) $table.getCardinalityValue($relationshipRole) association to $target.destinationClassName -->
<one-to-one
name="$table.getVariableName($relationshipRole)"
class="${target.package}.${target.destinationClassName}"
outer-join="auto"
constrained="true"
#if ($plugin.genXDocletTags)
>
<meta attribute="field-description">
@hibernate.one-to-one
class="${target.package}.${target.destinationClassName}"
outer-join="auto"
constrained="true"
</meta>
</one-to-one>
#else
/>
#end
#end
#end

##DISABLE KEY-BASE ONE-TO-ONE
#end

####### Non primary key based roles
#foreach($relationshipRole in $table.notPrimaryKeyRoles)
#if( $relationshipRole.enabled)
#set( $one2one = $relationshipRole.relation.one2One )
#set( $many2many = $relationshipRole.relation.many2Many )
#set( $bidirectional = $relationshipRole.relation.bidirectional )
#set( $target = $relationshipRole.getTarget($plugin))
########## Compute cardinality strings for javadocs (useful for humans)
<!-- $table.getDirectionality($relationshipRole) $table.getCardinalityValue($relationshipRole) association to $target.destinationClassName -->
#if( !$relationshipRole.targetMany )
## With the test $relationshipRole.originPrimaryKey we generate one2one
## relationships not based on the primary key only as many-to-one relations
## on one side and property-ref on the other.
#if( ($one2one && $relationshipRole.originPrimaryKey) || ($one2one && $table.isRoleOneToOnePrimaryKeys($relationshipRole, $target)))
<one-to-one
name="$table.getVariableName($relationshipRole)"
class="${target.package}.${target.destinationClassName}"
outer-join="auto"
#if(! $table.isRoleOneToOnePrimaryKeys($relationshipRole, $target))
#if ($bidirectional)
property-ref="$target.getVariableName($relationshipRole.targetRole)"
#else
### Should only be one.
#foreach( $columnMap in $relationshipRole.columnMaps )
property-ref="$target.getColumn($columnMap.foreignKey).variableName"
#end
#end
#end
#if ($plugin.genXDocletTags)
>
<meta attribute="field-description">
@hibernate.one-to-one
outer-join="auto"
#if(! $table.isRoleOneToOnePrimaryKeys($relationshipRole, $target))
#if ($bidirectional)
property-ref="$target.getVariableName($relationshipRole.targetRole)"
#else
### Should only be one.
#foreach( $columnMap in $relationshipRole.columnMaps )
property-ref="$target.getColumn($columnMap.foreignKey).variableName"
#end
#end
#end
</meta>
</one-to-one>
#else
/>
#end
#else
<many-to-one
name="$table.getVariableName($relationshipRole)"
class="${target.package}.${target.destinationClassName}"
#if ($bidirectional && !$one2one)
not-null="true"
#end
## This is the case of one-to-one relationships not based on the pk.
#if ($bidirectional && $one2one)
unique="true"
#end
>
#if ($plugin.genXDocletTags)
<meta attribute="field-description">
@hibernate.many-to-one
#if ($bidirectional && !$one2one)
not-null="true"
#end
## This is the case of one-to-one relationships not based on the pk.
#if ($bidirectional && $one2one)
unique="true"
#end
#foreach( $columnMap in $relationshipRole.targetRole.columnMaps )
@hibernate.column name="$columnMap.foreignKey"
#end
</meta>
#end
#foreach( $columnMap in $relationshipRole.targetRole.columnMaps )
<column name="$columnMap.foreignKey" />
#end
</many-to-one>
#end
#else
<set
name="$table.getVariableName($relationshipRole)"
lazy="true"
#if( !$many2many && $bidirectional )
inverse="true"
#end
#if( $many2many )
table="$relationshipRole.relation.joinTable.sqlName"
#end
>
#if ($plugin.genXDocletTags)
<meta attribute="field-description">
@hibernate.set
lazy="true"
#if( !$many2many && $bidirectional )
inverse="true"
#end
#if( $many2many )
table="$relationshipRole.relation.joinTable.sqlName"
#end

#foreach( $columnMap in $relationshipRole.columnMaps )
@hibernate.collection-key
column="$columnMap.foreignKey"
#end

#if( !$many2many )
@hibernate.collection-one-to-many
class="${target.package}.${target.destinationClassName}"
#else
#foreach( $columnMap in $relationshipRole.targetRole.columnMaps )
@hibernate.collection-many-to-many
class="${target.package}.${target.destinationClassName}"
column="$columnMap.foreignKey"
#end
#end
</meta>
#end
<key>
#foreach( $columnMap in $relationshipRole.columnMaps )
<column name="$columnMap.foreignKey" />
#end
</key>
#if( !$many2many )
<one-to-many
#else
<many-to-many
#end
class="${target.package}.${target.destinationClassName}"
#if( $many2many)
>
#foreach( $columnMap in $relationshipRole.targetRole.columnMaps )
<column name="$columnMap.foreignKey" />
#end
</many-to-many>
#else
/>
#end
</set>
#end
#end
#end

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 10:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Glad you have found a way to satisfy your needs. This just proves the value of open source. I'm working on new features so I will review this area and your suggestions/observations. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 11:12 pm 
Newbie

Joined: Tue Mar 02, 2004 10:56 pm
Posts: 12
hello, i used middlegen plugin and have similar problem i believe.

inre this statement:
Quote:
The names generated (if you don't play with the naming algorithm) are based on table and property names. It does not look for duplicates within a table or in the associations. It is a good idea to have a naming convention that would avoid naming conflicts.


i think this is a problem i am encountering - i cannot find documentation on naming requirements - my problem is that when i access my app for the first time and tomcat is loading my mappings and classes, it stops loading once it hits my first mapping with a <set> in it - probably a naming problem - if i remove the <set> block, the classloader continues, until it hits my next <set>-burdened class.

can you point me to documentation on this - [/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 10:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
thomas,

Can you start a new topic and include a simple DDL and mapping document so we can see what might be wrong.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 1:30 pm 
Newbie

Joined: Tue Mar 02, 2004 10:56 pm
Posts: 12
thank you, will do - good idea.

actually, i just discovered my problem, though don't know whether it is documented somewhere - i have looked

The middlegen hbm mapping generation task creates a <meta attribute> element within the <composite-id> element. This does not appear to be legal. i believe i am using hte latest middlegen and i know i am using the 2.0 hibernate dtd from sourceforge - the top of the stack trace relevant to this error is:

2004-03-03 09:13:18,988 ERROR [TP-Processor2] cfg.Configuration (Configuration.java:276) - Could not configure datastore from
input stream
java.lang.NullPointerException
at net.sf.hibernate.util.StringHelper.qualify(StringHelper.java:239)
at net.sf.hibernate.cfg.Binder.bindComponent(Binder.java:734)


and in next stack it says:

2004-03-03 09:13:19,039 ERROR [TP-Processor2] cfg.Configuration (Configuration.java:248) - Could not compile the mapping docu
ment
net.sf.hibernate.MappingException: duplicate import: Eventgiftbundle


i am not sure what the exact issue is, but if i remove the meta attribtue tag, it goes away

!! i have created a new post for this at http://forum.hibernate.org/viewtopic.ph ... 08#2193108 !!

thank you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 7:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Most likely you have an old version of hibernate that does not accept/have the DTD modifications etc necessary to parse the extended meta tags added during the v2.1 beta development.


Top
 Profile  
 
 Post subject: Re: Middlegen: single pk column, mapping (ont-to-many), javatype
PostPosted: Sun Dec 23, 2012 9:00 pm 
Newbie

Joined: Thu Dec 20, 2012 11:11 pm
Posts: 6
Cool! Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.