Saturday, 19 November 2016

Attribute Override in Hibernate

Model Class

/**
 *
 */
package com.learnjavabyanand;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author ananddw
 *
 */
@Entity
@Table(name="student_details")
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int studentrollnumber;

@Column(name="student_name")
private String studentName;


@AttributeOverrides({
@AttributeOverride(name="company_location",column=@Column(name="company1_location")),
@AttributeOverride(name="company_name",column=@Column(name="company1_name"))})
private CompanyDetails comany_detalils;



public CompanyDetails getComany_detalils2() {
return comany_detalils2;
}

public void setComany_detalils2(CompanyDetails comany_detalils2) {
this.comany_detalils2 = comany_detalils2;
}

@Embedded
private CompanyDetails comany_detalils2;


public CompanyDetails getComany_detalils() {
return comany_detalils;
}

public void setComany_detalils(CompanyDetails comany_detalils) {
this.comany_detalils = comany_detalils;
}

public int getStudentrollnumber() {
return studentrollnumber;
}

public void setStudentrollnumber(int studentrollnumber) {
this.studentrollnumber = studentrollnumber;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}




}



Model Class 2 : 


package com.learnjavabyanand;

import javax.persistence.Embeddable;

@Embeddable
public class CompanyDetails {
private String company_name;
private String company_location;
public String getCompany_name() {
return company_name;
}
public void setCompany_name(String company_name) {
this.company_name = company_name;
}
public String getCompany_location() {
return company_location;
}
public void setCompany_location(String company_location) {
this.company_location = company_location;
}

}


Hibernate Client : 


package com.learnjavabyanand;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
 * @author ananddw
 *
 */
public class StudentClient {

public static void main(String[] args) {
// TODO Auto-generated method stub

Student student= new Student();
student.setStudentName("Anand Dwivedi");
CompanyDetails comp= new CompanyDetails();
comp.setCompany_name("Rediff");
comp.setCompany_location("Bangalore");
CompanyDetails comp2= new CompanyDetails();
comp2.setCompany_name("Google");
comp2.setCompany_location("Hyderabad");
student.setComany_detalils(comp);
student.setComany_detalils2(comp2);
try{
SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory();
Session session=factory.openSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
}

}


hibernate Configuration 


<?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">

<hibernate-configuration>
  <session-factory>
  <!--  Postgres DB Details  -->
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hibernate</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">password</property>
      <!--  Postgres DB Details End  -->
    
    <!-- Postgres Dialect has information of list of  queries avil in Postgres DB -->
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <!-- Postgres Dialect End -->
    
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
    <!--  Echo show Query  -->
    <property name="show_sql">true</property>
     <!--  Echo show Query End  -->
    
    <property name="format_sql">true</property>
    
    <!--  Create every time table and insert Data -->
    <property name="hibernate.hbm2ddl.auto">create</property>
    <!--  Create every time table and insert Data End -->
    
    <!--  Define Package for mapping class -->
    
    <mapping class="com.learnjavabyanand.Student"/>
    <!--  Define Package for mapping class End -->
   
  </session-factory>
</hibernate-configuration>



Snapshot : 




PS: Add all the required jars

Class Embedded In Hibernate

Model Class


/**
 *
 */
package com.learnjavabyanand;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author ananddw
 *
 */
@Entity
@Table(name="student_details")
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int studentrollnumber;

@Column(name="student_name")
private String studentName;


@Embedded
private CompanyDetails comany_detalils;


public CompanyDetails getComany_detalils() {
return comany_detalils;
}

public void setComany_detalils(CompanyDetails comany_detalils) {
this.comany_detalils = comany_detalils;
}

public int getStudentrollnumber() {
return studentrollnumber;
}

public void setStudentrollnumber(int studentrollnumber) {
this.studentrollnumber = studentrollnumber;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}




}


Model Class 2 

package com.learnjavabyanand;

import javax.persistence.Embeddable;

