One to Many Table Mapping

1. Uni-Directional

Category.java
public class Category {
private long id;
private String name;
private Set<Product> products;

public Category() {
}
public Category(String name) {
this.name = name;
}
       //setters...getters...
}

Product.java
public class Product {
private long id;
private String name;
private String description;

public Product() {
}
public Product(String name, String description) {
this.name = name;
this.description = description;
}
       // setters...getter...
}


1.1 XML Mapping

category.hbm.xml
<hibernate-mapping package="com.practice.one2many.mapping.unidirectional.xml.approach"
auto-import="false">
<class name="Category" table="CATEGORY3">
<id name="id" column="CAT_ID">
<generator class="sequence"/>
</id>
<property name="name" />
<set name="products" cascade="all">
<key column="CAT_ID" not-null="true" />
<one-to-many class="Product" />
</set>
</class>
</hibernate-mapping>

product.hbm.xml
<hibernate-mapping
package="com.practice.one2many.mapping.unidirectional.xml.approach"
auto-import="false">
<class name="Product" table="PRODUCT3">
<id name="id" column="P_ID">
<generator class="sequence" />
</id>
<property name="name" />
<property name="description" />
</class>
</hibernate-mapping>

hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
..............................
<mapping resource="com/practice/one2many/mapping/unidirectional/xml/approach/category.hbm.xml"/>
<mapping resource="com/practice/one2many/mapping/unidirectional/xml/approach/product.hbm.xml"/>
...........................
</session-factory>
</hibernate-configuration>


1.2 Annotation Mapping

Category.java
@Entity(name="com.practice.one2many.mapping.unidirectional.annotation.approach.Category")
@Table(name="CATEGORY4")
public class Category {
@Id
@Column(name="CAT_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private long id;
private String name;
@OneToMany(cascade=CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="CAT_ID")
private Set<Product> products;

public Category() {
}

public Category(String name) {
this.name = name;
}
        //setters...getters...
}

Product.java
@Entity(name = "com.practice.one2many.mapping.unidirectional.annotation.approach.Product")
@Table(name = "PRODUCT4")
public class Product {
@Id
@Column(name = "P_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;
private String description;
public Product() {
}
public Product(String name, String description) {
this.name = name;
this.description = description;
}
        //setters...getters...
}

hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
        .........
<mapping class="com.practice.one2many.mapping.unidirectional.annotation.approach.Category"/>
<mapping class="com.practice.one2many.mapping.unidirectional.annotation.approach.Product"/>
</session-factory>
</hibernate-configuration>

Test Code
public class Client1 {
public static void main(String[] args) {
Session session = HibernateUtil.getSession();

Category category = new Category("Electronics");

Product mobile = new Product("Mobile", "This is Nokia Mobile");
Product laptop = new Product("Laptop", "This is Apple Laptop");
Product speaker = new Product("Speaker", "This is Laptop Speaker");

Set<Product> products = new HashSet<>();
products.add(mobile);
products.add(laptop);
products.add(speaker);
category.setProducts(products);

Transaction t = session.beginTransaction();
session.save(category);
// s1.persist(m1);
// s1.persist(m2);
t.commit();
System.out.println("done");
}
}


2. Bi-Directional

Category.java
public class Category {
private long id;
private String name;
private Set<Product> products;
public Category() {
}
public Category(String name) {
this.name = name;
}
        //setters...getters
}

Product.java
public class Product {
private long id;
private String name;
private String description;
private Category category;
public Product() {
}
public Product(String name, String description, Category category) {
this.name = name;
this.description = description;
this.category = category;
}
        //setters...getters
}


1.1 XML Mapping

category.hbm.xml
<hibernate-mapping
package="com.practice.one2many.mapping.bidirectional.xml.approach"
auto-import="false">

<class name="Category" table="CATEGORY1">
<id name="id" column="CAT_ID">
<generator class="sequence" />
</id>
<property name="name" />
<set name="products" inverse="true" cascade="all">
<key column="CAT_ID" not-null="true" />
<one-to-many class="Product" />
</set>
</class>
</hibernate-mapping>

product.hbm.xml
<hibernate-mapping
package="com.practice.one2many.mapping.bidirectional.xml.approach"
auto-import="false">

<class name="Product" table="PRODUCT1">
<id name="id" column="P_ID">
<generator class="sequence" />
</id>
<property name="name" />
<property name="description" />
<many-to-one name="category" class="Category"
column="CAT_ID" not-null="true" />
</class>
</hibernate-mapping>

hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
..................
<mapping resource="com/practice/one2many/mapping/bidirectional/xml/approach/category.hbm.xml"/>
<mapping resource="com/practice/one2many/mapping/bidirectional/xml/approach/product.hbm.xml"/>
</session-factory>
</hibernate-configuration>

1.2 Annotation Mapping

Category.java
@Entity(name="com.practice.one2many.mapping.bidirectional.annotation.approach.Category")
@Table(name="CATEGORY2")
public class Category {
@Id
@Column(name="CAT_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private long id;
private String name;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="category")
private Set<Product> products;
public Category() {
}
public Category(String name) {
this.name = name;
}
        //setters...getters...
}

Product.java
@Entity(name="com.practice.one2many.mapping.bidirectional.annotation.approach.Product")
@Table(name="PRODUCT2")
public class Product {
@Id
@Column(name="P_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private long id;
private String name;
private String description;
@ManyToOne
@JoinColumn(name="CAT_ID")
private Category category;

public Product() {
}

public Product(String name, String description, Category category) {
this.name = name;
this.description = description;
this.category = category;
}
        //setters...getters...
}

hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
..............................
<mapping class="com.practice.one2many.mapping.bidirectional.annotation.approach.Category"/>
<mapping class="com.practice.one2many.mapping.bidirectional.annotation.approach.Product"/>
</session-factory>
</hibernate-configuration>

Test Code
public class Client1 {
public static void main(String[] args) {
Session session = HibernateUtil.getSession();

Category category = new Category("Electronics");

Product mobile = new Product("Mobile", "This is Nokia Mobile", category);
Product laptop = new Product("Laptop", "This is Apple Laptop", category);
Product speaker = new Product("Speaker", "This is Laptop Speaker", category);

Set<Product> products = new HashSet<>();
products.add(mobile);
products.add(laptop);
products.add(speaker);
category.setProducts(products);

Transaction t = session.beginTransaction();
session.save(category);
// s1.persist(m1);
// s1.persist(m2);
t.commit();
System.out.println("done");
}
}

Comments

Popular posts from this blog

SQL basic interview question

gsutil Vs Storage Transfer Service Vs Transfer Appliance