Skip to content

Commit b18e9cf

Browse files
committed
Source code for 'No Hibernate Session bound to thread'
1 parent b53e462 commit b18e9cf

10 files changed

Lines changed: 291 additions & 155 deletions

File tree

‎spring-boot/pom.xml‎

Lines changed: 153 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,156 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.baeldung</groupId>
5-
<artifactId>spring-boot</artifactId>
6-
<version>0.0.1-SNAPSHOT</version>
7-
<packaging>war</packaging>
8-
<name>Spring Boot Actuator</name>
9-
<description>This is simple boot application for Spring boot actuator test</description>
10-
11-
<!-- Inherit defaults from Spring Boot -->
12-
<parent>
13-
<groupId>org.springframework.boot</groupId>
14-
<artifactId>spring-boot-starter-parent</artifactId>
15-
<version>1.4.0.RC1</version>
16-
<relativePath /> <!-- lookup parent from repository -->
17-
</parent>
18-
19-
<properties>
20-
<!-- The main class to start by executing java -jar -->
21-
<start-class>org.baeldung.boot.DemoApplication</start-class>
22-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23-
<java.version>1.8</java.version>
24-
<!-- <spring.version>4.3.0.RELEASE</spring.version> -->
25-
</properties>
26-
27-
<dependencies>
28-
<dependency>
29-
<groupId>org.springframework.boot</groupId>
30-
<artifactId>spring-boot-starter-web</artifactId>
31-
</dependency>
32-
33-
<dependency>
34-
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-starter-data-jpa</artifactId>
36-
</dependency>
37-
38-
<dependency>
39-
<groupId>org.springframework.boot</groupId>
40-
<artifactId>spring-boot-starter-actuator</artifactId>
41-
</dependency>
42-
43-
<dependency>
44-
<groupId>org.springframework.boot</groupId>
45-
<artifactId>spring-boot-starter-security</artifactId>
46-
</dependency>
47-
48-
<dependency>
49-
<groupId>io.dropwizard.metrics</groupId>
50-
<artifactId>metrics-core</artifactId>
51-
</dependency>
52-
53-
<dependency>
54-
<groupId>com.h2database</groupId>
55-
<artifactId>h2</artifactId>
56-
</dependency>
57-
58-
<dependency>
59-
<groupId>org.springframework.boot</groupId>
60-
<artifactId>spring-boot-starter-test</artifactId>
61-
<scope>test</scope>
62-
</dependency>
63-
64-
<dependency>
65-
<groupId>org.springframework.boot</groupId>
66-
<artifactId>spring-boot-starter</artifactId>
67-
</dependency>
68-
<dependency>
69-
<groupId>com.jayway.jsonpath</groupId>
70-
<artifactId>json-path</artifactId>
71-
<scope>test</scope>
72-
</dependency>
73-
<dependency>
74-
<groupId>org.springframework.boot</groupId>
75-
<artifactId>spring-boot-starter-mail</artifactId>
76-
</dependency>
77-
<dependency>
78-
<groupId>org.subethamail</groupId>
79-
<artifactId>subethasmtp</artifactId>
80-
<version>3.1.7</version>
81-
<scope>test</scope>
82-
</dependency>
83-
</dependencies>
84-
85-
<build>
86-
<finalName>spring-boot</finalName>
87-
<resources>
88-
<resource>
89-
<directory>src/main/resources</directory>
90-
<filtering>true</filtering>
91-
</resource>
92-
</resources>
93-
94-
<plugins>
95-
96-
<plugin>
97-
<groupId>org.springframework.boot</groupId>
98-
<artifactId>spring-boot-maven-plugin</artifactId>
99-
</plugin>
100-
101-
<plugin>
102-
<groupId>org.apache.maven.plugins</groupId>
103-
<artifactId>maven-compiler-plugin</artifactId>
104-
<configuration>
105-
<source>1.8</source>
106-
<target>1.8</target>
107-
</configuration>
108-
</plugin>
109-
110-
<plugin>
111-
<groupId>org.apache.maven.plugins</groupId>
112-
<artifactId>maven-war-plugin</artifactId>
113-
</plugin>
114-
115-
</plugins>
116-
117-
</build>
118-
119-
<repositories>
120-
<repository>
121-
<id>spring-snapshots</id>
122-
<name>Spring Snapshots</name>
123-
<url>https://repo.spring.io/snapshot</url>
124-
<snapshots>
125-
<enabled>true</enabled>
126-
</snapshots>
127-
</repository>
128-
<repository>
129-
<id>spring-milestones</id>
130-
<name>Spring Milestones</name>
131-
<url>https://repo.spring.io/milestone</url>
132-
<snapshots>
133-
<enabled>false</enabled>
134-
</snapshots>
135-
</repository>
136-
</repositories>
137-
<pluginRepositories>
138-
<pluginRepository>
139-
<id>spring-snapshots</id>
140-
<name>Spring Snapshots</name>
141-
<url>https://repo.spring.io/snapshot</url>
142-
<snapshots>
143-
<enabled>true</enabled>
144-
</snapshots>
145-
</pluginRepository>
146-
<pluginRepository>
147-
<id>spring-milestones</id>
148-
<name>Spring Milestones</name>
149-
<url>https://repo.spring.io/milestone</url>
150-
<snapshots>
151-
<enabled>false</enabled>
152-
</snapshots>
153-
</pluginRepository>
154-
</pluginRepositories>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-boot</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
<name>Spring Boot Actuator</name>
9+
<description>This is simple boot application for Spring boot actuator test</description>
10+
11+
<!-- Inherit defaults from Spring Boot -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.4.0.RC1</version>
16+
<relativePath /> <!-- lookup parent from repository -->
17+
</parent>
18+
19+
<properties>
20+
<!-- The main class to start by executing java -jar -->
21+
<start-class>org.baeldung.boot.DemoApplication</start-class>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
<spring.version>4.3.1.RELEASE</spring.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-data-jpa</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-actuator</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-security</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>io.dropwizard.metrics</groupId>
50+
<artifactId>metrics-core</artifactId>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>com.h2database</groupId>
55+
<artifactId>h2</artifactId>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-starter-test</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter</artifactId>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.jayway.jsonpath</groupId>
70+
<artifactId>json-path</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-starter-mail</artifactId>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.subethamail</groupId>
79+
<artifactId>subethasmtp</artifactId>
80+
<version>3.1.7</version>
81+
<scope>test</scope>
82+
</dependency>
83+
</dependencies>
84+
85+
<build>
86+
<finalName>spring-boot</finalName>
87+
<resources>
88+
<resource>
89+
<directory>src/main/resources</directory>
90+
<filtering>true</filtering>
91+
</resource>
92+
</resources>
93+
94+
<plugins>
95+
96+
<plugin>
97+
<groupId>org.springframework.boot</groupId>
98+
<artifactId>spring-boot-maven-plugin</artifactId>
99+
</plugin>
100+
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-compiler-plugin</artifactId>
104+
<configuration>
105+
<source>1.8</source>
106+
<target>1.8</target>
107+
</configuration>
108+
</plugin>
109+
110+
<plugin>
111+
<groupId>org.apache.maven.plugins</groupId>
112+
<artifactId>maven-war-plugin</artifactId>
113+
</plugin>
114+
115+
</plugins>
116+
117+
</build>
118+
119+
<repositories>
120+
<repository>
121+
<id>spring-snapshots</id>
122+
<name>Spring Snapshots</name>
123+
<url>https://repo.spring.io/snapshot</url>
124+
<snapshots>
125+
<enabled>true</enabled>
126+
</snapshots>
127+
</repository>
128+
<repository>
129+
<id>spring-milestones</id>
130+
<name>Spring Milestones</name>
131+
<url>https://repo.spring.io/milestone</url>
132+
<snapshots>
133+
<enabled>false</enabled>
134+
</snapshots>
135+
</repository>
136+
</repositories>
137+
<pluginRepositories>
138+
<pluginRepository>
139+
<id>spring-snapshots</id>
140+
<name>Spring Snapshots</name>
141+
<url>https://repo.spring.io/snapshot</url>
142+
<snapshots>
143+
<enabled>true</enabled>
144+
</snapshots>
145+
</pluginRepository>
146+
<pluginRepository>
147+
<id>spring-milestones</id>
148+
<name>Spring Milestones</name>
149+
<url>https://repo.spring.io/milestone</url>
150+
<snapshots>
151+
<enabled>false</enabled>
152+
</snapshots>
153+
</pluginRepository>
154+
</pluginRepositories>
155155

