Java

Wednesday, 23 January 2013

spring data jpa onetoone



spring data jpa onetoone:

----------------------------------------------------------------------By, Madhav
                                                                                                        mail: gmrdizzy@gmail.com



TUTIORALS FROM MADHAV:
 

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 
































NOTE:   1)  maven dependeccy pom.xml  , we can get from previous example.
               2) presestance.xml , beans.xml file also we can get from my previous example program.



----------------------------------------------------------------------------------------

Employee.java

--------------------------------------------------------------------------------------



package
repository;
 

import
javax.persistence.*;

import
org.springframework.data.jpa.domain.AbstractPersistable;

@Entity
@Table
(name="employee1")

public
class Employee extends AbstractPersistable<Long>{
private String ename;
@OneToOne(mappedBy="employee", cascade=CascadeType.ALL)
private PersonalDetails personalDetails;
public PersonalDetails getPersonalDetails() {
return personalDetails;
}
public void setPersonalDetails(PersonalDetails personalDetails) {
this.personalDetails = personalDetails;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
}

-------------------------------------------------------------------------


EmployeeRepository .java

---------------------------------------------------------------------------

package repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface EmployeeRepository extends JpaRepository<Employee, Long>,
JpaSpecificationExecutor<Employee>{

}
--------------------------------------------------------------------------


PersonalDetails .java

----------------------------------------------------------------------------------------



package
repository;
 

import
javax.persistence.*;
import
org.springframework.data.jpa.domain.AbstractPersistable;

@Entity
@Table
(name="personaldetails")

public
class PersonalDetails extends AbstractPersistable<Long>{
private String firstname;
private String lastname;
@OneToOne
@PrimaryKeyJoinColumn
private Employee employee;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}

---------------------------------------------------------------------------

PersonalDetailsRepository

-----------------------------------------------------------------------------------------------------------\



package
repository;

import
org.springframework.data.jpa.repository.JpaRepository;
import
org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public
interface PersonalDetailsRepository extends JpaRepository<PersonalDetails, Long>,
JpaSpecificationExecutor<PersonalDetails>{
}


-------------------------------------------------------------------------------------------------------------------

TestCase .java

-----------------------------------------------------------------------------------------



package madhav;
import repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class TestCase {

 @Autowired
    private EmployeeRepository employeeRepository;
 @Autowired
 private PersonalDetailsRepository personalDetailsRepository;
   
    @Transactional
 public void createEmployee() {
         
         try
         {
       
          PersonalDetails pd=new PersonalDetails();
            
             pd.setFirstname("abc");
             pd.setLastname("rao");
             Employee e=new Employee();
             e.setEname("madhav");
             pd.setEmployee(e);
           
           System.out.println("before save");
           employeeRepository.saveAndFlush(e);
           personalDetailsRepository.saveAndFlush(pd);
            
           /*
            *
            NOTE:      another situation of onetoone
                       iam trying to attach the "PersonalDetails" details to the exist "Employee"
          
          
          
           Employee employee=employeeRepository.findOne(1);
           PersonalDetails pd=new PersonalDetails();
           pd.setFirstname("abc");
           pd.setLastname("rao");
           pd.setEmployee(e);
           employeeRepository.saveAndFlush(e);
           personalDetailsRepository.saveAndFlush(pd);*/
          
          
            
         }
         catch(Exception ex)
         {
         
         }
        
     
 }
 public PersonalDetailsRepository getPersonalDetailsRepository() {
  return personalDetailsRepository;
 }
 public void setPersonalDetailsRepository(
   PersonalDetailsRepository personalDetailsRepository) {
  this.personalDetailsRepository = personalDetailsRepository;
 }
 public EmployeeRepository getEmployeeRepository() {
  return employeeRepository;
 }
 public void setEmployeeRepository(EmployeeRepository employeeRepository) {
  this.employeeRepository = employeeRepository;
 }


 
}
--------------------------------------------------------------------------------------------------------------------------




output from console:

--------------------------------





07:25:00.282 [main] DEBUG org.hibernate.SQL -
insert
into
employee1
(ename)
values
(?)




07:25:00.437 [main] DEBUG org.hibernate.SQL -
insert
into
personaldetails
(firstname, lastname)
values
(?, ?)



output from mysql console
------------------------------------






TUTIORALS FROM MADHAV:
 

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT....  

spring data jpa inheritance table for class


spring data jpa inheritance table for class:
---------------------------------------------------------------------------

                                                                                                               by ,mdhav
                                                                                                                mail: gmrdizzy@gmail.com

 


TUTIORALS FROM MADHAV:
 

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 































NOTE:   1)  maven dependeccy pom.xml  , we can get from previous example.
               2) presestance.xml , beans.xml file also we can get from my previous example program.



