Java

Wednesday, 23 January 2013

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



No comments:

Post a Comment