BAEL-6346 - "Not a managed type" exception in Spring Data JPA
This commit is contained in:
parent
11084b012d
commit
60751b2a06
|
@ -14,6 +14,11 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jakarta.persistence</groupId>
|
||||
<artifactId>jakarta.persistence-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithjakartaannotation;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class EntityWithJakartaAnnotation {
|
||||
@Id
|
||||
private Long id;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithjakartaannotation;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EntityWithJakartaAnnotationApplication {
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithjakartaannotation;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface EntityWithJakartaAnnotationRepository extends JpaRepository<EntityWithJakartaAnnotation, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithoutannotation;
|
||||
|
||||
import javax.persistence.Id;
|
||||
|
||||
public class EntityWithoutAnnotation {
|
||||
@Id
|
||||
private Long id;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithoutannotation;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EntityWithoutAnnotationApplication {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithoutannotation;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface EntityWithoutAnnotationRepository
|
||||
extends JpaRepository<EntityWithoutAnnotation, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithoutannotationfixed;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class EntityWithoutAnnotationFixed {
|
||||
@Id
|
||||
private Long id;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithoutannotationfixed;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EntityWithoutAnnotationFixedApplication {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithoutannotationfixed;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface EntityWithoutAnnotationFixedRepository
|
||||
extends JpaRepository<EntityWithoutAnnotationFixed, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.app;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableJpaRepositories(basePackages =
|
||||
"com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.repository")
|
||||
public class WrongEntityScanApplication {
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class CorrectEntity {
|
||||
@Id
|
||||
private Long id;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.fixed.app;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableJpaRepositories(basePackages =
|
||||
"com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.repository")
|
||||
@EntityScan("com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.entity")
|
||||
public class WrongEntityScanFixedApplication {
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.entity.CorrectEntity;
|
||||
|
||||
public interface CorrectEntityRepository extends JpaRepository<CorrectEntity, Long> {
|
||||
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
server.port=0
|
||||
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=sa
|
||||
|
||||
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
|
||||
logging.level.com.baeldung.spring.data.persistence.search=debug
|
||||
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.springframework.boot.SpringApplication.run;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithoutannotation.EntityWithoutAnnotationApplication;
|
||||
import com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithoutannotationfixed.EntityWithoutAnnotationFixedApplication;
|
||||
import com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithoutannotationfixed.EntityWithoutAnnotationFixedRepository;
|
||||
import com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.entitywithjakartaannotation.EntityWithJakartaAnnotationApplication;
|
||||
import com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.app.WrongEntityScanApplication;
|
||||
import com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.fixed.app.WrongEntityScanFixedApplication;
|
||||
import com.baeldung.spring.notamanagedtypeexceptioninspringdatajpa.wrongentityscanapplication.repository.CorrectEntityRepository;
|
||||
|
||||
class NotManagedTypeExceptionIntegrationTest {
|
||||
@Test
|
||||
void givenEntityWithoutAnnotationApplicationWhenBootstrapThenExpectedExceptionThrown() {
|
||||
Exception exception = assertThrows(Exception.class,
|
||||
() -> run(EntityWithoutAnnotationApplication.class));
|
||||
|
||||
assertThat(exception)
|
||||
.getRootCause()
|
||||
.hasMessageContaining("Not a managed type");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenEntityWithoutAnnotationApplicationFixedWhenBootstrapThenRepositoryBeanShouldBePresentInContext() {
|
||||
ConfigurableApplicationContext context = run(EntityWithoutAnnotationFixedApplication.class);
|
||||
EntityWithoutAnnotationFixedRepository repository = context
|
||||
.getBean(EntityWithoutAnnotationFixedRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenEntityWithJakartaAnnotationApplicationWhenBootstrapThenExpectedExceptionThrown() {
|
||||
Exception exception = assertThrows(Exception.class,
|
||||
() -> run(EntityWithJakartaAnnotationApplication.class));
|
||||
|
||||
assertThat(exception)
|
||||
.getRootCause()
|
||||
.hasMessageContaining("Not a managed type");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenWrongEntityScanApplicationWhenBootstrapThenExpectedExceptionThrown() {
|
||||
Exception exception = assertThrows(Exception.class,
|
||||
() -> run(WrongEntityScanApplication.class));
|
||||
|
||||
assertThat(exception)
|
||||
.getRootCause()
|
||||
.hasMessageContaining("Not a managed type");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenWrongEntityScanApplicationFixedWhenBootstrapThenRepositoryBeanShouldBePresentInContext() {
|
||||
ConfigurableApplicationContext context = run(WrongEntityScanFixedApplication.class);
|
||||
CorrectEntityRepository repository = context
|
||||
.getBean(CorrectEntityRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue