Map a many-to-many relationship between 2 entities with a link table in the database schema.
1) Create the Category and Item entity persistent classes.
Category.java
Item.java
2) Create the Mapping files,
Category.hbm.xml
Item.hbm.xml
3) In the hibernate.cfg.xml file, add the following configuration,
4) Create the client code in a servlet, as follows,
5) Configure the servlet in a web.xml file,
6) Enter the following URL in the browser,
7) Check the database for the created tables and the data in those tables
1) Create the Category and Item entity persistent classes.
Category.java
package info.icontraining.hibernate;
import java.util.*;
public class Category {
private Long id;
private String categoryName;
private Set<Item> items = new HashSet<Item>() ;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Set<Item> getItems() {
return items;
}
public void setItems(Set<Item> items) {
this.items = items;
}
}
Item.java
package info.icontraining.hibernate;
import java.util.*;
public class Item {
private Long id;
private String itemName;
private Set<Category> categories = new HashSet<Category>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Set<Category> getCategories() {
return categories;
}
public void setCategories(Set<Category> categories) {
this.categories = categories;
}
}
2) Create the Mapping files,
Category.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="info.icontraining.hibernate.Category" table="CATEGORY_TABLE">
<id name="id" column="CATEGORY_ID">
<generator class="native" />
</id>
<property name="categoryName" column="CATEGORY_NAME" type="string" />
<set name="items" table="CATEGORY_ITEM" cascade="save-update">
<key column="CATEGORY_ID" />
<many-to-many class="info.icontraining.hibernate.Item" column="ITEM_ID" />
</set>
</class>
</hibernate-mapping>
Item.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="info.icontraining.hibernate.Item" table="ITEM_TABLE">
<id name="id" column="ITEM_ID">
<generator class="native" />
</id>
<property name="itemName" column="ITEM_NAME" type="string" />
<set name="categories" table="CATEGORY_ITEM" cascade="save-update" inverse="true">
<key column="ITEM_ID" />
<many-to-many class="info.icontraining.hibernate.Category" column="CATEGORY_ID" />
</set>
</class>
</hibernate-mapping>
3) In the hibernate.cfg.xml file, add the following configuration,
<mapping resource="info/icontraining/hibernate/Item.hbm.xml" />
<mapping resource="info/icontraining/hibernate/Category.hbm.xml" />
4) Create the client code in a servlet, as follows,
package info.icontraining.servlets;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.* ;
import org.hibernate.*;
import info.icontraining.hibernate.*;
public class CategoryItem extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
PrintWriter out = res.getWriter();
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Category category1 = new Category();
category1.setCategoryName("Category-1");
Category category2 = new Category();
category2.setCategoryName("Category-2");
Item item1 = new Item();
item1.setItemName("Item-1");
Item item2 = new Item();
item2.setItemName("Item-2");
category1.getItems().add(item1);
category1.getItems().add(item2);
category2.getItems().add(item1);
category2.getItems().add(item2);
item1.getCategories().add(category1);
item1.getCategories().add(category2);
item2.getCategories().add(category1);
item2.getCategories().add(category2);
session.save(category1);
session.save(category2);
tx.commit();
session.close();
Session newSession = HibernateUtil.getSessionFactory().openSession();
Transaction newTx = newSession.beginTransaction();
List<Category> categories = newSession.createQuery("from Category").list();
out.println( categories.size() + " categories found:" );
for ( Iterator<Category> iter = categories.iterator(); iter.hasNext(); ) {
Category cat = iter.next();
out.println( "Name of Category: " + cat.getCategoryName());
out.println( "Number of Items in Category: " + cat.getItems().size());
}
newTx.commit();
newSession.close();
}
}
5) Configure the servlet in a web.xml file,
<servlet>
<servlet-name>CategoryItem</servlet-name>
<servlet-class>info.icontraining.servlets.CategoryItem</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CategoryItem</servlet-name>
<url-pattern>/categoryItem</url-pattern>
</servlet-mapping>
6) Enter the following URL in the browser,
http://localhost:8080/WebAppName/categoryItem
7) Check the database for the created tables and the data in those tables
No comments:
Post a Comment