Merge pull request #16410 from sIvanovKonstantyn/master
BAEL-6346 - "Not a managed type" exception in Spring Data JPA
This commit is contained in:
commit
979f4ffb2c
|
@ -14,6 +14,11 @@
|
|||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>jakarta.persistence</groupId>
|
||||
<artifactId>jakarta.persistence-api</artifactId>
|
||||
<version>${jakarta.persistence-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
@ -142,6 +147,7 @@
|
|||
<postgresql.version>42.7.1</postgresql.version>
|
||||
<db.util.version>1.0.7</db.util.version>
|
||||
<hypersistence-utils.version>3.7.0</hypersistence-utils.version>
|
||||
<jakarta.persistence-api.version>3.1.0</jakarta.persistence-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.spring.notamanagedtype.jakartaannotation;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class EntityWithJakartaAnnotation {
|
||||
@Id
|
||||
private Long id;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtype.jakartaannotation;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EntityWithJakartaAnnotationApplication {
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.spring.notamanagedtype.jakartaannotation;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface EntityWithJakartaAnnotationRepository extends JpaRepository<EntityWithJakartaAnnotation, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtype.missedannotation;
|
||||
|
||||
import javax.persistence.Id;
|
||||
|
||||
public class EntityWithoutAnnotation {
|
||||
@Id
|
||||
private Long id;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtype.missedannotation;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EntityWithoutAnnotationApplication {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtype.missedannotation;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface EntityWithoutAnnotationRepository
|
||||
extends JpaRepository<EntityWithoutAnnotation, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.spring.notamanagedtype.missedannotationfixed;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class EntityWithoutAnnotationFixed {
|
||||
@Id
|
||||
private Long id;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtype.missedannotationfixed;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EntityWithoutAnnotationFixedApplication {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.spring.notamanagedtype.missedannotationfixed;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface EntityWithoutAnnotationFixedRepository
|
||||
extends JpaRepository<EntityWithoutAnnotationFixed, Long> {
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.spring.notamanagedtype.missedentityscan.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.notamanagedtype.missedentityscan.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.notamanagedtype.missedentityscan.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.notamanagedtype.missedentityscan.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.baeldung.spring.notamanagedtype.missedentityscan.entity.CorrectEntity;
|
||||
|
||||
public interface CorrectEntityRepository extends JpaRepository<CorrectEntity, Long> {
|
||||
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
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.notamanagedtype;
|
||||
|
||||
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.notamanagedtype.missedannotation.EntityWithoutAnnotationApplication;
|
||||
import com.baeldung.spring.notamanagedtype.missedannotationfixed.EntityWithoutAnnotationFixedApplication;
|
||||
import com.baeldung.spring.notamanagedtype.missedannotationfixed.EntityWithoutAnnotationFixedRepository;
|
||||
import com.baeldung.spring.notamanagedtype.jakartaannotation.EntityWithJakartaAnnotationApplication;
|
||||
import com.baeldung.spring.notamanagedtype.missedentityscan.app.WrongEntityScanApplication;
|
||||
import com.baeldung.spring.notamanagedtype.missedentityscan.fixed.app.WrongEntityScanFixedApplication;
|
||||
import com.baeldung.spring.notamanagedtype.missedentityscan.repository.CorrectEntityRepository;
|
||||
|
||||
class NotManagedTypeExceptionIntegrationTest {
|
||||
@Test
|
||||
void givenEntityWithoutAnnotationApplication_whenBootstrap_thenExpectedExceptionThrown() {
|
||||
Exception exception = assertThrows(Exception.class,
|
||||
() -> run(EntityWithoutAnnotationApplication.class));
|
||||
|
||||
assertThat(exception)
|
||||
.getRootCause()
|
||||
.hasMessageContaining("Not a managed type");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenEntityWithoutAnnotationApplicationFixed_whenBootstrap_thenRepositoryBeanShouldBePresentInContext() {
|
||||
ConfigurableApplicationContext context = run(EntityWithoutAnnotationFixedApplication.class);
|
||||
EntityWithoutAnnotationFixedRepository repository = context
|
||||
.getBean(EntityWithoutAnnotationFixedRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenEntityWithJakartaAnnotationApplication_whenBootstrap_thenExpectedExceptionThrown() {
|
||||
Exception exception = assertThrows(Exception.class,
|
||||
() -> run(EntityWithJakartaAnnotationApplication.class));
|
||||
|
||||
assertThat(exception)
|
||||
.getRootCause()
|
||||
.hasMessageContaining("Not a managed type");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenWrongEntityScanApplication_whenBootstrap_thenExpectedExceptionThrown() {
|
||||
Exception exception = assertThrows(Exception.class,
|
||||
() -> run(WrongEntityScanApplication.class));
|
||||
|
||||
assertThat(exception)
|
||||
.getRootCause()
|
||||
.hasMessageContaining("Not a managed type");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenWrongEntityScanApplicationFixed_whenBootstrap_thenRepositoryBeanShouldBePresentInContext() {
|
||||
ConfigurableApplicationContext context = run(WrongEntityScanFixedApplication.class);
|
||||
CorrectEntityRepository repository = context
|
||||
.getBean(CorrectEntityRepository.class);
|
||||
|
||||
assertThat(repository).isNotNull();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue