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


spring data jpa example for update

spring data jpa example for update:

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




ChargeCode.java:

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

package repository;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
@Table(name = "charge_codes")
public class ChargeCode extends AbstractPersistable<Long>{
 @Column(name = "charge_code", nullable = false, length = 20, unique=true)
 private String chargeCode;

 @Column(name = "charge_description", nullable = false, length = 20)
 private String chargeDescription;

 @Column(name = "charge_type", nullable = false, length = 20)
 private String chargeType;

 public ChargeCode(String chargeCode,String chargeDescription,String chargeType)
 {
  this.chargeCode=chargeCode;
  this.chargeDescription=chargeDescription;
  this.chargeType=chargeType;
 }


 public String getChargeCode() {
  return chargeCode;
 }

 public void setChargeCode(String chargeCode) {
  this.chargeCode = chargeCode;
 }

 public String getChargeDescription() {
  return chargeDescription;
 }

 public void setChargeDescription(String chargeDescription) {
  this.chargeDescription = chargeDescription;
 }

 public String getChargeType() {
  return chargeType;
 }

 public void setChargeType(String chargeType) {
  this.chargeType = chargeType;
 }

 public ChargeCode() {
  // TODO Auto-generated constructor stub
 }
}
-------------------------------------------------------------------------------

ChargeCodeRepository.java


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

package repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface ChargeCodeRepository extends JpaRepository<ChargeCode, Long>,
JpaSpecificationExecutor<ChargeCode>{
}
------------------------------------------------------------------------

TestCase.java

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

package madhav;
import repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class TestCase {

 @Autowired
    private ChargeCodeRepository chargeCodeRepository;
   

    @Transactional
 public void updateCharge() {
           
         try
         {

//hear i am trying to updating the charge_codes
ChargeCode cd=
this.chargeCodeRepository.findOne(new Long(4));
cd.setChargeCode("madhav");
cd.setChargeDescription("madhav is bad boy");

this.chargeCodeRepository.saveAndFlush(cd);         }
         catch(Exception ex)
         {
         
         }
        
     
 }
 public ChargeCodeRepository getChargeCodeRepository() {
  return chargeCodeRepository;
 }
 public void setChargeCodeRepository(ChargeCodeRepository chargeCodeRepository) {
  this.chargeCodeRepository = chargeCodeRepository;
 }
 public ChargeCode getCurrentAuditor() {
  // TODO Auto-generated method stub
  return null;
 }

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

Test.java

------------------------------------------------------------------------------------
package madhav;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

 public static void main(String[] args) {

  ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");

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

output from console:


00:21:09.322 [main] DEBUG org.hibernate.loader.Loader - Loading entity: [repository.ChargeCode#3]
00:21:09.416 [main] DEBUG org.hibernate.SQL -
select
chargecode0_.id as id0_0_,
chargecode0_.charge_code as charge2_0_0_,
chargecode0_.charge_description as charge3_0_0_,
chargecode0_.charge_type as charge4_0_0_
from
charge_codes chargecode0_
where
chargecode0_.id=?





00:34:49.442 [main] DEBUG o.h.internal.util.EntityPrinter - repository.ChargeCode{id=4, chargeDescription=madhav is bad boy, chargeType=gmrdizzy@gmail.com, chargeCode=madhav}
00:34:49.520 [main] DEBUG org.hibernate.SQL -
update
charge_codes
set
charge_code=?,
charge_description=?,
charge_type=?
where
id=?
00:34:49.911 [main] DEBUG o.s.orm.jpa.JpaTransactionManager - Initiating transaction commit


output from mysql prompt:


TUTIORALS FROM MADHAV:
 

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

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



spring data jpa example for delete

spring data jpa example for delete:

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




ChargeCode.java:

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

package repository;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
@Table(name = "charge_codes")
public class ChargeCode extends AbstractPersistable<Long>{
 @Column(name = "charge_code", nullable = false, length = 20, unique=true)
 private String chargeCode;

 @Column(name = "charge_description", nullable = false, length = 20)
 private String chargeDescription;

 @Column(name = "charge_type", nullable = false, length = 20)
 private String chargeType;

 public ChargeCode(String chargeCode,String chargeDescription,String chargeType)
 {
  this.chargeCode=chargeCode;
  this.chargeDescription=chargeDescription;
  this.chargeType=chargeType;
 }


 public String getChargeCode() {
  return chargeCode;
 }

 public void setChargeCode(String chargeCode) {
  this.chargeCode = chargeCode;
 }

 public String getChargeDescription() {
  return chargeDescription;
 }

 public void setChargeDescription(String chargeDescription) {
  this.chargeDescription = chargeDescription;
 }

 public String getChargeType() {
  return chargeType;
 }

 public void setChargeType(String chargeType) {
  this.chargeType = chargeType;
 }

 public ChargeCode() {

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

ChargeCodeRepository.java


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

package repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface ChargeCodeRepository extends JpaRepository<ChargeCode, Long>,
JpaSpecificationExecutor<ChargeCode>{
}
------------------------------------------------------------------------

TestCase.java

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

package madhav;
import repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class TestCase {

 @Autowired
    private ChargeCodeRepository chargeCodeRepository;


    @Transactional
 public void deleteCharge() {
       
         try
         {
  ChargeCode cd=this.chargeCodeRepository.findOne(new Long(1));

  this.chargeCodeRepository.delete(cd);
         }
         catch(Exception ex)
         {
         
         }
        
     
 }
 public ChargeCodeRepository getChargeCodeRepository() {
  return chargeCodeRepository;
 }
 public void setChargeCodeRepository(ChargeCodeRepository chargeCodeRepository) {
  this.chargeCodeRepository = chargeCodeRepository;
 }
 public ChargeCode getCurrentAuditor() {

  return null;
 }

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

Test.java

------------------------------------------------------------------------------------
package madhav;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

 public static void main(String[] args) {

  ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");

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

output from console:


00:21:09.322 [main] DEBUG org.hibernate.loader.Loader - Loading entity: [repository.ChargeCode#3]
00:21:09.416 [main] DEBUG org.hibernate.SQL -
select
chargecode0_.id as id0_0_,
chargecode0_.charge_code as charge2_0_0_,
chargecode0_.charge_description as charge3_0_0_,
chargecode0_.charge_type as charge4_0_0_
from
charge_codes chargecode0_
where
chargecode0_.id=?



00:21:09.900 [main] DEBUG o.h.internal.util.EntityPrinter - repository.ChargeCode{id=3, chargeDescription=madhav is bad boy, chargeType=gmrdizzy@gmail.com, chargeCode=madhav}
00:21:09.962 [main] DEBUG org.hibernate.SQL -
delete
from
charge_codes
where
id=?
00:21:10.103 [main] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - committed JDBC Connection


output from mysql 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....