Ok, I'll give you a simple example of a many-to-many relationship where you have some
extra properties in the "joining" table and want to treat
this as an entity of its own. In the example you have a person who subscribes to some
magazines. There is a many-to-many relationship between magazines and persons. The
subscription contains, except the keys of a person and a magazine, a startdate and an
enddate of the subscription (here treated as longs). In the example the relation is
not set to be bi-directional, but it's simple to add this if you wish. So, from a
Person you can get the magizines he/she subscribes on. I haven't actually tried to run
this code through xdoclet but I think it will work, or at least you get the idea how
it works.
Another thing...while sitting and playing with Hibernate and xdoclet i missed a
template for generating the hibernate.cfg.xml file so I build one. This mail is
already pretty long so I don't attach it here. But if anyone is interested just send
another mail to the list and I'll give it to you.
Person
------
/**
* @hibernate.class
* table="person"
*/
public class Person {
private long id = -1;
private String name = null;
private String address = null;
private Set subscriptions = null;
/**
* @hibernate.id
* generator-class="assigned"
* type="long"
* column="id"
*/
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
/**
* @hibernate.property
* column="name"
* type="java.lang.String"
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* @hibernate.property
* column="address"
* type="java.lang.String"
*/
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
/**
* @hibernate.set
* lazy="true"
* @hibernate.collection-composite-element
* class="Subscription"
* @hibernate.collection-key
* column="person_id"
*/
public Set getSubscriptions() {
return subscriptions;
}
public void setSubscriptions(Set subscriptions)
this.subscriptions = subscriptions;
}
}
Subscription
-------------
public class Subscription {
private long fromDate = -1;
private long toDate = -1;
private Magazine magazine = null;
/**
* @hibernate.property
* column="fromdate"
* type="long"
*/
public long getFromDate() {
return fromDate
}
public long setFromDate(long fromDate) {
this.fromDate = fromDate;
}
/**
* @hibernate.property
* column="todate"
* type="long"
*/
public long getToDate() {
return toDate
}
public long setToDate(long toDate) {
this.toDate = toDate;
}
/**
* @hibternate.many-to-one
* column="function_id"
*/
public Magzine getMagazine() {
return magazine;
}
public void setMagazine(Magazine magazine) {
this.magazine = magazine;
}
}
Magazine
--------
/**
* @hibernate.class
* table="magazine"
*/
public class Magazine {
private long id = -1;
private String name = null;
**
* @hibernate.id
* generator-class="assigned"
* type="long"
* column="id"
*/
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
/**
* @hibernate.property
* column="name"
* type="java.lang.String"
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Running this through xdoclet should generate a file, for Person (person.hmb.xml), like
this:
<?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>
<class name="Person" table="person" dynamic-update="false" dynamic-insert="false">
<id name="id" column="id" type="long">
<generator class="assigned"></generator>
</id>
<property name="name" type="java.lang.String" update="true" insert="true"
column="name" />
<set name="subsrciptions" lazy="true" inverse="false" cascade="none"
sort="unsorted">
<key column="personid" />
<composite-element class="Subscription">
<property name="startDate" type="long" update="true"
insert="true" column="startdate" />
<property name="endDate" type="long" update="true"
insert="true" column="enddate" />
<many-to-one
name="magazine"
class="Magazine"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="magazineid" />
</composite-element>
</set>
</class>
</hibernate-mapping>
Good luck and let me know if you're having any problems.
/Klas
>
> From: Mark Lowe <[EMAIL PROTECTED]>
> Date: 2004/02/10 ti PM 06:19:40 GMT
> To: [EMAIL PROTECTED]
> �mne: Re: [Xdoclet-user] Many to many with Hibernate
>
> Yeah
>
> I wouldn't mind having a butchers.. (uk idiom for take a look).
>
>
>
> On 10 Feb 2004, at 12:29, kaxell wrote:
>
> > Hi,
> >
> > Thanks for your replys, but the trick I used to get it working was
> > with the "@hibernate.collection-composite-element" tag. If anyone is
> > interested in seeing how it works just reply to this message and I'll
> > put an example on the list.
> >
> > /Klas
> >
> >
> >>
> >> From: "Shishir K. Singh" <[EMAIL PROTECTED]>
> >> Date: 2004/02/06 fr PM 04:23:22 GMT
> >> To: <[EMAIL PROTECTED]>
> >> �mne: RE: [Xdoclet-user] Many to many with Hibernate
> >>
> >> I would say that have a look at the test suite that comes with the
> >> xdoclet src and look for the hibernate examples. I think that I saw
> >> something similar to what you need. I too had the same concerns but I
> >> decided to opt the easy way out (due to lack of time) by putting id
> >> in
> >> each of my assocition class (that joins the two many to many class).
> >>
> >>
> >>
> >> -----Original Message-----
> >> From: [EMAIL PROTECTED]
> >> [mailto:[EMAIL PROTECTED] On Behalf Of kaxell
> >> Sent: Friday, February 06, 2004 10:47 AM
> >> To: [EMAIL PROTECTED]
> >> Subject: [Xdoclet-user] Many to many with Hibernate
> >>
> >> Hi,
> >>
> >> I'm using xdoclet to generate my hibernate mapping files but I have a
> >> problem when I'm trying to generate a many to many relationship. I
> >> have
> >> two tables which have a many to many relationship. They are linked to
> >> eachother with a third table which also contains some extra properties
> >> except the keys of the others. I have three separate classes and I'm
> >> using a many-to-one relationship in the linking table to the other two
> >> and from the two I'm using a one-to-many relationship. When I try to
> >> generate the mapping files I get an exception which says that my
> >> linking
> >> table is missing an id.
> >>
> >> What I understand is that I haven't got it right in the xdoclet tags
> >> but
> >> I don't know how to fix it. Does anyone have any suggestions or
> >> examples
> >> how to solve this? The doclet is looking something like this:
> >>
> >> /**
> >> * @hibernate.set
> >> * lazy="true"
> >> * inverse="true"
> >> * @hibernate.collection-key
> >> * column="colId"
> >> * @hibernate.collection-one-to-many
> >> * class="TheRelClass"
> >> *
> >> */
> >>
> >> --------------
> >>
> >> /**
> >> * @hibernate.many-to-one
> >> * column="colId"
> >> * class="TheManyClass"
> >> *
> >> */
> >>
> >>
> >> Thanks in advance.
> >>
> >> /K
> >>
> >>
> >>
> >> -------------------------------------------------------
> >> The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference
> >> on
> >> Open Tools Development and Integration See the breadth of Eclipse
> >> activity. February 3-5 in Anaheim, CA.
> >> http://www.eclipsecon.org/osdn
> >> _______________________________________________
> >> xdoclet-user mailing list
> >> [EMAIL PROTECTED]
> >> https://lists.sourceforge.net/lists/listinfo/xdoclet-user
> >>
> >>
> >> -------------------------------------------------------
> >> The SF.Net email is sponsored by EclipseCon 2004
> >> Premiere Conference on Open Tools Development and Integration
> >> See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
> >> http://www.eclipsecon.org/osdn
> >> _______________________________________________
> >> xdoclet-user mailing list
> >> [EMAIL PROTECTED]
> >> https://lists.sourceforge.net/lists/listinfo/xdoclet-user
> >>
> >
> >
> >
> > -------------------------------------------------------
> > The SF.Net email is sponsored by EclipseCon 2004
> > Premiere Conference on Open Tools Development and Integration
> > See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
> > http://www.eclipsecon.org/osdn
> > _______________________________________________
> > xdoclet-user mailing list
> > [EMAIL PROTECTED]
> > https://lists.sourceforge.net/lists/listinfo/xdoclet-user
> >
>
>
>
> -------------------------------------------------------
> The SF.Net email is sponsored by EclipseCon 2004
> Premiere Conference on Open Tools Development and Integration
> See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
> http://www.eclipsecon.org/osdn
> _______________________________________________
> xdoclet-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/xdoclet-user
>
-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
xdoclet-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-user