one of the biggest problem that we have found is the many-to-many relation. Here we can find a simple example that really :D work.
Assume that we want to create a relation between the table ITEM (that represents a book) and the table RESEARCHER that, as we can imagine, represents the author of a book.
Let us start our step by step guide.
1) create the item java beans like this
public class Item implements Serializable{
private int item_id;
private String type;
private String title;
private String description;
private String pub_date;
private Set<Researcher> writers;
private boolean deleted;
public Item(){
}
public int getItem_id() {
return item_id;
}
public void setItem_id(int item_id) {
this.item_id = item_id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPub_date() {
return pub_date;
}
public void setPub_date(String pub_date) {
this.pub_date = pub_date;
}
public Set<Researcher> getWriters() {
return writers;
}
public void setWriters(Set<Researcher> writers) {
this.writers = writers;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}
and now the researcher java bean
public class Researcher {
private int res_id;
private String fname;
private String lname;
private String email;
private int age;
private Entity entity;
private boolean deleted;
public Researcher() {
}
public int getRes_id() {
return res_id;
}
public void setRes_id(int res_id) {
this.res_id = res_id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Entity getEntity() {
return entity;
}
public void setEntity(Entity entity) {
this.entity = entity;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}
2) Let now Hibernate create its mapping file (researcher.hbm.xml and item.hbm.xml)
To do this step we need to install the Hibernate tool package for eclipse. We used Helios distribution of eclipse. In this case it is sufficient to go into Help -> Eclipse Marketplace and search Hibernate tool and install it.
Once we have this package installed, we just to right click on the package where we have our beans and select New -> Other... In the popup window select Hibernate -> Hibernate XML Mapping File (hbm.xml) . Now we can follow the wizard and it will create the mapping files for all beans in the selected package.
The mapping generator is not able to create many-to-many relations, so we have adjusted the one-to-many relation generated.
Here there is the mapping file for the item:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 12, 2010 11:48:52 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="DBmodel.Item" table="ITEM">
<id name="item_id" type="int">
<column name="ITEM_ID" />
<generator class="increment" />
</id>
<property name="type" type="java.lang.String">
<column name="TYPE" />
</property>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="description" type="java.lang.String">
<column name="DESCRIPTION" />
</property>
<property name="pub_date" type="java.lang.String">
<column name="PUB_DATE" />
</property>
<set name="writers" table="ITEM_RESEARCHER" cascade="all" lazy="false">
<key column="ITEM_ID"/>
<many-to-many column="RES_ID" class="DBmodel.Researcher" />
</set>
<property name="deleted" type="boolean">
<column name="DELETED" />
</property>
</class>
</hibernate-mapping>
The mapping file for the researcher does not contain anything for the many-to-many relation, it doesn't need to know about it. So it contains only the natural mappings for its fields, as we can see:
<hibernate-mapping>
<class name="DBmodel.Researcher" table="RESEARCHER">
<id name="res_id" type="int">
<column name="RES_ID" />
<generator class="increment" />
</id>
<property name="fname" type="java.lang.String">
<column name="FNAME" />
</property>
<property name="lname" type="java.lang.String">
<column name="LNAME" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<many-to-one name="entity" class="DBmodel.Entity" cascade="all" lazy="false">
<column name="ENTITY" />
</many-to-one>
<property name="deleted" type="boolean">
<column name="DELETED" />
</property>
</class>
</hibernate-mapping>
3) Let's explain how the many-to-many relation works.
First of all we need a set tag in which we put:
- name: the name of the field in the bean that stores the Set (HashSet).
- table: the name of a new table that contains the foreign keys of the two tables. It's a "middle table" that creates the relations between the two tables.
- cascade="all" and lazy="false": these are mandatory. They allows Hibernate to propagate updates into db tables.
- key: it contains the name of the column in this table (ITEM) that contains the foreign key for the relation.
- many-to-many: it contains the name of the column in the target table (RESEARCHER) and the class mapped into the target table
No comments:
Post a Comment