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 

Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Online Book Store Website Using Java

How to read CSV File in Java