diff --git a/pom.xml b/pom.xml index 43fcedcf8e..daa92744ae 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,7 @@ spring-hibernate3 spring-hibernate4 spring-jpa + spring-jpa-jndi spring-katharsis spring-mockito spring-mvc-java diff --git a/spring-jpa-jndi/.gitignore b/spring-jpa-jndi/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-jpa-jndi/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-jpa-jndi/README.md b/spring-jpa-jndi/README.md new file mode 100644 index 0000000000..6a99253545 --- /dev/null +++ b/spring-jpa-jndi/README.md @@ -0,0 +1,7 @@ +========= + +## Spring JPA using JNDI Project + + +### Relevant Articles: +- [Spring Persistence (Hibernate and JPA) with a JNDI datasource](http://www.baeldung.com/spring-jpa-fndi) \ No newline at end of file diff --git a/spring-jpa-jndi/pom.xml b/spring-jpa-jndi/pom.xml new file mode 100644 index 0000000000..f7042f2384 --- /dev/null +++ b/spring-jpa-jndi/pom.xml @@ -0,0 +1,145 @@ + + 4.0.0 + + com.baeldung + spring-jpa-jndi + 0.1-SNAPSHOT + war + + spring-jpa-jndi + + + + + + + org.springframework + spring-orm + ${org.springframework.version} + + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + + + javax.servlet + jstl + ${javax.servlet.jstl.version} + + + javax.servlet + servlet-api + ${javax.servlet.servlet-api.version} + + + + + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + + + xml-apis + xml-apis + 1.4.01 + + + org.javassist + javassist + ${javassist.version} + + + org.springframework.data + spring-data-jpa + ${spring-data-jpa.version} + + + + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + javax.el + javax.el-api + 2.2.5 + + + + + + spring-jpa-jndi + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + src/main/webapp + false + + + + + + + + + + 4.3.2.RELEASE + 3.20.0-GA + + + 1.2 + 2.5 + + + 4.3.11.Final + 1.8.2.RELEASE + 1.4.192 + + + 1.7.13 + 1.1.3 + + + 5.2.2.Final + + + 3.5.1 + 2.7 + 2.4 + + + + + \ No newline at end of file diff --git a/spring-jpa-jndi/src/main/java/org/baeldung/config/PersistenceJNDIConfig.java b/spring-jpa-jndi/src/main/java/org/baeldung/config/PersistenceJNDIConfig.java new file mode 100644 index 0000000000..7ea731d9d4 --- /dev/null +++ b/spring-jpa-jndi/src/main/java/org/baeldung/config/PersistenceJNDIConfig.java @@ -0,0 +1,76 @@ +package org.baeldung.config; + +import java.util.Properties; + +import javax.naming.NamingException; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jndi.JndiTemplate; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableTransactionManagement +@PropertySource({ "classpath:persistence-jndi.properties" }) +@ComponentScan({ "org.baeldung.persistence" }) +@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") +public class PersistenceJNDIConfig { + + @Autowired + private Environment env; + + public PersistenceJNDIConfig() { + super(); + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(dataSource()); + em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); + em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); + em.setJpaProperties(additionalProperties()); + return em; + } + + @Bean + public DataSource dataSource() { + try { + return (DataSource) new JndiTemplate().lookup(env.getProperty("jdbc.url")); + } catch (NamingException e) { + throw new IllegalArgumentException("Error looking up JNDI datasource", e); + } + } + + @Bean + public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) { + final JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(emf); + return transactionManager; + } + + @Bean + public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { + return new PersistenceExceptionTranslationPostProcessor(); + } + + final Properties additionalProperties() { + final Properties hibernateProperties = new Properties(); + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); + hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", "false"); + return hibernateProperties; + } +} \ No newline at end of file diff --git a/spring-jpa-jndi/src/main/java/org/baeldung/config/SpringWebConfig.java b/spring-jpa-jndi/src/main/java/org/baeldung/config/SpringWebConfig.java new file mode 100644 index 0000000000..6afb271b10 --- /dev/null +++ b/spring-jpa-jndi/src/main/java/org/baeldung/config/SpringWebConfig.java @@ -0,0 +1,24 @@ +package org.baeldung.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@EnableWebMvc +@Configuration +@ComponentScan({ "org.baeldung.web" }) +public class SpringWebConfig extends WebMvcConfigurerAdapter { + + @Bean + public InternalResourceViewResolver viewResolver() { + InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); + viewResolver.setViewClass(JstlView.class); + viewResolver.setPrefix("/WEB-INF/views/jsp/"); + viewResolver.setSuffix(".jsp"); + return viewResolver; + } +} diff --git a/spring-jpa-jndi/src/main/java/org/baeldung/config/WebInitializer.java b/spring-jpa-jndi/src/main/java/org/baeldung/config/WebInitializer.java new file mode 100644 index 0000000000..1c8ce5400f --- /dev/null +++ b/spring-jpa-jndi/src/main/java/org/baeldung/config/WebInitializer.java @@ -0,0 +1,20 @@ +package org.baeldung.config; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + @Override + protected Class[] getRootConfigClasses() { + return new Class[] { PersistenceJNDIConfig.class }; + } + + @Override + protected Class[] getServletConfigClasses() { + return new Class[] { SpringWebConfig.class }; + } + + @Override + protected String[] getServletMappings() { + return new String[] { "/" }; + } +} diff --git a/spring-jpa-jndi/src/main/java/org/baeldung/persistence/dao/FooDao.java b/spring-jpa-jndi/src/main/java/org/baeldung/persistence/dao/FooDao.java new file mode 100644 index 0000000000..0133a36a14 --- /dev/null +++ b/spring-jpa-jndi/src/main/java/org/baeldung/persistence/dao/FooDao.java @@ -0,0 +1,22 @@ +package org.baeldung.persistence.dao; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import org.baeldung.persistence.model.Foo; +import org.springframework.stereotype.Repository; + +@Repository +public class FooDao { + + @PersistenceContext + private EntityManager entityManager; + + @SuppressWarnings("unchecked") + public List findAll() { + return entityManager.createQuery("from " + Foo.class.getName()).getResultList(); + } + +} diff --git a/spring-jpa-jndi/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-jpa-jndi/src/main/java/org/baeldung/persistence/model/Foo.java new file mode 100644 index 0000000000..d351fc54b8 --- /dev/null +++ b/spring-jpa-jndi/src/main/java/org/baeldung/persistence/model/Foo.java @@ -0,0 +1,34 @@ +package org.baeldung.persistence.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Foo { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "ID") + private long id; + @Column(name = "NAME") + private String name; + + public long getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } +} diff --git a/spring-jpa-jndi/src/main/java/org/baeldung/persistence/service/FooService.java b/spring-jpa-jndi/src/main/java/org/baeldung/persistence/service/FooService.java new file mode 100644 index 0000000000..a3109f5042 --- /dev/null +++ b/spring-jpa-jndi/src/main/java/org/baeldung/persistence/service/FooService.java @@ -0,0 +1,22 @@ +package org.baeldung.persistence.service; + +import java.util.List; + +import org.baeldung.persistence.dao.FooDao; +import org.baeldung.persistence.model.Foo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class FooService { + + @Autowired + private FooDao dao; + + public List findAll() { + return dao.findAll(); + } + +} diff --git a/spring-jpa-jndi/src/main/java/org/baeldung/web/MainController.java b/spring-jpa-jndi/src/main/java/org/baeldung/web/MainController.java new file mode 100644 index 0000000000..6900482de8 --- /dev/null +++ b/spring-jpa-jndi/src/main/java/org/baeldung/web/MainController.java @@ -0,0 +1,21 @@ +package org.baeldung.web; + +import org.baeldung.persistence.service.FooService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class MainController { + + @Autowired + private FooService fooService; + + @GetMapping("/") + public String main(Model model) { + model.addAttribute("foos", fooService.findAll()); + return "index"; + } + +} diff --git a/spring-jpa-jndi/src/main/resources/context.xml b/spring-jpa-jndi/src/main/resources/context.xml new file mode 100644 index 0000000000..a64dfe9a61 --- /dev/null +++ b/spring-jpa-jndi/src/main/resources/context.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/spring-jpa-jndi/src/main/resources/logback.xml b/spring-jpa-jndi/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/spring-jpa-jndi/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-jpa-jndi/src/main/resources/persistence-jndi.properties b/spring-jpa-jndi/src/main/resources/persistence-jndi.properties new file mode 100644 index 0000000000..16d750d7f8 --- /dev/null +++ b/spring-jpa-jndi/src/main/resources/persistence-jndi.properties @@ -0,0 +1,8 @@ +# jdbc.X +jdbc.url=java:comp/env/jdbc/BaeldungDatabase + +# hibernate.X +hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect +hibernate.show_sql=false +#hibernate.hbm2ddl.auto=create +hibernate.hbm2ddl.auto=update \ No newline at end of file diff --git a/spring-jpa-jndi/src/main/resources/server.xml b/spring-jpa-jndi/src/main/resources/server.xml new file mode 100644 index 0000000000..5c61659018 --- /dev/null +++ b/spring-jpa-jndi/src/main/resources/server.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/spring-jpa-jndi/src/main/webapp/WEB-INF/views/jsp/index.jsp b/spring-jpa-jndi/src/main/webapp/WEB-INF/views/jsp/index.jsp new file mode 100644 index 0000000000..f2ec8e2bec --- /dev/null +++ b/spring-jpa-jndi/src/main/webapp/WEB-INF/views/jsp/index.jsp @@ -0,0 +1,14 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + + +Baeldung - Spring JNA JNDI + + + +

+ +

+
+ + \ No newline at end of file