Reputation: 11
I bumped into a problem with my school project I was attempting to set up h2 database for. Everything ran fine I guess until I created a Repository interface for my database, which looks simply like this
package com.protonmail.jan.backend.repository;
import org.springframework.data.jpa.repository.JpaRepository;
public interface RecordRepository extends JpaRepository {
}
When I run the application without the repository code, the app at least starts, I'm pretty new to spring-boot, so I have no idea what's going on here, but I guess it's some dependencies or something needed to create beans missing on my pom.xml file? Please help, here is some of the error log I got:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recordRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
Caused by: java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
this is my entity
package com.protonmail.jan.backend.entity;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Entity
public class Record extends AbstractEntity {
@NotNull
@NotEmpty
private Date date;
@NotNull
@NotEmpty
private static Map<Integer, Room> rooms;
static {
rooms = new HashMap<Integer, Room>(){
{
put(1, new Room());
put(2, new Room());
put(3, new Room());
}
};
}
public Collection<Room> getAllRooms(){
return this.rooms.values();
}
}
this is Room, which is contained in Record
package com.protonmail.jan.backend.entity;
import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Map;
@Entity
public class Room extends AbstractEntity{
private String name = "pokoj";
private Boolean lit = false;
private long temp = 0;
public Room() {
}
public Boolean getLit() {
return lit;
}
public void setLit(Boolean lit) {
this.lit = lit;
}
public long getTemp() {
return temp;
}
public void setTemp(long temp) {
this.temp = temp;
}
}
this is AbstractEntity class
package com.protonmail.jan.backend.entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public abstract class AbstractEntity {
@Id
@GeneratedValue(strategy= GenerationType.SEQUENCE)
private Long id;
public Long getId() {
return id;
}
public boolean isPersisted() {
return id != null;
}
@Override
public int hashCode() {
if (getId() != null) {
return getId().hashCode();
}
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
AbstractEntity other = (AbstractEntity) obj;
if (getId() == null || other.getId() == null) {
return false;
}
return getId().equals(other.getId());
}
}
i also have used
public interface RecordRepository extends JpaRepository<Repository, Long> {}
Upvotes: 1
Views: 22238
Reputation: 27
Try this: In Repository java class file, adding @Entity annotation will remove this error.
Upvotes: 1
Reputation: 107
I have gone through your code. If your using Spring boot version 4 or greater than 4 javax.persistence.; is depricated . instead of this import "import jakarta.persistence"
If your using spring boot version less than 3 , check package structure . your Springboot SpringApplication.run() should be under "package com.protonmail.jan.backend".
hope this will fix your issue .
If you want to see how to use package hirarchy , you can wach this link : https://www.youtube.com/watch?v=ug53epaPFrA&ab_channel=CodeJava
Please note: I am not owner of this link video. I found helpful so shared it .
Upvotes: 0
Reputation: 1
I was stuck not being able to help spring create a bean for the custom interface that extended JpaRepository. Tried everything, finally the answer was that Springboot expects packages to be named a certain way so that all components can be autowired.
Follow any of the common examples for Spring boot JPA CRUD, like 1
Usually the package part of the code is clipped. There lies the trick. Spring boot expects the Application class, that is the one labelled @SpringbootApplication to be in package com.base.app, while the repository interface to be in com.base.app.repo. If you instead have com.base.repo, it cannot locate and create the bean!!
Upvotes: 0
Reputation: 1
You can solve this by adding the following to the main class where spring boot starts running:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EntityScan("com.mine.alien") // path of the entity model
@EnableJpaRepositories("com.mine.demo.control") // path of jpa repository
@SpringBootApplication
public class SpringJpah2Application {
public static void main(String[] args) {
SpringApplication.run(SpringJpah2Application.class, args);
}
}
Upvotes: -1
Reputation: 7
@EnableJpaRepositories
annotation on the configuration class
also so @Repository
Upvotes: 0
Reputation: 2610
Please specify the managed object and id types in interface declaration:
public interface RecordRepository extends JpaRepository<PersitentClass, ClassId> {
}
Where: PersitentClass - the type of your entity model ClassId - the type of 'id' field
For the class details, please refer to JpaRepository doc.
Upvotes: 5