Java

Wednesday, 23 January 2013

spring data jpa inheritance table for subclass


spring data jpa inheritance table for subclass:
---------------------------------------------------------------------------
                                                                                                               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="employee")
@Inheritance
(strategy=InheritanceType.JOINED)

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="salariedemp")
@PrimaryKeyJoinColumn
(name="id")
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
{
SalariedEmployee se=
new SalariedEmployee();
se.setAnnualSal(2000);
se.setEname(
"ram");
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:



04:41:19.766 [main] DEBUG org.hibernate.SQL -
alter table salariedemp
drop
foreign key FKA3327E4F5AC3820D
04:41:20.063 [main] DEBUG org.hibernate.SQL -
drop table if exists employee
04:41:20.111 [main] DEBUG org.hibernate.SQL -
drop table if exists salariedemp
04:41:20.158 [main] DEBUG org.hibernate.SQL -
create table employee (
id bigint not null auto_increment,
ename varchar(255),
primary key (id)
)
04:41:20.251 [main] DEBUG org.hibernate.SQL -
create table salariedemp (
annualSal double precision not null,
id bigint not null,
primary key (id)
)
04:41:20.392 [main] DEBUG org.hibernate.SQL -
alter table salariedemp
add index FKA3327E4F5AC3820D (id),
add constraint FKA3327E4F5AC3820D
foreign key (id)
references employee (id)


04:41:22.833 [main] DEBUG org.hibernate.engine.spi.ActionQueue - Executing identity-insert immediately
04:41:22.926 [main] DEBUG org.hibernate.SQL -
insert
into
employee
(ename)
values
(?)
04:41:23.161 [main] DEBUG o.h.id.IdentifierGeneratorHelper - Natively generated identity: 1
04:41:23.176 [main] DEBUG org.hibernate.SQL -
insert
into
salariedemp
(annualSal, id)
values
(?, ?)


putput from sql prompt:

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


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