@Embeddable
public class CompanyDetails {
private String company_name;
private String company_location;
public String getCompany_name() {
return company_name;
}
public void setCompany_name(String company_name) {
this.company_name = company_name;
}
public String getCompany_location() {
return company_location;
}
public void setCompany_location(String company_location) {
this.company_location = company_location;
}

}


Client Programe : 


package com.learnjavabyanand;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
 * @author ananddw
 *
 */
public class StudentClient {

public static void main(String[] args) {
// TODO Auto-generated method stub

Student student= new Student();
student.setStudentName("Anand Dwivedi");
CompanyDetails comp= new CompanyDetails();
comp.setCompany_name("Rediff");
comp.setCompany_location("Bangalore");
student.setComany_detalils(comp);
try{
SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory();
Session session=factory.openSession();
session.beginTransaction();
session.save(student);
session.getTransaction().commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
}

}


hibernate Configuration 

<?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">

<hibernate-configuration>
  <session-factory>
  <!--  Postgres DB Details  -->
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hibernate</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">password</property>
      <!--  Postgres DB Details End  -->
    
    <!-- Postgres Dialect has information of list of  queries avil in Postgres DB -->
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <!-- Postgres Dialect End -->
    
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
    <!--  Echo show Query  -->
    <property name="show_sql">true</property>
     <!--  Echo show Query End  -->
    
    <property name="format_sql">true</property>
    
    <!--  Create every time table and insert Data -->
    <property name="hibernate.hbm2ddl.auto">create</property>
    <!--  Create every time table and insert Data End -->
    
    <!--  Define Package for mapping class -->
    
    <mapping class="com.learnjavabyanand.Student"/>
    <!--  Define Package for mapping class End -->
   
  </session-factory>
</hibernate-configuration>


Snapshot : 



PS: Add all the required jars 

Auto Increment Column Name in Hibernate

Model Class


/**
 *
 */
package com.learnjavabyanand;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author ananddw
 *
 */
@Entity
@Table(name="student_details")
public class Student {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int studentrollnumber;

@Column(name="student_name")
private String studentName;

public int getStudentrollnumber() {
return studentrollnumber;
}

public void setStudentrollnumber(int studentrollnumber) {
this.studentrollnumber = studentrollnumber;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}




}


Client Class: 

package com.learnjavabyanand;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
 * @author ananddw
 *
 */
public class StudentClient {

public static void main(String[] args) {
// TODO Auto-generated method stub

Student student= new Student();
student.setStudentName("Anand Dwivedi");
Student student2= new Student();
student2.setStudentName("Ajeet Singh");
Student student3= new Student();
student3.setStudentName("Antrish Mishra");
try{
SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory();
Session session=factory.openSession();
session.beginTransaction();
session.save(student);
session.save(student2);
session.save(student3);
session.getTransaction().commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
}

}


hibernate Configuration file 


<?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">

<hibernate-configuration>
  <session-factory>
  <!--  Postgres DB Details  -->
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hibernate</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">password</property>
      <!--  Postgres DB Details End  -->
    
    <!-- Postgres Dialect has information of list of  queries avil in Postgres DB -->
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <!-- Postgres Dialect End -->
    
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
    <!--  Echo show Query  -->
    <property name="show_sql">true</property>
     <!--  Echo show Query End  -->
    
    <property name="format_sql">true</property>
    
    <!--  Create every time table and insert Data -->
    <property name="hibernate.hbm2ddl.auto">create</property>
    <!--  Create every time table and insert Data End -->
    
    <!--  Define Package for mapping class -->
    
    <mapping class="com.learnjavabyanand.Student"/>
    <!--  Define Package for mapping class End -->
   