156156
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.session.exception;
2+
3+
import org.baeldung.boot.model.Foo;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.autoconfigure.domain.EntityScan;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
9+
10+
@EntityScan(basePackageClasses = Foo.class)
11+
@SpringBootApplication
12+
public class Application {
13+
public static void main(String[] args) {
14+
System.setProperty("spring.config.name", "exception");
15+
System.setProperty("spring.profiles.active", "exception");
16+
SpringApplication.run(Application.class, args);
17+
}
18+
19+
@Bean
20+
public HibernateJpaSessionFactoryBean sessionFactory() {
21+
return new HibernateJpaSessionFactoryBean();
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.session.exception.repository;
2+
3+
import org.baeldung.boot.model.Foo;
4+
5+
public interface FooRepository {
6+
7+
public void save(Foo foo);
8+
9+
public Foo get(Integer id);
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.baeldung.session.exception.repository;
2+
3+
import org.baeldung.boot.model.Foo;
4+
import org.hibernate.SessionFactory;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.context.annotation.Profile;
7+
import org.springframework.stereotype.Repository;
8+
9+
@Profile("exception")
10+
@Repository
11+
public class FooRepositoryImpl implements FooRepository {
12+
@Autowired
13+
private SessionFactory sessionFactory;
14+
15+
@Override
16+
public void save(Foo foo) {
17+
sessionFactory.getCurrentSession().saveOrUpdate(foo);
18+
}
19+
20+
@Override
21+
public Foo get(Integer id) {
22+
return (Foo) sessionFactory.getCurrentSession().get(Foo.class, id);
23+
}
24+
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung.boot;
2+
3+
import org.baeldung.session.exception.Application;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.TestPropertySource;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
10+
@RunWith(SpringJUnit4ClassRunner.class)
11+
@SpringBootTest(classes = Application.class)
12+
@TestPropertySource("classpath:exception.properties")
13+
public class ApplicationTests {
14+
@Test
15+
public void contextLoads() {
16+
}
17+
}

0 commit comments

Comments
 (0)