Inheritance Mapping

Inheritance Mapping:

Logging by Log4j using xml file:

Logging empowers the software engineer to compose the log points of interest into a document for all time. Log4j and Logback structures can be utilized as a part of sleep system to help logging.

  • By log4j.xml file (or)
  • By log4j.properties file

log4j.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>

<log4j:configuration xmlns:log4j=”http://jakarta.apache.org/log4j/”

debug=”false”>

<appender name=”CONSOLE” class=”org.apache.log4j.ConsoleAppender”>

<layout class=”org.apache.log4j.PatternLayout”>

<param name=”ConversionPattern” value=”[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n” />

</layout>

</appender>

<appender name=”ASYNC” class=”org.apache.log4j.AsyncAppender”>

<appender-ref ref=”CONSOLE” />

<appender-ref ref=”FILE” />

</appender>

<appender name=”FILE” class=”org.apache.log4j.RollingFileAppender”>

<param name=”File” value=“C:/javaspotlog.log” />

<param name=”MaxBackupIndex” value=”100″ />

<layout class=”org.apache.log4j.PatternLayout”>

<param name=”ConversionPattern” value=”[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n” />

</layout>

</appender>

<category name=”org.hibernate”>

<priority value=”DEBUG” />

</category>

<category name=”java.sql”>

<priority value=”debug” />

</category>

<root>

<priority value=”INFO” />

<appender-ref ref=”FILE” />

</root>

</log4j:configuration>

Logging by Log4j using properties file:

Log4j and Logback frameworks are used to support logging in hibernate, there are two ways to perform logging using log4j.

  1. By log4j.xml file
  2. By log4j.properties file

log4j.properties

# Direct log messages to a log file

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.File=C:\\javatpointhibernate.log

log4j.appender.file.MaxFileSize=1MB

log4j.appender.file.MaxBackupIndex=1

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n

log4j.properties

# Direct log messages to stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target=System.out

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n

# Root logger option

log4j.rootLogger=INFO, file, stdout

# Log everything. Good for troubleshooting

log4j.logger.org.hibernate=INFO

# Log all JDBC parameters

log4j.logger.org.hibernate.type=ALL

Inheritance Mapping 

Table Per Hierarchy

  • Table Per Concrete class
  • Table Per Subclass

Hibernate Table Per Hierarchy using xml file:

We can delineate legacy order classes with the table of the database. There are three legacy mapping techniques characterized in the rest.

 

Hierarchy using Annotation

Hierarchy using Annotation

In the past page, we have mapped the legacy progressive system with one table just utilizing xml document. Here, we will play out this undertaking utilizing explanation.

You have to utilize

@Inheritance(strategy=InheritanceType.SINGLE_TABLE), @DiscriminatorColumn and @DiscriminatorValue explanations for mapping table per progression technique.

If there should arise an occurrence of table for each pecking order, just a single table is required to delineate legacy chain of command. Here, an additional segment (otherwise called discriminator segment) is made in the table to recognize the class.

 Hibernate Table Per Hierarchy using Annotation

  • Create the persistent classes
  • Create the configuration file
  • Create the class to store the fetch the data

1) Create the Persistent classes

 File: Employee.java

package com.javaspot.mypackage;

import javax.persistence.*;

@Entity

@Table(name = “employee101″)

@Inheritance(strategy=InheritanceType.SINGLE_TABLE)

@DiscriminatorColumn(name=”type”,discriminatorType=DiscriminatorType.STRING)

@DiscriminatorValue(value=”employee”)

public class Employee {

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name = “id”)

private int id;

@Column(name = “name”)

private String name;

//setters and getters

}

File: Regular_Employee.java

package com.javaspot.mypackage;

import javax.persistence.*;

@Entity

@DiscriminatorValue(“regularemployee”)

public class Regular_Employee extends Employee{

@Column(name=”salary”)

private float salary;

@Column(name=”bonus”)

private int bonus;

//setters and getters

}

 File: Contract_Employee.java

package com.javaspot.mypackage;

import javax.persistence.Column;

