Skip to content

Commit e318452

Browse files
varomorfpivovarit
authored andcommitted
Code for Spring Persistence (Hibernate and JPA) with a JNDI datasource article (eugenp#632)
* Add new module for mocks comparison. * Add sources for testing. * Changes on testCase. * Enter some tests for mockito. * More tests for Mockito. * Even more tests. * Add the rest of the mocking libraries. * Javadoc on test. * Test bare bones for EasyMock. * Fist kind of test and setup. * Add tests using EasyMock with a change on LoginService. * Create LoginControllerTest.java * Test setup * [JMockit] No method called test. * [JMockit] Two methods called test. * [JMockit] One method called test. * [JMockit] Exception mock test * [JMockit] Mocked object to pass around test. * [JMockit] Custom matcher test. * [JMockit] Partial mocking test. * [JMockit] Fix with IDE. * Not stubs. Mocks. MOCKS!!! * Remove unnecesary import. * Use correct encoding. Was having problems with buildings. * Remove failing module. * Create new module mocks and move mock-comparisons there. * Add jmockit module. * Add model class. * Add collaborator class. * Add performer class. * Add performer test. * Fix * Add interface for tests. * Test for any. * Test for with. * Test for null. * Test for times. * Test for arg that. * Test for result and returns. * Test for delegate. * Add verifications to any tests. * Add verifications to with test. * Add verification examples to methods using null. * Add verifications to methods using times. * Formatting. * Compress tests and fix one test. * Adding new article to readme. * [BAEL-178] Add collaborator for advanced article. * [BAEL-178] Add link to readme. * [BAEL-178] Add test for mockUp. * [BAEL-178] Add test for invoke method. * [BAEL-178] Add constructors and tests for mockup for constructors. * [BAEL-178] Add private fields and more test for deencapsulation. * [BAEL-178] Add inner class and test for instantiating inner classes. * [BAEL-178] Multimocks. * [BAEL-178] Add test for expectation reusing. * [BAEL-178] Move test class to tests folders. * Add postgresql dependency. * Add test and config with properties. * [BAEL-114] Add new project for JPA with JNDI. * [BAEL-114] Config without xml. * [BAEL-114] Bring part of Foo, FooServie and FooDao. * [BAEL-114] Show all foos. * [BAEL-114] Readme. * [BAEL-114] Undo changes on main jpa project. * [BAEL-114] Remove unnecesary dependencies. * [BAEL-114] Add tomcat config. * [BAEL-114] Fixes.
1 parent 03f1602 commit e318452

16 files changed

Lines changed: 434 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<module>spring-hibernate3</module>
7575
<module>spring-hibernate4</module>
7676
<module>spring-jpa</module>
77+
<module>spring-jpa-jndi</module>
7778
<module>spring-katharsis</module>
7879
<module>spring-mockito</module>
7980
<module>spring-mvc-java</module>

‎spring-jpa-jndi/.gitignore‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

‎spring-jpa-jndi/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=========
2+
3+
## Spring JPA using JNDI Project
4+
5+
6+
### Relevant Articles:
7+
- [Spring Persistence (Hibernate and JPA) with a JNDI datasource](http://www.baeldung.com/spring-jpa-fndi)

‎spring-jpa-jndi/pom.xml‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-jpa-jndi</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
9+
<name>spring-jpa-jndi</name>
10+
11+
<dependencies>
12+
13+
<!-- Spring -->
14+
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-orm</artifactId>
18+
<version>${org.springframework.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-context</artifactId>
23+
<version>${org.springframework.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring-webmvc</artifactId>
28+
<version>${org.springframework.version}</version>
29+
</dependency>
30+
31+
<!-- web -->
32+
<dependency>
33+
<groupId>javax.servlet</groupId>
34+
<artifactId>jstl</artifactId>
35+
<version>${javax.servlet.jstl.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>javax.servlet</groupId>
39+
<artifactId>servlet-api</artifactId>
40+
<version>${javax.servlet.servlet-api.version}</version>
41+
</dependency>
42+
43+
<!-- persistence -->
44+
45+
<dependency>
46+
<groupId>org.hibernate</groupId>
47+
<artifactId>hibernate-entitymanager</artifactId>
48+
<version>${hibernate.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>xml-apis</groupId>
52+
<artifactId>xml-apis</artifactId>
53+
<version>1.4.01</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.javassist</groupId>
57+
<artifactId>javassist</artifactId>
58+
<version>${javassist.version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.data</groupId>
62+
<artifactId>spring-data-jpa</artifactId>
63+
<version>${spring-data-jpa.version}</version>
64+
</dependency>
65+
66+
<!-- validation -->
67+
68+
<dependency>
69+
<groupId>org.hibernate</groupId>
70+
<artifactId>hibernate-validator</artifactId>
71+
<version>${hibernate-validator.version}</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>javax.el</groupId>
75+
<artifactId>javax.el-api</artifactId>
76+
<version>2.2.5</version>
77+
</dependency>
78+
79+
</dependencies>
80+
81+
<build>
82+
<finalName>spring-jpa-jndi</finalName>
83+
<resources>
84+
<resource>
85+
<directory>src/main/resources</directory>
86+
<filtering>true</filtering>
87+
</resource>
88+
</resources>
89+
90+
<plugins>
91+
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-compiler-plugin</artifactId>
95+
<version>${maven-compiler-plugin.version}</version>
96+
<configuration>
97+
<source>1.8</source>
98+
<target>1.8</target>
99+
</configuration>
100+
</plugin>
101+
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-war-plugin</artifactId>
105+
<version>${maven-war-plugin.version}</version>
106+
<configuration>
107+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
108+
<failOnMissingWebXml>false</failOnMissingWebXml>
109+
</configuration>
110+
</plugin>
111+
112+
</plugins>
113+
114+
</build>
115+
116+
<properties>
117+
<!-- Spring -->
118+
<org.springframework.version>4.3.2.RELEASE</org.springframework.version>
119+
<javassist.version>3.20.0-GA</javassist.version>
120+
121+
<!-- web -->
122+
<javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
123+
<javax.servlet.servlet-api.version>2.5</javax.servlet.servlet-api.version>
124+
125+
<!-- persistence -->
126+
<hibernate.version>4.3.11.Final</hibernate.version>
127+
<spring-data-jpa.version>1.8.2.RELEASE</spring-data-jpa.version>
128+
<h2.version>1.4.192</h2.version>
129+
130+
<!-- logging -->
131+
<org.slf4j.version>1.7.13</org.slf4j.version>
132+
<logback.version>1.1.3</logback.version>
133+
134+
<!-- various -->
135+
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
136+
137+
<!-- maven plugins -->
138+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
139+
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
140+
<maven-war-plugin.version>2.4</maven-war-plugin.version>
141+
<!-- <maven-war-plugin.version>2.6</maven-war-plugin.version> -->
142+
143+
</properties>
144+
145+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.baeldung.config;
2+
3+
import java.util.Properties;
4+
5+
import javax.naming.NamingException;
6+
import javax.persistence.EntityManagerFactory;
7+
import javax.sql.DataSource;
8+
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.ComponentScan;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.context.annotation.PropertySource;
14+
import org.springframework.core.env.Environment;
15+
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
16+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
17+
import org.springframework.jndi.JndiTemplate;
18+
import org.springframework.orm.jpa.JpaTransactionManager;
19+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
20+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
21+
import org.springframework.transaction.PlatformTransactionManager;
22+
import org.springframework.transaction.annotation.EnableTransactionManagement;
23+
24+
@Configuration
25+
@EnableTransactionManagement
26+
@PropertySource({ "classpath:persistence-jndi.properties" })
27+
@ComponentScan({ "org.baeldung.persistence" })
28+
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
29+
public class PersistenceJNDIConfig {
30+
31+
@Autowired
32+
private Environment env;
33+
34+
public PersistenceJNDIConfig() {
35+
super();
36+
}
37+
38+
@Bean
39+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
40+
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
41+
em.setDataSource(dataSource());
42+
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
43+
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
44+
em.setJpaProperties(additionalProperties());
45+
return em;
46+
}
47+
48+
@Bean
49+
public DataSource dataSource() {
50+
try {
51+
return (DataSource) new JndiTemplate().lookup(env.getProperty("jdbc.url"));
52+
} catch (NamingException e) {
53+
throw new IllegalArgumentException("Error looking up JNDI datasource", e);
54+
}
55+
}
56+
57+
@Bean
58+
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
59+
final JpaTransactionManager transactionManager = new JpaTransactionManager();
60+
transactionManager.setEntityManagerFactory(emf);
61+
return transactionManager;
62+
}
63+
64+
@Bean
65+
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
66+
return new PersistenceExceptionTranslationPostProcessor();
67+
}
68+
69+
final Properties additionalProperties() {
70+
final Properties hibernateProperties = new Properties();
71+
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
72+
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
73+
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", "false");
74+
return hibernateProperties;
75+
}
76+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
8+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
9+
import org.springframework.web.servlet.view.JstlView;
10+
11+
@EnableWebMvc
12+
@Configuration
13+
@ComponentScan({ "org.baeldung.web" })
14+
public class SpringWebConfig extends WebMvcConfigurerAdapter {
15+
16+
@Bean
17+
public InternalResourceViewResolver viewResolver() {
18+
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
19+
viewResolver.setViewClass(JstlView.class);
20+
viewResolver.setPrefix("/WEB-INF/views/jsp/");
21+
viewResolver.setSuffix(".jsp");
22+
return viewResolver;
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
@Override
7+
protected Class<?>[] getRootConfigClasses() {
8+
return new Class[] { PersistenceJNDIConfig.class };
9+
}
10+
11+
@Override
12+
protected Class<?>[] getServletConfigClasses() {
13+
return new Class[] { SpringWebConfig.class };
14+
}
15+
16+
@Override
17+
protected String[] getServletMappings() {
18+
return new String[] { "/" };
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.persistence.dao;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.EntityManager;
6+
import javax.persistence.PersistenceContext;
7+
8+
import org.baeldung.persistence.model.Foo;
9+
import org.springframework.stereotype.Repository;
10+
11+
@Repository
12+
public class FooDao {
13+
14+
@PersistenceContext
15+
private EntityManager entityManager;
16+
17+
@SuppressWarnings("unchecked")
18+
public List<Foo> findAll() {
19+
return entityManager.createQuery("from " + Foo.class.getName()).getResultList();
20+
}
21+
22+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.baeldung.persistence.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Foo {
11+
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.AUTO)
14+
@Column(name = "ID")
15+
private long id;
16+
@Column(name = "NAME")
17+
private String name;
18+
19+
public long getId() {
20+
return id;
21+
}
22+
23+
public void setId(final int id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(final String name) {
32+
this.name = name;
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.baeldung.persistence.service;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.persistence.dao.FooDao;
6+
import org.baeldung.persistence.model.Foo;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.transaction.annotation.Transactional;
10+
11+
@Service
12+
@Transactional
13+
public class FooService {
14+
15+
@Autowired
16+
private FooDao dao;
17+
18+
public List<Foo> findAll() {
19+
return dao.findAll();
20+
}
21+
22+
}

0 commit comments

Comments
 (0)