diff --git a/persistence-modules/pom.xml b/persistence-modules/pom.xml index 78f1afd39a..6186900a6e 100644 --- a/persistence-modules/pom.xml +++ b/persistence-modules/pom.xml @@ -78,6 +78,7 @@ spring-hibernate-5 spring-hibernate4 spring-jpa + spring-jpa-2 spring-persistence-simple spring-persistence-simple-2 diff --git a/persistence-modules/spring-jpa-2/README.md b/persistence-modules/spring-jpa-2/README.md index 71b368b44a..b1786f49bd 100644 --- a/persistence-modules/spring-jpa-2/README.md +++ b/persistence-modules/spring-jpa-2/README.md @@ -1,19 +1,5 @@ -========= - -## Spring JPA Example Project - +## Spring JPA (2) ### Relevant Articles: - [Many-To-Many Relationship in JPA](https://www.baeldung.com/jpa-many-to-many) -- More articles: [[<-- prev]](/spring-jpa) - - -### Eclipse Config -After importing the project into Eclipse, you may see the following error: -"No persistence xml file found in project" - -This can be ignored: -- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project" -Or: -- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator - +- More articles: [[<-- prev]](/spring-jpa) \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/pom.xml b/persistence-modules/spring-jpa-2/pom.xml index 410ed592b0..08a1f0c6a3 100644 --- a/persistence-modules/spring-jpa-2/pom.xml +++ b/persistence-modules/spring-jpa-2/pom.xml @@ -2,9 +2,9 @@ 4.0.0 - spring-jpa + spring-jpa-2 0.1-SNAPSHOT - spring-jpa + spring-jpa-2 war @@ -31,94 +31,20 @@ spring-context ${org.springframework.version} - - org.springframework - spring-webmvc - ${org.springframework.version} - - org.hibernate hibernate-entitymanager ${hibernate.version} - - xml-apis - xml-apis - ${xml-apis.version} - - - org.javassist - javassist - ${javassist.version} - - - mysql - mysql-connector-java - ${mysql-connector-java.version} - runtime - - - org.springframework.data - spring-data-jpa - ${spring-data-jpa.version} - com.h2database h2 ${h2.version} - - - - org.hibernate - hibernate-validator - ${hibernate-validator.version} - - - javax.el - javax.el-api - ${javax.el-api.version} - - - - - javax.servlet - jstl - ${jstl.version} - - - javax.servlet - servlet-api - provided - ${javax.servlet.servlet-api.version} - - - - - - com.google.guava - guava - ${guava.version} - - - org.assertj - assertj-core - ${assertj.version} - - - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - test - - org.springframework spring-test @@ -131,22 +57,6 @@ 5.1.5.RELEASE - 3.21.0-GA - - 6.0.6 - 2.1.5.RELEASE - - - 2.5 - - - 6.0.15.Final - 1.4.01 - 2.2.5 - - - 21.0 - 3.8.0 \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/PersistenceJPAConfig.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/PersistenceJPAConfig.java deleted file mode 100644 index c489321122..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/PersistenceJPAConfig.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.baeldung.config; - -import com.google.common.base.Preconditions; -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.jdbc.datasource.DriverManagerDataSource; -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; - -import javax.persistence.EntityManagerFactory; -import javax.sql.DataSource; -import java.util.Properties; - -@Configuration -@EnableTransactionManagement -@PropertySource({ "classpath:persistence-h2.properties" }) -@ComponentScan({ "com.baeldung.persistence" }) -@EnableJpaRepositories(basePackages = "com.baeldung.persistence.dao") -public class PersistenceJPAConfig { - - @Autowired - private Environment env; - - public PersistenceJPAConfig() { - super(); - } - - // beans - - @Bean - public LocalContainerEntityManagerFactoryBean entityManagerFactory() { - final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); - em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" }); - - final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); - em.setJpaVendorAdapter(vendorAdapter); - em.setJpaProperties(additionalProperties()); - - return em; - } - - @Bean - public DataSource dataSource() { - final DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); - dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); - dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); - dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); - - return dataSource; - } - - @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/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/PersistenceJPAConfigXml.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/PersistenceJPAConfigXml.java deleted file mode 100644 index 95224a4662..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/PersistenceJPAConfigXml.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.config; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.ImportResource; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -// @Configuration -@EnableTransactionManagement -@ComponentScan({ "com.baeldung.persistence" }) -@ImportResource({ "classpath:jpaConfig.xml" }) -public class PersistenceJPAConfigXml { - - public PersistenceJPAConfigXml() { - super(); - } - -} \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/SpringWebConfig.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/SpringWebConfig.java deleted file mode 100644 index 475970d1f0..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/SpringWebConfig.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.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({ "com.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/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/StudentJpaConfig.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/StudentJpaConfig.java deleted file mode 100644 index 54ced72dd1..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/StudentJpaConfig.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.baeldung.config; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.springframework.orm.jpa.JpaTransactionManager; -import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -import javax.persistence.EntityManagerFactory; -import javax.sql.DataSource; -import java.util.Properties; - -@Configuration -@EnableJpaRepositories(basePackages = "com.baeldung.inmemory.persistence.dao") -@PropertySource("persistence-student.properties") -@EnableTransactionManagement -public class StudentJpaConfig { - - @Autowired - private Environment env; - - @Bean - public DataSource dataSource() { - final DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); - dataSource.setUrl(env.getProperty("jdbc.url")); - dataSource.setUsername(env.getProperty("jdbc.user")); - dataSource.setPassword(env.getProperty("jdbc.pass")); - - return dataSource; - } - - @Bean - public LocalContainerEntityManagerFactoryBean entityManagerFactory() { - final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); - em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "com.baeldung.inmemory.persistence.model" }); - em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); - em.setJpaProperties(additionalProperties()); - return em; - } - - @Bean - JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { - JpaTransactionManager transactionManager = new JpaTransactionManager(); - transactionManager.setEntityManagerFactory(entityManagerFactory); - return transactionManager; - } - - 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.show_sql", env.getProperty("hibernate.show_sql")); - hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache")); - hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache")); - - return hibernateProperties; - } -} diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/WebInitializer.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/WebInitializer.java deleted file mode 100644 index be81cca76b..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/config/WebInitializer.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.config; - -import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; - -public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { - @Override - protected Class[] getRootConfigClasses() { - return new Class[] { PersistenceJPAConfig.class }; - } - - @Override - protected Class[] getServletConfigClasses() { - return new Class[] { SpringWebConfig.class }; - } - - @Override - protected String[] getServletMappings() { - return new String[] { "/" }; - } -} diff --git a/persistence-modules/spring-jpa-2/src/main/resources/context.xml b/persistence-modules/spring-jpa-2/src/main/resources/context.xml deleted file mode 100644 index a64dfe9a61..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/resources/context.xml +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/src/main/resources/logback.xml b/persistence-modules/spring-jpa-2/src/main/resources/logback.xml deleted file mode 100644 index ec0dc2469a..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/resources/logback.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - web - %date [%thread] %-5level %logger{36} - %message%n - - - - - - - - - - - - - - \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/src/main/resources/persistence-h2.properties b/persistence-modules/spring-jpa-2/src/main/resources/persistence-h2.properties deleted file mode 100644 index a3060cc796..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/resources/persistence-h2.properties +++ /dev/null @@ -1,10 +0,0 @@ -# jdbc.X -jdbc.driverClassName=org.h2.Driver -jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 -jdbc.user=sa -jdbc.pass= - -# hibernate.X -hibernate.dialect=org.hibernate.dialect.H2Dialect -hibernate.show_sql=true -hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/src/main/resources/persistence-student.properties b/persistence-modules/spring-jpa-2/src/main/resources/persistence-student.properties deleted file mode 100644 index d4c82420de..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/resources/persistence-student.properties +++ /dev/null @@ -1,11 +0,0 @@ -jdbc.driverClassName=com.mysql.cj.jdbc.Driver -jdbc.url=jdbc:mysql://localhost:3306/myDb -jdbc.user=tutorialuser -jdbc.pass=tutorialpass - -hibernate.dialect=org.hibernate.dialect.MySQL5Dialect -hibernate.show_sql=true -hibernate.hbm2ddl.auto=create-drop - -hibernate.cache.use_second_level_cache=false -hibernate.cache.use_query_cache=false \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/src/main/resources/persistence.xml b/persistence-modules/spring-jpa-2/src/main/resources/persistence.xml deleted file mode 100644 index 57687c306d..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/resources/persistence.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - ${hibernate.hbm2ddl.auto} - ${hibernate.dialect} - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/src/main/resources/server.xml b/persistence-modules/spring-jpa-2/src/main/resources/server.xml deleted file mode 100644 index 5c61659018..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/resources/server.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/src/main/resources/sqlfiles.properties b/persistence-modules/spring-jpa-2/src/main/resources/sqlfiles.properties deleted file mode 100644 index 0bea6adad1..0000000000 --- a/persistence-modules/spring-jpa-2/src/main/resources/sqlfiles.properties +++ /dev/null @@ -1 +0,0 @@ -spring.jpa.hibernate.ddl-auto=none \ No newline at end of file diff --git a/persistence-modules/spring-jpa-2/src/test/java/META-INF/persistence.xml b/persistence-modules/spring-jpa-2/src/test/java/META-INF/persistence.xml deleted file mode 100644 index 495f076fef..0000000000 --- a/persistence-modules/spring-jpa-2/src/test/java/META-INF/persistence.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - com.baeldung.persistence.model.Foo - com.baeldung.persistence.model.Bar - - - - - - - - - - - - - diff --git a/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/SpringContextTest.java b/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/SpringContextTest.java deleted file mode 100644 index abc73e250d..0000000000 --- a/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/SpringContextTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung; - -import com.baeldung.config.PersistenceJPAConfig; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; -import org.springframework.test.context.web.WebAppConfiguration; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) -@WebAppConfiguration -@DirtiesContext -public class SpringContextTest { - - @Test - public void whenSpringContextIsBootstrapped_thenNoExceptions() { - } -} diff --git a/persistence-modules/spring-jpa-2/src/test/resources/persistence-student.properties b/persistence-modules/spring-jpa-2/src/test/resources/persistence-student.properties deleted file mode 100644 index 3b6b580630..0000000000 --- a/persistence-modules/spring-jpa-2/src/test/resources/persistence-student.properties +++ /dev/null @@ -1,9 +0,0 @@ -jdbc.driverClassName=org.h2.Driver -jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1 - -hibernate.dialect=org.hibernate.dialect.H2Dialect -hibernate.show_sql=true -hibernate.hbm2ddl.auto=create - -hibernate.cache.use_second_level_cache=false -hibernate.cache.use_query_cache=false \ No newline at end of file diff --git a/persistence-modules/spring-jpa/README.md b/persistence-modules/spring-jpa/README.md index 3eb8ae8d55..d260913635 100644 --- a/persistence-modules/spring-jpa/README.md +++ b/persistence-modules/spring-jpa/README.md @@ -1,7 +1,4 @@ -========= - -## Spring JPA Example Project - +## Spring JPA (1) ### Relevant Articles: - [The DAO with JPA and Spring](https://www.baeldung.com/spring-dao-jpa)