Java

Wednesday 23 January 2013

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



No comments:

Post a Comment