  </session-factory>
</hibernate-configuration>




Snapshot : 



PS: Add all the required Jars 

Retrive Obect In Hibernate

Model Class

package com.learnjavabyanand;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * @author ananddw
 *
 */
//@Entity will create table as per class name By if you specify name then table name must be the name what you will provide inside Entity
@Entity(name="employee_Details")
public class EmployeeDetails {

//we can put this annotation above getter as well
@Id
@Column(name="employee_name")
private String employeeName;

//ignore this variable and not create column for us
private String employeeNumber;

@Column(name="employee_location")
private String employeeLocation ;

//Used because to store Date not full time stamp
@Temporal(TemporalType.DATE)
private Date todayDate;

public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(String employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getEmployeeLocation() {
return employeeLocation;
}
public void setEmployeeLocation(String employeeLocation) {
this.employeeLocation = employeeLocation;
}
public Date getTodayDate() {
return todayDate;
}
public void setTodayDate(Date todayDate) {
this.todayDate = todayDate;
}



}


Client Class

package com.learnjavabyanand;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 * 
 * @author ananddw
 *
 */
public class HibernateClient {

public static void main(String[] args) {
// TODO Auto-generated method stub
EmployeeDetails employee= new EmployeeDetails();
employee.setEmployeeName("Anand Dwivedi");
employee.setEmployeeNumber("MER-13");
employee.setEmployeeLocation("Bangalore");
employee.setTodayDate(new Date());
try{
//get the configuration  and create a session factory once in out application 
SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory();
//create session from session factory to save/retrive or other operation 
Session session=factory.openSession();
//new transaction and save object inside trans
session.beginTransaction();
//save Object 
session.save(employee);
//commit the session 
session.getTransaction().commit();
session.close();
//Retriving All the Object 
employee=null;
session=factory.openSession();
session.beginTransaction();
employee=(EmployeeDetails)session.get(EmployeeDetails.class, "Anand Dwivedi");
System.out.println(employee.getEmployeeName());
}catch(Exception e){
e.printStackTrace();
}

}

}


hibernate Configuration 

<?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">

<hibernate-configuration>
  <session-factory>
  <!--  Postgres DB Details  -->
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hibernate</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">password</property>
      <!--  Postgres DB Details End  -->
    
    <!-- Postgres Dialect has information of list of  queries avil in Postgres DB -->
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <!-- Postgres Dialect End -->
    
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
    <!--  Echo show Query  -->
    <property name="show_sql">true</property>
     <!--  Echo show Query End  -->
    
    <property name="format_sql">true</property>
    
    <!--  Create every time table and insert Data -->
    <property name="hibernate.hbm2ddl.auto">create</property>
    <!--  Create every time table and insert Data End -->
    
    <!--  Define Package for mapping class -->
    <mapping class="com.learnjavabyanand.EmployeeDetails"/>
    <!--  Define Package for mapping class End -->
   
  </session-factory>
</hibernate-configuration>


Snapshot : 




PS: Add All the required Jars 

Transiant ,Temporaland table Annotation in Hibernate


Model Class: 


package com.learnjavabyanand;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * @author ananddw
 *
 */
//@Entity will create table as per class name By if you specify name then table name must be the name what you will provide inside Entity
@Entity(name="employee_Details")
public class EmployeeDetails {

//we can put this annotation above getter as well
@Id
@Column(name="employee_name")
private String employeeName;

//ignore this variable and not create column for us
@Transient
private String employeeNumber;

@Column(name="employee_location")
private String employeeLocation ;

//Used because to store Date not full time stamp
@Temporal(TemporalType.DATE)
private Date todayDate;

public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(String employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getEmployeeLocation() {
return employeeLocation;
}
public void setEmployeeLocation(String employeeLocation) {
this.employeeLocation = employeeLocation;
}
public Date getTodayDate() {
return todayDate;
}
public void setTodayDate(Date todayDate) {
this.todayDate = todayDate;
}



}




HiberNate Client :-


package com.learnjavabyanand;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author ananddw
 *
 */
public class HibernateClient {

public static void main(String[] args) {
// TODO Auto-generated method stub

EmployeeDetails employee= new EmployeeDetails();
employee.setEmployeeName("Anand Dwivedi");
employee.setEmployeeNumber("MER-12");
employee.setEmployeeLocation("Bangalore");
employee.setTodayDate(new Date());

try{
//get the configuration  and create a session factory once in out application
SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory();
//create session from session factory to save/retrive or other operation
Session session=factory.openSession();
//new transaction and save object inside trans
session.beginTransaction();
//save Object
session.save(employee);

//commit the session
session.getTransaction().commit();

}catch(Exception e){
e.printStackTrace();
}

}

}



hibernate.cfg.xml

<?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">

<hibernate-configuration>
  <session-factory>
  <!--  Postgres DB Details  -->
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hibernate</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">password</property>
      <!--  Postgres DB Details End  -->
    
    <!-- Postgres Dialect has information of list of  queries avil in Postgres DB -->
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <!-- Postgres Dialect End -->
    
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
    <!--  Echo show Query  -->
    <property name="show_sql">true</property>
     <!--  Echo show Query End  -->
    
    <property name="format_sql">true</property>
    
    <!--  Create every time table and insert Data -->
    <property name="hibernate.hbm2ddl.auto">create</property>
    <!--  Create every time table and insert Data End -->
    
    <!--  Define Package for mapping class -->
    <mapping class="com.learnjavabyanand.EmployeeDetails"/>
    <!--  Define Package for mapping class End -->
   
  </session-factory>
</hibernate-configuration>


Folder Structure : 





PS: Please Add all the hibernate required jars as well as Database jars

HiberNate Simple Application Using Annotation


Model Class: 


package com.learnjavabyanand;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

/**
 * @author ananddw
 *
 */
//@Entity will create table as per class name By if you specify name then table name must be the name what you will provide inside Entity
@Entity(name="employee_Details")
public class EmployeeDetails {

//we can put this annotation above getter as well
@Id
@Column(name="employee_name")
private String employeeName;

@Column(name="employee_number")
private String employeeNumber;

@Column(name="employee_location")
private String employeeLocation ;


public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(String employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getEmployeeLocation() {
return employeeLocation;
}
public void setEmployeeLocation(String employeeLocation) {
this.employeeLocation = employeeLocation;
}



}



HiberNate Client :-


package com.learnjavabyanand;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 * 
 * @author ananddw
 *
 */
public class HibernateClient {

public static void main(String[] args) {
// TODO Auto-generated method stub
EmployeeDetails employee= new EmployeeDetails();
employee.setEmployeeName("Anand Dwivedi");
employee.setEmployeeNumber("MER-12");
employee.setEmployeeLocation("Bangalore");
try{
//get the configuration  and create a session factory once in out application 
SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory();
//create session from session factory to save/retrive or other operation 
Session session=factory.openSession();
//new transaction and save object inside trans
session.beginTransaction();
//save Object 
session.save(employee);
//commit the session 
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}

}

}


hibernate.cfg.xml

<?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">

<hibernate-configuration>
  <session-factory>
  <!--  Postgres DB Details  -->
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hibernate</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">password</property>
      <!--  Postgres DB Details End  -->
    
    <!-- Postgres Dialect has information of list of  queries avil in Postgres DB -->
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <!-- Postgres Dialect End -->
    
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    
    <!--  Echo show Query  -->
    <property name="show_sql">true</property>
     <!--  Echo show Query End  -->
    
    <property name="format_sql">true</property>
    
    <!--  Create every time table and insert Data -->
    <property name="hibernate.hbm2ddl.auto">create</property>
    <!--  Create every time table and insert Data End -->
    
    <!--  Define Package for mapping class -->
    <mapping class="com.learnjavabyanand.EmployeeDetails"/>
    <!--  Define Package for mapping class End -->
   
  </session-factory>
</hibernate-configuration>


Folder Structure : 





PS: Please Add all the hibernate required jars as well as Database jars

Map class field to map

 import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class AutogeneratedClassMapper {     public static M...