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();
}
}