import javax.persistence.DiscriminatorValue;

import javax.persistence.Entity;

@Entity

@DiscriminatorValue(“contractemployee”)

public class Contract_Employee extends Employee{

@Column(name=”pay_per_hour”)

private float pay_per_hour;

@Column(name=”contract_duration”)

private String contract_duration;

//setters and getters

}

2) Add the persistent classes in configuration file

<mapping class=”com.javaspot.mypackage.Employee”/>

<mapping class=”com.javaspot.mypackage.Contract_Employee”/>

<mapping class=”com.javaspot.mypackage.Regular_Employee”/>

</pre></div>

<table >

<tr><td>Now the configuration file will look like this:

</td></tr>

</table>

<span id=”filename”>File: hibernate.cfg.xml</span>

<div class=”codeblock”><pre name=”code” class=”java” >

<?xml version=’1.0′ encoding=’UTF-8′?>

<!DOCTYPE hibernate-configuration PUBLIC

“-//Hibernate/Hibernate Configuration DTD 3.0//EN”

“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>

<!– Generated by MyEclipse Hibernate Tools.                   –>

<hibernate-configuration>

<session-factory>

<property name=”hbm2ddl.auto”>update</property>

<property name=”dialect”>org.hibernate.dialect.Oracle9Dialect</property>

<property name=”connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>

<property name=”connection.username”>system</property>

<property name=”connection.password”>oracle</property>

<property name=”connection.driver_class”>oracle.jdbc.driver.OracleDriver</propert>

<mapping class=”com.javaspot.mypackage.Employee”/>

<mapping class=”com.javaspot.mypackage.Contract_Employee”/>

<mapping class=”com.javaspot.mypackage.Regular_Employee”/>

</session-factory>

</hibernate-configuration>

3) Create the class that stores the persistent object

 File: StoreTest.java

package com.javaspot.mypackage;

import org.hibernate.*;

import org.hibernate.cfg.*;

public class StoreData {

public static void main(String[] args) {

AnnotationConfiguration cfg=new AnnotationConfiguration();

Session session=cfg.configure(“hibernate.cfg.xml”).buildSessionFactory().openSession();      Transaction t=session.beginTransaction();

Employee e1=new Employee();

e1.setName(“sonoo”);

Regular_Employee e2=new Regular_Employee();

e2.setName(“Vivek Kumar”);

e2.setSalary(50000);

e2.setBonus(5);

Contract_Employee e3=new Contract_Employee();

e3.setName(“Arjun Kumar”);

e3.setPay_per_hour(1000);

e3.setContract_duration(“15 hours”);

session.persist(e1);

session.persist(e2);

session.persist(e3);

t.commit();

session.close();

System.out.println(“success”);

}

}

 Table Per Concrete class using Annotation:

If there should be an occurrence of Table Per Concrete class, tables are made per class. So there are no nullable esteems in the table. Weakness of this approach is that copy sections are made in the subclass tables.

Here, we have to utilize @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) comment in the parent class and @AttributeOverrides explanation in the subclasses.

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) determines that we are utilizing table per solid class methodology. It ought to be determined in the parent class as it were.

@AttributeOverrides characterizes that parent class characteristics will be overriden in this class. In table structure, parent class table sections will be included the subclass table.

Table Per Subclass;

If there should arise an occurrence of Table Per Subclass, subclass mapped tables are identified with parent class mapped table by essential key and remote key relationship.

The <joined-subclass> component of class is utilized to delineate youngster class with parent utilizing the essential key and outside key connection.

In this case, we will utilize hb2ddl.auto property to create the table consequently. So we don’t should be stressed over making tables in the database.

Table Per Subclass using Annotation:

As we have determined before, if there should arise an occurrence of table for every subclass procedure, tables are made according to diligent classes however they are reated utilizing essential and remote key. So there won’t be copy sections in the connection.

We have to indicate @Inheritance(strategy=InheritanceType.JOINED) in the parent class and @PrimaryKeyJoinColumn comment in the subclasses.

 

Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


100% Secure Payments. All major credit & debit cards accepted.