I’m working on a Maven multi-module project and encountered an issue with Spring Boot. My project structure includes Common
, Parent
, Management
, and Client
modules. Specifically, I’m having trouble with the User
entity not being recognized as a managed type by Hibernate. The error I’m seeing is:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [/path/to/UserController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'userService' defined in file [/path/to/UserService.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'userRepository' defined in com.gigantic.admin.Repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.gigantic.entity.User
My project structure looks like this:
com.gigantic
├── Gigantic-Common
│ └── src/main/java/com/gigantic/entity
│ └── User.java
├── Gigantic-Web-Parent
│ └── pom.xml
├── Gigantic-management
│ └── src/main/java/com/gigantic/admin
│ ├── Controller
│ │ └── UserController.java
│ ├── Service
│ │ └── UserService.java
│ └── Repository
│ └── UserRepository.java
│ └── src/main/java/com/gigantic/management
│ └── Application.java
├── client
│ └── ...
The User
entity is located in the common
module:
package com.gigantic.entity; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class User { @Id private Long id; private String username; private String password; // Getters and setters }
My main application class in the management
module is set up to scan for entities, repositories, and components:
@Entity
@Table(name = "users")
public class User extends IdBasedEntity {
//Attributes
@Column(length = 128, nullable = false, unique = true)
private String email;
......
The UserRepository
is defined as follows:
package com.gigantic.admin.Repository;
import com.gigantic.entity.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}
....
The UserService
is defined as follows:
@Service
@Transactional
@Component
public class UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
@Autowired
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
.....
The UserController
is defined as follows:
@RestController
@RequestMapping("/user")
public class UserController {
private final UserService userService;
private final RoleService roleService;
public UserController(UserService userService, RoleService roleService) {
this.userService = userService;
this.roleService = roleService;
.....
In my pom.xml
for the management
module, I have added the dependency to the common
module:
<dependencies>
<dependency>
<groupId>com.gigantic</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</dependency>
<!-- Other dependencies like Spring Boot, JPA, etc. -->
</dependencies>
Despite setting up the project this way, I am still encountering the error that User
is not a managed type. I have verified that the User
entity is annotated with @Entity
and that the package scanning configuration includes the com.gigantic.entity
package. I have also ensured that the management
module depends on the common
module in the pom.xml
.
My Repo Link : https://github.com/6531503042/Gigantic-Mall
What could be causing Hibernate to not recognize the User
entity as a managed type? How can I resolve this issue?
BENGIGIGIGI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.