spring data jpa onetoone:
----------------------------------------------------------------------By, Madhav
mail: gmrdizzy@gmail.com
TUTIORALS FROM MADHAV:
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>{
}
}
}
}
}-------------------------------------------------------------------------
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>{
}
}
}
}
}
}
}
---------------------------------------------------------------------------
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)
{
}
}
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;
}
return personalDetailsRepository;
}
public void setPersonalDetailsRepository(
PersonalDetailsRepository personalDetailsRepository) {
this.personalDetailsRepository = personalDetailsRepository;
}
PersonalDetailsRepository personalDetailsRepository) {
this.personalDetailsRepository = personalDetailsRepository;
}
public EmployeeRepository getEmployeeRepository() {
return employeeRepository;
}
return employeeRepository;
}
public void setEmployeeRepository(EmployeeRepository employeeRepository) {
this.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
------------------------------------
Related topics:
6. spring data jpa onetoone
6. spring data jpa onetoone
TUTIORALS FROM MADHAV:
Love your tutorials . Tnank you!
ReplyDelete