This commit is contained in:
Jonathan Cook
2019-10-23 15:01:44 +02:00
parent db85c8f275
commit 684ec0d2e3
20486 changed files with 1642483 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
*.class
derby.log
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear
+11
View File
@@ -0,0 +1,11 @@
## Spring `Exception`s
This module contains articles about Spring `Exception`s
### Relevant articles:
- [Spring BeanCreationException](https://www.baeldung.com/spring-beancreationexception)
- [Spring DataIntegrityViolationException](https://www.baeldung.com/spring-dataIntegrityviolationexception)
- [Spring BeanDefinitionStoreException](https://www.baeldung.com/spring-beandefinitionstoreexception)
- [Spring NoSuchBeanDefinitionException](https://www.baeldung.com/spring-nosuchbeandefinitionexception)
- [Guide to Spring NonTransientDataAccessException](https://www.baeldung.com/nontransientdataaccessexception)
- [Hibernate Mapping Exception Unknown Entity](https://www.baeldung.com/hibernate-mappingexception-unknown-entity)
+207
View File
@@ -0,0 +1,207 @@
<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>com.baeldung</groupId>
<artifactId>spring-exceptions</artifactId>
<version>0.1-SNAPSHOT</version>
<name>spring-exceptions</name>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- persistence -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>${tomcat-dbcp.version}</version>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<!-- web -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
<scope>runtime</scope>
</dependency>
<!-- util -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
<scope>test</scope>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>${javax.el.version}</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>${derby.version}</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>${derby.version}</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbynet</artifactId>
<version>${derby.version}</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>${derby.version}</version>
</dependency>
</dependencies>
<build>
<finalName>spring-exceptions</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- Spring -->
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
<javassist.version>3.21.0-GA</javassist.version>
<!-- persistence -->
<hibernate.version>5.2.5.Final</hibernate.version>
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
<tomcat-dbcp.version>7.0.73</tomcat-dbcp.version>
<derby.version>10.13.1.1</derby.version>
<!-- various -->
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
<javax.el.version>2.2</javax.el.version>
<!-- util -->
<guava.version>19.0</guava.version>
<httpcore.version>4.4.5</httpcore.version>
<httpclient.version>4.5.2</httpclient.version>
<rest-assured.version>2.9.0</rest-assured.version>
<!-- maven plugins -->
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
</properties>
</project>
@@ -0,0 +1,12 @@
package org.baeldung.ex.beancreationexception.cause1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Autowired
private BeanB dependency;
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause1;
public class BeanB {
//
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.beancreationexception.cause2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Autowired
private IBeanB dependency;
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause2;
import org.springframework.stereotype.Component;
@Component
public class BeanB1 implements IBeanB {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause2;
import org.springframework.stereotype.Component;
@Component
public class BeanB2 implements IBeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause2;
public interface IBeanB {
//
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.beancreationexception.cause3;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
public BeanA() {
super();
throw new NullPointerException();
}
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause4;
import org.springframework.stereotype.Component;
@Component
public abstract class BeanA implements IBeanA {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause4;
public interface IBeanA {
//
}
@@ -0,0 +1,13 @@
package org.baeldung.ex.beancreationexception.cause5;
import org.springframework.stereotype.Component;
@Component
public class BeanA implements IBeanA {
public BeanA(final String name) {
super();
System.out.println(name);
}
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause5;
public interface IBeanA {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause6;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
private IBeanB dependency;
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.beancreationexception.cause6;
import org.springframework.stereotype.Component;
@Component
public class BeanB implements IBeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause6;
public interface IBeanB {
//
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.cause8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA implements IBeanA {
private IBeanB beanB;
@Autowired
public BeanA(final IBeanB beanB) {
super();
this.beanB = beanB;
}
}
@@ -0,0 +1,15 @@
package org.baeldung.ex.beancreationexception.cause8;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanB implements IBeanB {
final IBeanA beanA;
@Autowired
public BeanB(final IBeanA beanA) {
super();
this.beanA = beanA;
}
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause8;
public interface IBeanA {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause8;
public interface IBeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause9;
public abstract class BeanA implements IBeanA {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause9;
public class BeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.beancreationexception.cause9;
public interface IBeanA {
//
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause1")
public class Cause1ContextWithJavaConfig {
public Cause1ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause2")
public class Cause2ContextWithJavaConfig {
public Cause2ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause3")
public class Cause3ContextWithJavaConfig {
public Cause3ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,18 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause4")
@ImportResource("classpath:beancreationexception_cause4.xml")
public class Cause4ContextWithJavaConfig {
public Cause4ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause5")
public class Cause5ContextWithJavaConfig {
public Cause5ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,18 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause6")
@ImportResource("classpath:beancreationexception_cause6.xml")
public class Cause6ContextWithJavaConfig {
public Cause6ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,18 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause7")
@ImportResource("classpath:beancreationexception_cause7.xml")
public class Cause7ContextWithJavaConfig {
public Cause7ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beancreationexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause8")
public class Cause8ContextWithJavaConfig {
public Cause8ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,30 @@
package org.baeldung.ex.beancreationexception.spring;
import org.baeldung.ex.beancreationexception.cause9.BeanB;
import org.springframework.beans.factory.BeanFactory;
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.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beancreationexception.cause9")
@ImportResource("classpath:beancreationexception_cause9.xml")
public class Cause9ContextWithJavaConfig {
@Autowired
BeanFactory beanFactory;
public Cause9ContextWithJavaConfig() {
super();
}
// beans
@Bean
public BeanB beanB() {
beanFactory.getBean("beanA");
return new BeanB();
}
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.beandefinitionstoreexception.cause2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Value("${some.property2}")
private String someProperty2;
}
@@ -0,0 +1,18 @@
package org.baeldung.ex.beandefinitionstoreexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ComponentScan("org.baeldung.ex.beandefinitionstoreexception.cause1")
@ImportResource("beans.xml")
public class Cause1ContextWithJavaConfig {
public Cause1ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,27 @@
package org.baeldung.ex.beandefinitionstoreexception.spring;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan("org.baeldung.ex.beandefinitionstoreexception.cause2")
public class Cause2ContextWithJavaConfig {
@Value("${some.property}")
private String someProperty;
public Cause2ContextWithJavaConfig() {
super();
}
// beans
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.beandefinitionstoreexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.beandefinitionstoreexception.cause3")
public class Cause3ContextWithJavaConfig {
public Cause3ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,26 @@
package org.baeldung.ex.dataintegrityviolationexception.spring;
import org.baeldung.spring.config.PersistenceConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
@Configuration
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause1")
@Import(PersistenceConfig.class)
public class Cause1DataContextWithJavaConfig {
public Cause1DataContextWithJavaConfig() {
super();
}
// beans
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
@@ -0,0 +1,19 @@
package org.baeldung.ex.dataintegrityviolationexception.spring;
import org.baeldung.spring.config.PersistenceConfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause2")
@Import(PersistenceConfig.class)
public class Cause2DataContextWithJavaConfig {
public Cause2DataContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,19 @@
package org.baeldung.ex.dataintegrityviolationexception.spring;
import org.baeldung.spring.config.PersistenceConfig;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@ComponentScan("org.baeldung.ex.dataIntegrityviolationexception.cause3")
@Import(PersistenceConfig.class)
public class Cause3DataContextWithJavaConfig {
public Cause3DataContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,29 @@
package org.baeldung.ex.mappingexception.cause1.persistence.model;
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
public Foo() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
}
@@ -0,0 +1,31 @@
package org.baeldung.ex.mappingexception.cause2.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
public Foo() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
}
@@ -0,0 +1,31 @@
package org.baeldung.ex.mappingexception.cause3.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
public Foo() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
}
@@ -0,0 +1,31 @@
package org.baeldung.ex.mappingexception.cause4.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
public Foo() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
}
@@ -0,0 +1,76 @@
package org.baeldung.ex.mappingexception.spring;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
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.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.ex.mappingexception.cause1.persistence" })
public class Cause1PersistenceConfig {
@Autowired
private Environment env;
public Cause1PersistenceConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.ex.mappingexception.cause1.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
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 HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
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.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,78 @@
package org.baeldung.ex.mappingexception.spring;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
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.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.ex.mappingexception.cause2.persistence" })
public class Cause2PersistenceConfig {
@Autowired
private Environment env;
public Cause2PersistenceConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setHibernateProperties(hibernateProperties());
// sessionFactory.setPackagesToScan(new String[] { "org.baeldung.ex.mappingexception.cause2.persistence.model" });
// sessionFactory.setAnnotatedClasses(new Class[] { Foo.class });
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
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 HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
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.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,79 @@
package org.baeldung.ex.mappingexception.spring;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import org.baeldung.ex.mappingexception.cause3.persistence.model.Foo;
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.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.ex.mappingexception.cause2.persistence" })
public class Cause3PersistenceConfig {
@Autowired
private Environment env;
public Cause3PersistenceConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setHibernateProperties(hibernateProperties());
// sessionFactory.setPackagesToScan(new String[] { "org.baeldung.ex.mappingexception.cause2.persistence.model" });
sessionFactory.setAnnotatedClasses(new Class[] { Foo.class });
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
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 HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
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.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,76 @@
package org.baeldung.ex.nontransientexception.cause;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
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.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-derby.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class Cause1NonTransientConfig {
@Autowired
private Environment env;
public Cause1NonTransientConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
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 HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
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.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,75 @@
package org.baeldung.ex.nontransientexception.cause;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
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.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-derby.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class Cause4NonTransientConfig {
@Autowired
private Environment env;
public Cause4NonTransientConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
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 HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
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.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,75 @@
package org.baeldung.ex.nontransientexception.cause;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
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.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql-incorrect.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class Cause5NonTransientConfig {
@Autowired
private Environment env;
public Cause5NonTransientConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
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 HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
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.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Autowired
private BeanB dependency;
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause1;
public class BeanB {
//
}
@@ -0,0 +1,12 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanA {
@Autowired
private IBeanB dependency;
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause2;
import org.springframework.stereotype.Component;
@Component
public class BeanB1 implements IBeanB {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause2;
import org.springframework.stereotype.Component;
@Component
public class BeanB2 implements IBeanB {
//
}
@@ -0,0 +1,5 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause2;
public interface IBeanB {
//
}
@@ -0,0 +1,19 @@
package org.baeldung.ex.nosuchbeandefinitionexception.cause3;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class BeanA implements InitializingBean {
@Autowired
private ApplicationContext context;
@Override
public void afterPropertiesSet() {
context.getBean("someBeanName");
}
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.nosuchbeandefinitionexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.nosuchbeandefinitionexception.cause1")
public class Cause1ContextWithJavaConfig {
public Cause1ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.nosuchbeandefinitionexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.nosuchbeandefinitionexception.cause2")
public class Cause2ContextWithJavaConfig {
public Cause2ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.ex.nosuchbeandefinitionexception.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("org.baeldung.ex.nosuchbeandefinitionexception.cause3")
public class Cause3ContextWithJavaConfig {
public Cause3ContextWithJavaConfig() {
super();
}
// beans
}
@@ -0,0 +1,26 @@
package org.baeldung.persistence;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class Setup implements ApplicationListener<ContextRefreshedEvent> {
private boolean setupDone;
public Setup() {
super();
}
//
@Override
public final void onApplicationEvent(final ContextRefreshedEvent event) {
if (!setupDone) {
System.out.println();
setupDone = true;
}
}
}
@@ -0,0 +1,65 @@
package org.baeldung.persistence.common;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.base.Preconditions;
@SuppressWarnings("unchecked")
public abstract class AbstractHibernateDao<T extends Serializable> implements IOperations<T> {
private Class<T> clazz;
@Autowired
private SessionFactory sessionFactory;
// API
protected final void setClazz(final Class<T> clazzToSet) {
clazz = Preconditions.checkNotNull(clazzToSet);
}
@Override
public final T findOne(final long id) {
return (T) getCurrentSession().get(clazz, id);
}
@Override
public final List<T> findAll() {
return getCurrentSession().createQuery("from " + clazz.getName()).list();
}
@Override
public final void create(final T entity) {
Preconditions.checkNotNull(entity);
// getCurrentSession().persist(entity);
getCurrentSession().saveOrUpdate(entity);
}
@Override
public final T update(final T entity) {
Preconditions.checkNotNull(entity);
return (T) getCurrentSession().merge(entity);
}
@Override
public final void delete(final T entity) {
Preconditions.checkNotNull(entity);
getCurrentSession().delete(entity);
}
@Override
public final void deleteById(final long entityId) {
final T entity = findOne(entityId);
Preconditions.checkState(entity != null);
delete(entity);
}
protected final Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}
@@ -0,0 +1,44 @@
package org.baeldung.persistence.common;
import java.io.Serializable;
import java.util.List;
import org.baeldung.persistence.common.IOperations;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
@Override
public T findOne(final long id) {
return getDao().findOne(id);
}
@Override
public List<T> findAll() {
return getDao().findAll();
}
@Override
public void create(final T entity) {
getDao().create(entity);
}
@Override
public T update(final T entity) {
return getDao().update(entity);
}
@Override
public void delete(final T entity) {
getDao().delete(entity);
}
@Override
public void deleteById(final long entityId) {
getDao().deleteById(entityId);
}
protected abstract IOperations<T> getDao();
}
@@ -0,0 +1,20 @@
package org.baeldung.persistence.common;
import java.io.Serializable;
import java.util.List;
public interface IOperations<T extends Serializable> {
T findOne(final long id);
List<T> findAll();
void create(final T entity);
T update(final T entity);
void delete(final T entity);
void deleteById(final long entityId);
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Child;
public interface IChildDao extends IOperations<Child> {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Foo;
public interface IFooDao extends IOperations<Foo> {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.dao;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Parent;
public interface IParentDao extends IOperations<Parent> {
//
}
@@ -0,0 +1,24 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.common.AbstractHibernateDao;
import org.baeldung.persistence.dao.IChildDao;
import org.baeldung.persistence.model.Child;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class ChildDao extends AbstractHibernateDao<Child> implements IChildDao {
@Autowired
private SessionFactory sessionFactory;
public ChildDao() {
super();
setClazz(Child.class);
}
// API
}
@@ -0,0 +1,24 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.common.AbstractHibernateDao;
import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.model.Foo;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class FooDao extends AbstractHibernateDao<Foo> implements IFooDao {
@Autowired
private SessionFactory sessionFactory;
public FooDao() {
super();
setClazz(Foo.class);
}
// API
}
@@ -0,0 +1,24 @@
package org.baeldung.persistence.dao.impl;
import org.baeldung.persistence.common.AbstractHibernateDao;
import org.baeldung.persistence.dao.IParentDao;
import org.baeldung.persistence.model.Parent;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class ParentDao extends AbstractHibernateDao<Parent> implements IParentDao {
@Autowired
private SessionFactory sessionFactory;
public ParentDao() {
super();
setClazz(Parent.class);
}
// API
}
@@ -0,0 +1,51 @@
package org.baeldung.persistence.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Child implements Serializable {
@Id
@GeneratedValue
private long id;
@OneToOne(mappedBy = "child")
private Parent parent;
public Child() {
super();
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public Parent getParent() {
return parent;
}
public void setParent(final Parent parent) {
this.parent = parent;
}
//
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Child [id=").append(id).append("]");
return builder.toString();
}
}
@@ -0,0 +1,83 @@
package org.baeldung.persistence.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String name;
public Foo() {
super();
}
public Foo(final String name) {
super();
this.name = name;
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
//
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Foo other = (Foo) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
}
}
@@ -0,0 +1,60 @@
package org.baeldung.persistence.model;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Parent implements Serializable {
@Id
@GeneratedValue
private long id;
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH })
@JoinColumn(name = "child_fk")
private Child child;
public Parent() {
super();
}
public Parent(final Child child) {
super();
this.child = child;
}
// API
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
public Child getChild() {
return child;
}
public void setChild(final Child child) {
this.child = child;
}
//
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Parent [id=").append(id).append("]");
return builder.toString();
}
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Child;
public interface IChildService extends IOperations<Child> {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Foo;
public interface IFooService extends IOperations<Foo> {
//
}
@@ -0,0 +1,8 @@
package org.baeldung.persistence.service;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.model.Parent;
public interface IParentService extends IOperations<Parent> {
//
}
@@ -0,0 +1,28 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.common.AbstractService;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.dao.IChildDao;
import org.baeldung.persistence.model.Child;
import org.baeldung.persistence.service.IChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ChildService extends AbstractService<Child> implements IChildService {
@Autowired
private IChildDao dao;
public ChildService() {
super();
}
// API
@Override
protected IOperations<Child> getDao() {
return dao;
}
}
@@ -0,0 +1,28 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.common.AbstractService;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.dao.IFooDao;
import org.baeldung.persistence.model.Foo;
import org.baeldung.persistence.service.IFooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FooService extends AbstractService<Foo> implements IFooService {
@Autowired
private IFooDao dao;
public FooService() {
super();
}
// API
@Override
protected IOperations<Foo> getDao() {
return dao;
}
}
@@ -0,0 +1,28 @@
package org.baeldung.persistence.service.impl;
import org.baeldung.persistence.common.AbstractService;
import org.baeldung.persistence.common.IOperations;
import org.baeldung.persistence.dao.IParentDao;
import org.baeldung.persistence.model.Parent;
import org.baeldung.persistence.service.IParentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ParentService extends AbstractService<Parent> implements IParentService {
@Autowired
private IParentDao dao;
public ParentService() {
super();
}
// API
@Override
protected IOperations<Parent> getDao() {
return dao;
}
}
@@ -0,0 +1,30 @@
package org.baeldung.properties.core;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
public class ComponentInXmlUsingProperties implements InitializingBean {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
public ComponentInXmlUsingProperties(final String propertyValue) {
super();
System.out.println("Constructor Injection - Property Value resolted to: " + propertyValue);
}
//
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("in afterPropertiesSet via @Value: " + injectedProperty);
System.out.println("in afterPropertiesSet Environment: " + env.getProperty("key.something"));
}
}
@@ -0,0 +1,30 @@
package org.baeldung.properties.core;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class ComponentUsingProperties implements InitializingBean {
@Autowired
private Environment env;
@Value("${key.something}")
private String injectedProperty;
public ComponentUsingProperties() {
super();
}
//
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("in afterPropertiesSet via @Value: " + injectedProperty);
System.out.println("in afterPropertiesSet Environment: " + env.getProperty("key.something"));
}
}
@@ -0,0 +1,25 @@
package org.baeldung.properties.spring;
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.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan("org.baeldung.properties.core")
@PropertySource("classpath:foo.properties")
public class PropertiesWithJavaConfig {
public PropertiesWithJavaConfig() {
super();
}
// beans
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@@ -0,0 +1,16 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfigOther {
public PropertiesWithJavaConfigOther() {
super();
}
// beans
}
@@ -0,0 +1,16 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:configForProperties.xml")
@ComponentScan("org.baeldung.core")
public class PropertiesWithXmlConfig {
public PropertiesWithXmlConfig() {
super();
}
}
@@ -0,0 +1,16 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:configForPropertiesOne.xml")
@ComponentScan("org.baeldung.core")
public class PropertiesWithXmlConfigOne {
public PropertiesWithXmlConfigOne() {
super();
}
}
@@ -0,0 +1,14 @@
package org.baeldung.properties.spring;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:configForPropertiesTwo.xml")
public class PropertiesWithXmlConfigTwo {
public PropertiesWithXmlConfigTwo() {
super();
}
}
@@ -0,0 +1,15 @@
package org.baeldung.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@ComponentScan("org.baeldung.core")
public class CoreConfig extends WebMvcConfigurerAdapter {
public CoreConfig() {
super();
}
}
@@ -0,0 +1,41 @@
package org.baeldung.spring.config;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MainWebAppInitializer implements WebApplicationInitializer {
/**
* Register and configure all Servlet container components necessary to power the web application.
*/
@Override
public void onStartup(final ServletContext sc) throws ServletException {
System.out.println("MainWebAppInitializer.onStartup()");
// Create the 'root' Spring application context
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.scan("org.baeldung.spring.config");
// root.getEnvironment().setDefaultProfiles("embedded");
// Manages the lifecycle of the root application context
sc.addListener(new ContextLoaderListener(root));
// Handles requests into the application
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext()));
appServlet.setLoadOnStartup(1);
final Set<String> mappingConflicts = appServlet.addMapping("/");
if (!mappingConflicts.isEmpty()) {
throw new IllegalStateException("'appServlet' could not be mapped to '/' due " + "to an existing mapping. This is a known issue under Tomcat versions " + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
}
}
}
@@ -0,0 +1,39 @@
package org.baeldung.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@EnableWebMvc
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
public MvcConfig() {
super();
}
// API
@Override
public void addViewControllers(final ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/sample.html");
}
@Bean
public ViewResolver viewResolver() {
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
}
@@ -0,0 +1,76 @@
package org.baeldung.spring.config;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
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.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.google.common.base.Preconditions;
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "org.baeldung.persistence" })
public class PersistenceConfig {
@Autowired
private Environment env;
public PersistenceConfig() {
super();
}
@Bean
public LocalSessionFactoryBean sessionFactory() {
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
sessionFactory.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource restDataSource() {
final BasicDataSource dataSource = new BasicDataSource();
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 HibernateTransactionManager transactionManager() {
final HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties hibernateProperties() {
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.globally_quoted_identifiers", "true");
return hibernateProperties;
}
}
@@ -0,0 +1 @@
key.something2=val2
@@ -0,0 +1,11 @@
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="beanA" class="org.baeldung.ex.beancreationexception.cause4.BeanA" />
</beans>
@@ -0,0 +1,13 @@
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="beanA" class="org.baeldung.ex.beancreationexception.cause6.BeanA">
<property name="beanB" ref="beanB" />
</bean>
</beans>
@@ -0,0 +1,11 @@
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="beanZ" class="org.baeldung.ex.beancreationexception.cause7.BeanZ" />
</beans>
@@ -0,0 +1,11 @@
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean id="beanA" abstract="true" class="org.baeldung.ex.beancreationexception.cause9.BeanA" />
</beans>
@@ -0,0 +1,16 @@
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:property-placeholder location="classpath:foo.properties"/>
<bean id="componentInXmlUsingProperties" class="org.baeldung.properties.core.ComponentInXmlUsingProperties">
<constructor-arg value="${key.something}"/>
</bean>
</beans>
@@ -0,0 +1,16 @@
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:property-placeholder location="classpath:foo.properties" ignore-unresolvable="true" order="1"/>
<bean id="componentInXmlUsingProperties" class="org.baeldung.properties.core.ComponentInXmlUsingProperties">
<constructor-arg value="${key.something2}"/>
</bean>
</beans>
@@ -0,0 +1,12 @@
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:property-placeholder location="classpath:bar.properties" order="2"/>
</beans>
@@ -0,0 +1 @@
key.something=val
@@ -0,0 +1,7 @@
hibernate.connection.username=tutorialuser
hibernate.connection.password=tutorialmy5ql
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.connection.url=jdbc:mysql://localhost:3306/spring_hibernate4_exceptions?createDatabaseIfNotExist=true
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create

Some files were not shown because too many files have changed in this diff Show More