Skip to content

Commit 979f4ff

Browse files
Merge pull request #16410 from sIvanovKonstantyn/master
BAEL-6346 - "Not a managed type" exception in Spring Data JPA
2 parents f2e0668 + c8f82ab commit 979f4ff

16 files changed

+190
-1
lines changed

persistence-modules/spring-data-jpa-repo-4/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
</parent>
1515

1616
<dependencies>
17+
<dependency>
18+
<groupId>jakarta.persistence</groupId>
19+
<artifactId>jakarta.persistence-api</artifactId>
20+
<version>${jakarta.persistence-api.version}</version>
21+
</dependency>
1722
<dependency>
1823
<groupId>org.springframework.boot</groupId>
1924
<artifactId>spring-boot-starter-web</artifactId>
@@ -142,6 +147,7 @@
142147
<postgresql.version>42.7.1</postgresql.version>
143148
<db.util.version>1.0.7</db.util.version>
144149
<hypersistence-utils.version>3.7.0</hypersistence-utils.version>
150+
<jakarta.persistence-api.version>3.1.0</jakarta.persistence-api.version>
145151
</properties>
146152

147153
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.spring.notamanagedtype.jakartaannotation;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.Id;
5+
6+
@Entity
7+
public class EntityWithJakartaAnnotation {
8+
@Id
9+
private Long id;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.notamanagedtype.jakartaannotation;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
@SpringBootApplication
6+
public class EntityWithJakartaAnnotationApplication {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.spring.notamanagedtype.jakartaannotation;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface EntityWithJakartaAnnotationRepository extends JpaRepository<EntityWithJakartaAnnotation, Long> {
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.notamanagedtype.missedannotation;
2+
3+
import javax.persistence.Id;
4+
5+
public class EntityWithoutAnnotation {
6+
@Id
7+
private Long id;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.notamanagedtype.missedannotation;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
@SpringBootApplication
6+
public class EntityWithoutAnnotationApplication {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.notamanagedtype.missedannotation;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface EntityWithoutAnnotationRepository
6+
extends JpaRepository<EntityWithoutAnnotation, Long> {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.spring.notamanagedtype.missedannotationfixed;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class EntityWithoutAnnotationFixed {
8+
@Id
9+
private Long id;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.notamanagedtype.missedannotationfixed;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
@SpringBootApplication
6+
public class EntityWithoutAnnotationFixedApplication {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.spring.notamanagedtype.missedannotationfixed;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface EntityWithoutAnnotationFixedRepository
6+
extends JpaRepository<EntityWithoutAnnotationFixed, Long> {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.spring.notamanagedtype.missedentityscan.app;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
5+
6+
@SpringBootApplication
7+
@EnableJpaRepositories(basePackages =
8+
"com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.repository")
9+
public class WrongEntityScanApplication {
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.spring.notamanagedtype.missedentityscan.entity;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class CorrectEntity {
8+
@Id
9+
private Long id;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.spring.notamanagedtype.missedentityscan.fixed.app;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.boot.autoconfigure.domain.EntityScan;
5+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
6+
7+
@SpringBootApplication
8+
@EnableJpaRepositories(basePackages =
9+
"com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.repository")
10+
@EntityScan("com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.entity")
11+
public class WrongEntityScanFixedApplication {
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.spring.notamanagedtype.missedentityscan.repository;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
import com.baeldung.spring.notamanagedtype.missedentityscan.entity.CorrectEntity;
6+
7+
public interface CorrectEntityRepository extends JpaRepository<CorrectEntity, Long> {
8+
9+
}

persistence-modules/spring-data-jpa-repo-4/src/main/resources/application.properties

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
22
spring.datasource.username=sa
33
spring.datasource.password=sa
4-
54
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
65
logging.level.com.baeldung.spring.data.persistence.search=debug
76

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.spring.notamanagedtype;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.springframework.boot.SpringApplication.run;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.context.ConfigurableApplicationContext;
9+
10+
import com.baeldung.spring.notamanagedtype.missedannotation.EntityWithoutAnnotationApplication;
11+
import com.baeldung.spring.notamanagedtype.missedannotationfixed.EntityWithoutAnnotationFixedApplication;
12+
import com.baeldung.spring.notamanagedtype.missedannotationfixed.EntityWithoutAnnotationFixedRepository;
13+
import com.baeldung.spring.notamanagedtype.jakartaannotation.EntityWithJakartaAnnotationApplication;
14+
import com.baeldung.spring.notamanagedtype.missedentityscan.app.WrongEntityScanApplication;
15+
import com.baeldung.spring.notamanagedtype.missedentityscan.fixed.app.WrongEntityScanFixedApplication;
16+
import com.baeldung.spring.notamanagedtype.missedentityscan.repository.CorrectEntityRepository;
17+
18+
class NotManagedTypeExceptionIntegrationTest {
19+
@Test
20+
void givenEntityWithoutAnnotationApplication_whenBootstrap_thenExpectedExceptionThrown() {
21+
Exception exception = assertThrows(Exception.class,
22+
() -> run(EntityWithoutAnnotationApplication.class));
23+
24+
assertThat(exception)
25+
.getRootCause()
26+
.hasMessageContaining("Not a managed type");
27+
}
28+
29+
@Test
30+
void givenEntityWithoutAnnotationApplicationFixed_whenBootstrap_thenRepositoryBeanShouldBePresentInContext() {
31+
ConfigurableApplicationContext context = run(EntityWithoutAnnotationFixedApplication.class);
32+
EntityWithoutAnnotationFixedRepository repository = context
33+
.getBean(EntityWithoutAnnotationFixedRepository.class);
34+
35+
assertThat(repository).isNotNull();
36+
}
37+
38+
@Test
39+
void givenEntityWithJakartaAnnotationApplication_whenBootstrap_thenExpectedExceptionThrown() {
40+
Exception exception = assertThrows(Exception.class,
41+
() -> run(EntityWithJakartaAnnotationApplication.class));
42+
43+
assertThat(exception)
44+
.getRootCause()
45+
.hasMessageContaining("Not a managed type");
46+
}
47+
48+
@Test
49+
void givenWrongEntityScanApplication_whenBootstrap_thenExpectedExceptionThrown() {
50+
Exception exception = assertThrows(Exception.class,
51+
() -> run(WrongEntityScanApplication.class));
52+
53+
assertThat(exception)
54+
.getRootCause()
55+
.hasMessageContaining("Not a managed type");
56+
}
57+
58+
@Test
59+
void givenWrongEntityScanApplicationFixed_whenBootstrap_thenRepositoryBeanShouldBePresentInContext() {
60+
ConfigurableApplicationContext context = run(WrongEntityScanFixedApplication.class);
61+
CorrectEntityRepository repository = context
62+
.getBean(CorrectEntityRepository.class);
63+
64+
assertThat(repository).isNotNull();
65+
}
66+
}

0 commit comments

Comments
 (0)