addheader

Wednesday, 23 January 2013

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




No comments:

Post a Comment