Double Bonanza Offer - Upto 30% Off + 1 Self Paced Course Free | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
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:
class="org.apache.log4j.AsyncAppender">class="org.apache.log4j.RollingFileAppender">"C:/javaspotlog.log" />class="org.apache.log4j.PatternLayout">

JAVA Tutorial Video

[embed]https://www.youtube.com/watch?v=Mb8nG-NH6gg&t=2835s[/embed]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 filelog4j.appender.file=org.apache.log4j.RollingFileAppenderlog4j.appender.file.File=C:\\javatpointhibernate.loglog4j.appender.file.MaxFileSize=1MBlog4j.appender.file.MaxBackupIndex=1log4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%nlog4j.properties# Direct log messages to stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n# Root logger optionlog4j.rootLogger=INFO, file, stdout# Log everything. Good for troubleshootinglog4j.logger.org.hibernate=INFO# Log all JDBC parameterslog4j.logger.org.hibernate.type=ALLInheritance 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 AnnotationHierarchy 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.javapackage 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.javapackage 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.javapackage 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 fileclass="com.javaspot.mypackage.Employee"/>class="com.javaspot.mypackage.Contract_Employee"/>class="com.javaspot.mypackage.Regular_Employee"/>
Now the configuration file will look like this:File: hibernate.cfg.xmlclass="codeblock">class="java" >updateorg.hibernate.dialect.Oracle9Dialectjdbc:oracle:thin:@localhost:1521:xesystemoracleoracle.jdbc.driver.OracleDriverclass="com.javaspot.mypackage.Employee"/>class="com.javaspot.mypackage.Contract_Employee"/>class="com.javaspot.mypackage.Regular_Employee"/>3) Create the class that stores the persistent object File: StoreTest.javapackage 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 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.