Employee .java


-----------------------------------------------------------------------------------------------------------------------------

package repository;
import javax.persistence.*;
import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
@Table(name="employeeinfo")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="discriminator",
    discriminatorType=DiscriminatorType.STRING
)
@DiscriminatorValue(value="e")

public class Employee extends AbstractPersistable<Long> {
       
         
            private String ename;
           
            public String getEname() {
                        return ename;
            }
            public void setEname(String ename) {
                        this.ename = ename;
            }
}


----------------------------------------------------------------------------------------------

EmployeeRepository .java

------------------------------------------------------------------------------------------------------------------------------------


package
repository;


import
org.springframework.data.jpa.repository.JpaRepository;
import
org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public
interface EmployeeRepository extends JpaRepository<Employee, Long>,

JpaSpecificationExecutor<Employee>{
}

--------------------------------------------------------------------------------------


SalariedEmployee .java

----------------------------------------------------------------------------------------------------------------------------

package repository; import javax.persistence.*; import org.springframework.data.jpa.domain.AbstractPersistable;

@Entity
@Table(name="employeeinfo")
@DiscriminatorValue("se")
public class SalariedEmployee extends Employee {
            public double annualSal;
            public double getAnnualSal() {
                        return annualSal;
            }
            public void setAnnualSal(double annualSal) {
                        this.annualSal = annualSal;
            }
}


-----------------------------------------------------------------------------------------------------------------------------------


SalariedEmployeeRepository .java

-------------------------------------------------------------------------------------------------------------------------------



package
repository;


import
org.springframework.data.jpa.repository.JpaRepository;
import

org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public
interface SalariedEmployeeRepository extends JpaRepository<SalariedEmployee, Long>,

JpaSpecificationExecutor<SalariedEmployee>{
}

-----------------------------------------------------------------------------------------------------------------------

TestCase .java

--------------------------------------------------------------------------------------------------------------------


package
madhav;
import

repository.*;
import

org.springframework.beans.factory.annotation.Autowired;
import

org.springframework.stereotype.Service;
import

org.springframework.transaction.annotation.Transactional;
@Service

public
class TestCase {
@Autowired
private EmployeeRepository employeeRepository;
@Autowired
private SalariedEmployeeRepository salariedEmployeeRepository;
@Transactional
public void createEmployee() {
try
{
Employee e=new
Employee();
e.setEname("madhav");
SalariedEmployee se=new
SalariedEmployee();
se.setAnnualSal(2000);
se.setEname("raghu");
employeeRepository.saveAndFlush(e);
salariedEmployeeRepository.saveAndFlush(se);
}
catch(Exception ex)
{
}
}
public EmployeeRepository getEmployeeRepository() {
return employeeRepository;
}
public void setEmployeeRepository(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
public SalariedEmployeeRepository getSalariedEmployeeRepository() {
return salariedEmployeeRepository;
}
public void setSalariedEmployeeRepository(
SalariedEmployeeRepository salariedEmployeeRepository) {
this.salariedEmployeeRepository = salariedEmployeeRepository;
}
}

-------------------------------------------------------------------------------------------------------------------------------------


Test .java

---------------------------------------------------------------------------------------------------------------------------------
package madhav;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");

  Object tc=(Object)ctx.getBean("testCase");
  TestCase g=(TestCase)tc;
  g.createEmployee();
 }
}
 ---------------------------------------------------------------------------------------------------------------------------------------------



output from cosole:


05:40:22.496 [main] DEBUG org.hibernate.SQL -
drop table if exists employeeinfo
05:40:22.558 [main] DEBUG org.hibernate.SQL -
create table employeeinfo (
discriminator varchar(31) not null,
id bigint not null auto_increment,
ename varchar(255),
annualSal double precision,
primary key (id)
)
05:40:22.683 [main] INFO o.h.tool.hbm2ddl.SchemaExport - HHH000230: Schema export complete

05:40:23.890 [main] DEBUG org.hibernate.engine.spi.ActionQueue - Executing identity-insert immediately
05:40:23.984 [main] DEBUG org.hibernate.SQL -
insert
into
employeeinfo
(ename, discriminator)
values
(?, 'e')


05:40:24.266 [main] DEBUG org.hibernate.SQL -
insert
into
employeeinfo
(ename, annualSal, discriminator)
values
(?, ?, 'se')


putput from sql prompt:

--------------------------------


 

 Related topics:


               1.  pring data jpa example program for insert


        6. spring data jpa onetoone

 

TUTIORALS FROM MADHAV:
 

     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY

    SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT....