Sie sind auf Seite 1von 4

1.

To accomplish New teckbook store application, we need to perform following


tasks:
a. configgure persistence in spring configuration file
b. add the JAAR files
c. execute the application

http://teamrockers.me/downloads/load/2016%20Movies/Kuttrame%20Thandanai%20(2016)
%20DVDScr/Mp4/Kuttrame%20Thandanai%20(2016)%20DVDScr-2.mp4

@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManage
rFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/spring_jpa");
dataSource.setUsername( "tutorialuser" );
dataSource.setPassword( "tutorialmy5ql" );
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf
){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5D
ialect");
return properties;
}
}
Also, note that, before Spring 3.2, cglib had to be on the classpath for Java @C
onfiguration classes to work; to better understand the need for cglib as a depen
dency, see this discussion about the cglib artifact in Spring.
3. The JPA Spring Configuration with XML
The same Spring Configuration with XML:

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


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManag
erFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.baeldung.persistence.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapt
er" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</p
rop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManage
rDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring_jpa" />
<property name="username" value="tutorialuser" />
<property name="password" value="tutorialmy5ql" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransacti
onManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven />
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationP
ostProcessor" />
</beans>
There is a relatively small difference between the way Spring is configured in X
ML and the new Java based configuration in XML, a reference to another bean can
point to either the bean or a bean factory for that bean. In Java however, since
the types are different, the compiler doesn t allow it, and so the EntityManagerF
actory is first retrieved from it s bean factory and then passed to the transactio
n manager:
txManager.setEntityManagerFactory( this.entityManagerFactoryBean().getObject() )
;
4. Going full XML-less
Usually JPA defines a persistence unit through the META-INF/persistence.xml file
. Starting with Spring 3.1, the persistence.xml is no longer necessary the Local
ContainerEntityManagerFactoryBean now supports a packagesToScan property where the
packages to scan for @Entity classes can be specified.
This file was the last piece of XML to be removed now, JPA can be fully set up w
ith no XML.
4.1. The JPA Properties
JPA properties would usually be specified in the persistence.xml file; alternati
vely, the properties can be specified directly to the entity manager factory bea
n:
factoryBean.setJpaProperties( this.additionalProperties() );
As a side-note, if Hibernate would be the persistence provider, then this would
be the way to specify Hibernate specific properties.
5. The Maven configuration
In addition to Spring Core
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
<scope>runtime</scope>
</dependency>
TO ADD JAR FILES
Create a new Java Project OverNEW TECH BOOK STORE, right click and the
n create a new folder, call it "libs"
Download the latest libraries of Spring 3 MVC: http://projects.spring.io/spring-
framework/
Once you've downloaded the libraries, extract the content of the zip and copy th
e required "jar" files inside the folder "libs" you've just created.
Once you copied the files to the folder, select all the files, right-click, look
at "Build Path" and click on "Add to Build Path"
then in the project explorer, you will be able to see that in "Referenced Librar
ies" you have the Spring libraries.
Just have fun.
But as recommended and suggested, try downloading maven, there is an integration
also if you go and install from the Eclipse Market: m2eclipse.
Then, another option is like many people answered you: use STS, which comes with
maven and will make your work easier and faster.
Now seems like Spring just wants to work with dependency managers, so you can go
to the central repository.

Das könnte Ihnen auch gefallen