Hibernate - one to one xml declaration:
AbcParentEntity.hbm.xml
====================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcParentEntity" table="PARENT_TABLE">
<id name="parentId" type="short">
<column name="PARENT_COL_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="PARENT_COL_NAME" length="200" />
</property>
<one-to-one name="childObject" class="com.xyz.entity.AbcChildEntity" cascade="all" />
</class>
</hibernate-mapping>
AbcChildEntity.hbm.xml
===================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcChildEntity" table="CHILD_TABLE_1">
<id name="childId" type="short">
<column name="CHILD_TAB_COL_ABC_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="childName" type="string">
<column name="CHILD_TAB_COL_ABC_NAME" length="200" />
</property>
</class>
</hibernate-mapping>
AbcParentEntity.hbm.xml
==========================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcParentEntity" table="PARENT_TABLE">
<id name="parentId" type="short">
<column name="PARENT_COL_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="PARENT_COL_NAME" length="200" />
</property>
<set name="childObjects1" table="CHILD_TABLE_1" inverse="true" lazy="true" fetch="select">
<key>
<column name="CHILD_TAB_COL_ABC_ID" precision="22" scale="0" />
</key>
<one-to-many class="com.xyz.entity.AbcChildEntity" />
</set>
<set name="childObjects2" table="CHILD_TABLE_2" inverse="true" lazy="true" fetch="select">
<key>
<column name="CHILD_TAB_COL_XYZ_ID" precision="4" scale="0" />
</key>
<one-to-many class="com.xyz.entity.XyzChildEntity" />
</set>
</class>
</hibernate-mapping>
AbcChildEntity.hbm.xml
================================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcChildEntity" table="CHILD_TABLE_1">
<id name="childId" type="short">
<column name="CHILD_TAB_COL_ABC_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="childName" type="string">
<column name="CHILD_TAB_COL_ABC_NAME" length="200" />
</property>
</class>
</hibernate-mapping>
XyzChildEntity.hbm.xml
==============================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.XyzChildEntity" table="CHILD_TABLE_2">
<id name="xyzId" type="big_decimal">
<column name="CHILD_TAB_COL_XYZ_ID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="xyzName" type="string">
<column name="CHILD_TAB_COL_XYZ_NAME" length="200" />
</property>
</class>
</hibernate-mapping>
AbcParentEntity.java
=================================================
package com.xyz.entity;
import java.util.Set;
import java.io.Serializable;
public class AbcParentEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Short parentId;
private String name;
private Set<XyzChildEntity> childObjects2;
private Set<AbcChildEntity> childObjects1;
public AbcParentEntity() {
}
public Short getParentId() {
return this.parentId;
}
public void setParentId(Short parentId) {
this.parentId = parentId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<AbcChildEntity> getChildObjects1() {
return this.childObjects1;
}
public void setChildObjects1(Set<AbcChildEntity> childObjects1) {
this.childObjects1 = childObjects1;
}
public Set<XyzChildEntity> getChildObjects2() {
return this.childObjects2;
}
public void setChildObjects2(Set<XyzChildEntity> childObjects2) {
this.childObjects2 = childObjects2;
}
}
AbcChildEntity.java
===============================================
package com.xyz.entity;
import java.io.Serializable;
public class AbcChildEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Short childId;
private String subVerticalName;
public AbcChildEntity() {
}
public Short getChildId() {
return this.childId;
}
public void setChildId(Short childId) {
this.childId = childId;
}
public String getChildName() {
return this.childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
}
XyzChildEntity.java
==============================================
package com.xyz.entity;
import java.math.BigDecimal;
import java.util.Set;
import java.io.Serializable;
public class XyzChildEntity implements Serializable {
private static final long serialVersionUID = 1L;
private BigDecimal xyzId;
private String customerName;
public XyzChildEntity() {
}
public BigDecimal getXyzId() {
return this.xyzId;
}
public void setXyzId(BigDecimal xyzId) {
this.customerId = xyzId;
}
public String getCustomerName() {
return this.customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
Hibernate - many to one xml declaration:
AbcParentEntity.hbm.xml :
=====================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcParentEntity" table="PARENT_TABLE">
<id name="parentId" type="short">
<column name="PARENT_COL_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="PARENT_COL_NAME" length="200" />
</property>
</class>
</hibernate-mapping>
AbcChildEntity.hbm.xml :
====================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcChildEntity" table="CHILD_TABLE_1">
<id name="childId" type="short">
<column name="CHILD_TAB_COL_ABC_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="childName" type="string">
<column name="CHILD_TAB_COL_ABC_NAME" length="200" />
</property>
<many-to-one name="parentObject" class="com.xyz.entity.AbcParentEntity" cascade="all" />
</class>
</hibernate-mapping>
Hibernate Many to Many xml declaration:
Tables:
Table_name Column_Names
Company company_id, company_name
Item item_id, item_name
Com_Item company_id, item_id
AbcCompany.hbm.xml :
===================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.abc.AbcCompany" table="Company">
<id name="companyId" type="java.lang.Integer">
<column name="company_id" />
<generator class="identity" />
</id>
<property name="companyName" type="string">
<column name="company_name" length="10" not-null="true" unique="true" />
</property>
<set name="items" table="Com_Item"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key>
<column name="company_id" not-null="true" />
</key>
<many-to-many entity-name="com.abc.AbcItem">
<column name="item_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
AbcItem.hbm.xml :
===============
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.abc.AbcItem" table="Item">
<id name="itemId" type="java.lang.Integer">
<column name="item_id" />
<generator class="identity" />
</id>
<property name="itemName" type="string">
<column name="item_name" length="10" not-null="true" unique="true" />
</property>
<set name="companies" table="Com_Item"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key>
<column name="item_id" not-null="true" />
</key>
<many-to-many entity-name="com.abc.AbcCompany">
<column name="company_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
Table Per Class Hierarchy
table_name column_names
Card cid, amt,type, ctype, dctype
Card.hbm.xml
==============
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.Card" table="Card">
<id name="cardId" column="cid" />
<discriminator column="dcolumn" type="string" length="5"/>
<property name="" column="amt" />
<subclass name="com.xyz.CreditCard" discriminator-value="CC">
<property name="creditCardType" column="cctype" length="10" />
</subclass>
<subclass name="com.xyz.DebitCard" discriminator-value="DC">
<property name="debitCardType" column="dctype" length="10" />
</subclass>
</class>
</hibernate-mapping>
Table Per Concrete class
table_name column_names
Card cid, amt
Credit_Card cid, amt, ctype
Debit_Card cid, amt, dctype
<?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="com.xyz.Card" table="Card">
<id name="cardId" column="cid" />
<property name="" column="amt" />
<union-subclass name="com.xyz.CreditCard" table="Credit_Card">
<property name="creditCardType" column="cctype" length="10" />
</union-subclass>
<union-subclass name="com.xyz.DebitCard" table="Debit_Card">
<property name="debitCardType" column="dctype" length="10" />
</union-subclass>
</class>
</hibernate-mapping>
Table Per Sub class
table_name column_names
Card cid, amt
Credit_Card ccid, ctype
Debit_Card cdid, dctype
<?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="com.xyz.Card" table="Card">
<id name="cardId" column="cid" />
<property name="" column="amt" />
<joined-subclass name="com.xyz.CreditCard" table="Credit_Card">
<key column="ccId"></key>
<property name="creditCardType" column="cctype" length="10" />
</joined-subclass>
<joined-subclass name="com.xyz.DebitCard" table="Debit_Card">
<key column="dcId"></key>
<property name="debitCardType" column="dctype" length="10" />
</joined-subclass>
</class>
</hibernate-mapping>
AbcParentEntity.hbm.xml
====================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcParentEntity" table="PARENT_TABLE">
<id name="parentId" type="short">
<column name="PARENT_COL_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="PARENT_COL_NAME" length="200" />
</property>
<one-to-one name="childObject" class="com.xyz.entity.AbcChildEntity" cascade="all" />
</class>
</hibernate-mapping>
AbcChildEntity.hbm.xml
===================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcChildEntity" table="CHILD_TABLE_1">
<id name="childId" type="short">
<column name="CHILD_TAB_COL_ABC_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="childName" type="string">
<column name="CHILD_TAB_COL_ABC_NAME" length="200" />
</property>
</class>
</hibernate-mapping>
One-to-many association in hibernate demonstration
AbcParentEntity.hbm.xml
==========================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcParentEntity" table="PARENT_TABLE">
<id name="parentId" type="short">
<column name="PARENT_COL_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="PARENT_COL_NAME" length="200" />
</property>
<set name="childObjects1" table="CHILD_TABLE_1" inverse="true" lazy="true" fetch="select">
<key>
<column name="CHILD_TAB_COL_ABC_ID" precision="22" scale="0" />
</key>
<one-to-many class="com.xyz.entity.AbcChildEntity" />
</set>
<set name="childObjects2" table="CHILD_TABLE_2" inverse="true" lazy="true" fetch="select">
<key>
<column name="CHILD_TAB_COL_XYZ_ID" precision="4" scale="0" />
</key>
<one-to-many class="com.xyz.entity.XyzChildEntity" />
</set>
</class>
</hibernate-mapping>
AbcChildEntity.hbm.xml
================================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcChildEntity" table="CHILD_TABLE_1">
<id name="childId" type="short">
<column name="CHILD_TAB_COL_ABC_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="childName" type="string">
<column name="CHILD_TAB_COL_ABC_NAME" length="200" />
</property>
</class>
</hibernate-mapping>
XyzChildEntity.hbm.xml
==============================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.XyzChildEntity" table="CHILD_TABLE_2">
<id name="xyzId" type="big_decimal">
<column name="CHILD_TAB_COL_XYZ_ID" precision="22" scale="0" />
<generator class="assigned" />
</id>
<property name="xyzName" type="string">
<column name="CHILD_TAB_COL_XYZ_NAME" length="200" />
</property>
</class>
</hibernate-mapping>
AbcParentEntity.java
=================================================
package com.xyz.entity;
import java.util.Set;
import java.io.Serializable;
public class AbcParentEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Short parentId;
private String name;
private Set<XyzChildEntity> childObjects2;
private Set<AbcChildEntity> childObjects1;
public AbcParentEntity() {
}
public Short getParentId() {
return this.parentId;
}
public void setParentId(Short parentId) {
this.parentId = parentId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set<AbcChildEntity> getChildObjects1() {
return this.childObjects1;
}
public void setChildObjects1(Set<AbcChildEntity> childObjects1) {
this.childObjects1 = childObjects1;
}
public Set<XyzChildEntity> getChildObjects2() {
return this.childObjects2;
}
public void setChildObjects2(Set<XyzChildEntity> childObjects2) {
this.childObjects2 = childObjects2;
}
}
AbcChildEntity.java
===============================================
package com.xyz.entity;
import java.io.Serializable;
public class AbcChildEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Short childId;
private String subVerticalName;
public AbcChildEntity() {
}
public Short getChildId() {
return this.childId;
}
public void setChildId(Short childId) {
this.childId = childId;
}
public String getChildName() {
return this.childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
}
XyzChildEntity.java
==============================================
package com.xyz.entity;
import java.math.BigDecimal;
import java.util.Set;
import java.io.Serializable;
public class XyzChildEntity implements Serializable {
private static final long serialVersionUID = 1L;
private BigDecimal xyzId;
private String customerName;
public XyzChildEntity() {
}
public BigDecimal getXyzId() {
return this.xyzId;
}
public void setXyzId(BigDecimal xyzId) {
this.customerId = xyzId;
}
public String getCustomerName() {
return this.customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
Hibernate - many to one xml declaration:
AbcParentEntity.hbm.xml :
=====================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcParentEntity" table="PARENT_TABLE">
<id name="parentId" type="short">
<column name="PARENT_COL_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="PARENT_COL_NAME" length="200" />
</property>
</class>
</hibernate-mapping>
AbcChildEntity.hbm.xml :
====================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcChildEntity" table="CHILD_TABLE_1">
<id name="childId" type="short">
<column name="CHILD_TAB_COL_ABC_ID" precision="4" scale="0" />
<generator class="assigned" />
</id>
<property name="childName" type="string">
<column name="CHILD_TAB_COL_ABC_NAME" length="200" />
</property>
<many-to-one name="parentObject" class="com.xyz.entity.AbcParentEntity" cascade="all" />
</class>
</hibernate-mapping>
Hibernate Many to Many xml declaration:
Tables:
Table_name Column_Names
Company company_id, company_name
Item item_id, item_name
Com_Item company_id, item_id
AbcCompany.hbm.xml :
===================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.abc.AbcCompany" table="Company">
<id name="companyId" type="java.lang.Integer">
<column name="company_id" />
<generator class="identity" />
</id>
<property name="companyName" type="string">
<column name="company_name" length="10" not-null="true" unique="true" />
</property>
<set name="items" table="Com_Item"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key>
<column name="company_id" not-null="true" />
</key>
<many-to-many entity-name="com.abc.AbcItem">
<column name="item_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
AbcItem.hbm.xml :
===============
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.abc.AbcItem" table="Item">
<id name="itemId" type="java.lang.Integer">
<column name="item_id" />
<generator class="identity" />
</id>
<property name="itemName" type="string">
<column name="item_name" length="10" not-null="true" unique="true" />
</property>
<set name="companies" table="Com_Item"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key>
<column name="item_id" not-null="true" />
</key>
<many-to-many entity-name="com.abc.AbcCompany">
<column name="company_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
Table Per Class Hierarchy
table_name column_names
Card cid, amt,type, ctype, dctype
Card.hbm.xml
==============
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.Card" table="Card">
<id name="cardId" column="cid" />
<discriminator column="dcolumn" type="string" length="5"/>
<property name="" column="amt" />
<subclass name="com.xyz.CreditCard" discriminator-value="CC">
<property name="creditCardType" column="cctype" length="10" />
</subclass>
<subclass name="com.xyz.DebitCard" discriminator-value="DC">
<property name="debitCardType" column="dctype" length="10" />
</subclass>
</class>
</hibernate-mapping>
Table Per Concrete class
table_name column_names
Card cid, amt
Credit_Card cid, amt, ctype
Debit_Card cid, amt, dctype
<?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="com.xyz.Card" table="Card">
<id name="cardId" column="cid" />
<property name="" column="amt" />
<union-subclass name="com.xyz.CreditCard" table="Credit_Card">
<property name="creditCardType" column="cctype" length="10" />
</union-subclass>
<union-subclass name="com.xyz.DebitCard" table="Debit_Card">
<property name="debitCardType" column="dctype" length="10" />
</union-subclass>
</class>
</hibernate-mapping>
Table Per Sub class
table_name column_names
Card cid, amt
Credit_Card ccid, ctype
Debit_Card cdid, dctype
<?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="com.xyz.Card" table="Card">
<id name="cardId" column="cid" />
<property name="" column="amt" />
<joined-subclass name="com.xyz.CreditCard" table="Credit_Card">
<key column="ccId"></key>
<property name="creditCardType" column="cctype" length="10" />
</joined-subclass>
<joined-subclass name="com.xyz.DebitCard" table="Debit_Card">
<key column="dcId"></key>
<property name="debitCardType" column="dctype" length="10" />
</joined-subclass>
</class>
</hibernate-mapping>
No comments:
Post a Comment