addheader

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....  

No comments:

Post a Comment