addheader

Tuesday, 22 January 2013

spring data jpa example program for insert

 

spring data jpa:                                                by

                                                                                madhav

 


TUTIORALS FROM MADHAV:

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

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

     SPRING-DATA-JPA     REST-WEB-SERVICE     STRUTS2HIBERNATE    GWT.... 

 

























------------------------------------------------------------------------------------------------------
pom.xml

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


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion> 

<groupId>jpadata</groupId> 

<artifactId>jpadata</artifactId>

 <version>0.0.1-SNAPSHOT</version> 

<properties> 

<org.springframework.version>3.1.2.RELEASE</org.springframework.version><org.springframework.data.version>1.1.0.RELEASE</org.springframework.data.version><org.hibernate.version>4.1.6.Final</org.hibernate.version><org.hibernate.validator.version>4.2.0.Final</org.hibernate.validator.version><org.slf4j.version>1.6.4</org.slf4j.version><ch.qos.logback.version>1.0.2</ch.qos.logback.version>

 <joda-time.version>2.1</joda-time.version> 

<mysql.version>5.1.19</mysql.version>

 <commons-logging.version>1.1.1</commons-logging.version>

 </properties>

 <dependencies>

 <dependency> 

<groupId>org.springframework.data</groupId>

 <artifactId>spring-data-jpa</artifactId>

 

 <version>${org.springframework.data.version}</version> 

</dependency>

<dependency> 

<groupId>org.springframework</groupId>

 <artifactId>spring-test</artifactId>

 <version>${org.springframework.version}</version>

 </dependency><dependency><groupId>org.hibernate</groupId>

 <artifactId>hibernate-entitymanager</artifactId>

 <version>${org.hibernate.version}</version>

 </dependency>

<dependency> 

<groupId>org.hibernate</groupId>

 <artifactId>hibernate-validator</artifactId> 

<version>${org.hibernate.validator.version}</version> 

</dependency>

 <dependency> 

<groupId>commons-dbcp</groupId> 

<artifactId>commons-dbcp</artifactId>

 <version>1.4</version>

 </dependency>

<dependency> 

<groupId>commons-logging</groupId> 

<artifactId>commons-logging</artifactId>

 <version>${commons-logging.version}</version>

 <scope>provided</scope> 

</dependency> 

<dependency>

 <groupId>org.slf4j</groupId>

 <artifactId>slf4j-api</artifactId> 

<version>${org.slf4j.version}</version></dependency>

<dependency> 
<groupId>org.slf4j</groupId> 
<artifactId>jcl-over-slf4j</artifactId>
 <version>${org.slf4j.version}</version>
 <scope>runtime</scope>
 </dependency>
 <dependency> 
<groupId>org.slf4j</groupId>
 <artifactId>log4j-over-slf4j</artifactId><version>${org.slf4j.version}</version>
 <scope>runtime</scope></dependency>
 <dependency>
 <groupId>ch.qos.logback</groupId>
 <artifactId>logback-classic</artifactId> 
<version>${ch.qos.logback.version}</version> 
<scope>runtime</scope>
 </dependency>
<dependency> 
<groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId> 
<version>3.2.0.RELEASE</version>
 </dependency>

<dependency> 

<groupId>org.springframework</groupId>

 <artifactId>spring-beans</artifactId>

 <version>3.2.0.RELEASE</version> 

</dependency>


 <dependency>

 <groupId>mysql</groupId>

 <artifactId>mysql-connector-java</artifactId>

 <version>5.1.19</version></dependency>

 </dependencies>

 <build>

 <plugins> 

<plugin>

 <groupId>org.apache.maven.plugins</groupId> 

<artifactId>maven-compiler-plugin</artifactId> 

<version>2.3.2</version>

 <configuration><source>1.6</source><target>1.6</target></configuration> 

</plugin> 

</plugins>
</
build> 

</project>

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



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 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>{
}

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

persistance.xml


--------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?><

persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

 <persistence-unit name="spring.data.jpa">
<properties> 
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" /> 
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mysql" /> 
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> 
<property name="hibernate.connection.username" value="root" /> 
<property name="hibernate.connection.password" value="mysql" /> 
<property name="hibernate.show_sql" value="false"/> 
<property name="hibernate.format_sql" value="true"/> 
<property name="hibernate.use_sql_comments" value="false"/> 
<property name="hibernate.hbm2ddl.auto" value="create"/> </properties> 
</persistence-unit></
persistence>  -------------------------------------------------------------------------------------------------------

beans.xml

------------------------------------------------------------------------------------------------------
  <context:component-scan base-package="madhav,repository"></context:component-scan>
<context:annotation-config>
</context:annotation-config>
<!--<context:property-placeholder/>

-->

<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="username" value="root"></property><property name="password" value="mysql"></property>

 <property name="url" value="jdbc:mysql://localhost:3306/mysql"></property><property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><property name="dataSource" ref="dataSource"></property> 

<property name="jpaVendorAdapter">
 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"><property name="showSql" value="false"></property>
 <property name="database" value="MYSQL"></property>
 </bean> 
</property><!-- <property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
</props>
</property>
-->
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"></property>
 <property name="persistenceUnitName" value="spring.data.jpa"></property><aop:scoped-proxy proxy-target-class="false"/> 
</bean>
< bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"><property name="entityManagerFactory" ref="entityManagerFactory"></property>

 <property name="dataSource" ref="dataSource"></property>
 <property name="persistenceUnitName" value="spring.data.jpa"></property> 
</bean>
< jpa:repositories base-package="repository"entity-manager-factory-ref="entityManagerFactory"transaction-manager-ref="transactionManager"></jpa:repositories> 

</beans> 
 
-------------------------------------------------------------------------------------------
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 createCharge() {


try
{
ChargeCode cd=new ChargeCode("softech", "madhav is a good boy", "gmrdizzy@gmail.com");

this.chargeCodeRepository.saveAndFlush(cd);
}
catch(Exception ex)
{

}


}
public ChargeCodeRepository getChargeCodeRepository() {
return chargeCodeRepository;
}
public void setChargeCodeRepository(ChargeCodeRepository chargeCodeRepository) {
this.chargeCodeRepository = chargeCodeRepository;
}


}
-------------------------------------------------------------------------------------------
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.createCharge();
}
}
------------------------------------------------------------------------------------------------------------
 
output in the 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