Search code examples
javaspring-bootjpah2autocreate

Spring Boot and JPA with a H2 Database, Not a managed type Error for my entity


im struggling since 3 days with the following problem. Im about to do a project for my class in university using Spring Boot, JPA and the h2 database for an event-website.

The only problem now is that i cant seem to get rid of this error to be able to autocreate my tables...

here an overview of my files:

NewEvent.java (its the entity)

package com.projekt.projekt;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "Event", schema="EVENT")
public class NewEvent {

u/Id
u/GeneratedValue(strategy = GenerationType.IDENTITY)
public Long eventID;
u/Column(name = "id")
private String cid;
u/Column(name = "title")
private String ctitle;
u/Column(name = "location")
private String clocation;
u/Column(name = "email")
private String cemail;
u/Column(name = "phone")
private String cphone;
u/Column(name = "date")
private String cdate;
u/Column(name = "time")
private String ctime;
u/Column(name = "description")
private String cdescription;
u/Column(name = "test")
private String test;

private String title;
private String location;
private String email;
private String phone;
private java.sql.Date date;
private java.sql.Time time;
private String description;

public Long getEventID() {
return eventID;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public java.sql.Date getDate() {
return date;
}
public void setDate(java.sql.Date date) {
this.date = date;
}
public java.sql.Time getTime() {
return time;
}
public void setTime(java.sql.Time time) {
this.time = time;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}

}

NewEventRepository.java (my repository):

package com.projekt.projekt;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

u/Repository
public interface NewEventRepository extends JpaRepository<NewEvent, Long> {
// Add custom query methods if needed
}

ProjektApplication.java(Application File):

package com.projekt.projekt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
u/SpringBootApplication
u/EnableJpaRepositories("com.projekt.*")
u/ComponentScan(basePackageClasses=controller.class)
u/EntityScan("com.projekt.*")
u/EnableAutoConfiguration
u/EnableConfigurationProperties
public class ProjektApplication {
public static void main(String[] args) {
SpringApplication.run(ProjektApplication.class, args);
}
}

My dependencies(pom.xml):

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.projekt</groupId>
<artifactId>projekt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>projekt</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>2.2.3</version>
</dependency> -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

And my application properties:

spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.defer-datasource-initialization=true
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:file:/data/demo
spring.h2.console.path=/h2-console
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=true
spring.jpa.hibernate.ddl-auto=update
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.tool.hbm2ddl=DEBUG
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true

now here is my file structure:

Filestructure

Spring Boot JPA not a managed type error keeps me from going on in my project you can basically ignore the php for now since its not gonna be used and wasnt deleted since my group made it.

i would be very happy if anyone could help me getting rid of this error "not managed type" somehow, i feel like if tried almost every tutorial and ressources i have found.. i would be very happy if anyone could help me getting rid of this error "not managed type" somehow, i feel like if tried almost every tutorial and ressources i have found..

I was trying refractoring, changing imports, setting it all up new from scratch, following tutorials, giving it new names, triple checking packages. Here is an example error message i get now:

Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.projekt.projekt.NewEvent

i didnt want to put my whole message in since my complete filestructure is in there


Solution

  • Think this is similar to this question here - How to fix UnsatisfiedDependencyException: Error creating bean with name 'employeeController'?

    Try using Jakarta instead.