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

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Java Design pattern

Map class field to map