I am using Spring Boot 3.0. I think that I have a configuration error wrt my package structure annotations, but can't quite figure it out. There are similar questions on here, but I've tried so many variations of the solutions and still scratching my head. Thanks in advance for the help!
The error I am seeing:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'groupController' defined in URL [jar:file:/workspace/modernmail-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/modernmail/web/GroupController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'groupRepository' defined in com.modernmail.model.GroupRepository defined in @EnableJpaRepositories declared on ModernMailApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.modernmail.model.Group
My application:
package com.modernmail;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@ComponentScan(basePackages = {"com.modernmail", "com.modernmail.model"})
@EntityScan(basePackages = {"com.modernmail", "com.modernmail.model"})
@EnableJpaRepositories("com.modernmail.model")
@SpringBootApplication
public class ModernMailApplication {
public static void main(String[] args) {
SpringApplication.run(ModernMailApplication.class, args);
}
}
My initializer Component:
package com.modernmail;
import com.modernmail.model.Event;
import com.modernmail.model.Group;
import com.modernmail.model.GroupRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.Collections;
import java.util.stream.Stream;
@Component
class Initializer implements CommandLineRunner {
private final GroupRepository repository;
public Initializer(GroupRepository repository) {
this.repository = repository;
}
...
My repository:
package com.modernmail.model;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface GroupRepository extends JpaRepository<Group, Long> {
Group findByName(String name);
}
My entity:
package com.modernmail.model;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import jakarta.persistence.*;
import java.util.Set;
@Data
@NoArgsConstructor
@RequiredArgsConstructor
@Entity
@Table(name = "user_group")
public class Group {
@Id
@GeneratedValue
private Long id;
@NonNull
private String name;
private String address;
private String city;
private String stateOrProvince;
private String country;
private String postalCode;
@ManyToOne(cascade=CascadeType.PERSIST)
private User user;
@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
private Set<Event> events;
}
My controller:
package com.modernmail.web;
import com.modernmail.model.Group;
import com.modernmail.model.GroupRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Optional;
@RestController
@RequestMapping("/api")
class GroupController {
private final Logger log = LoggerFactory.getLogger(GroupController.class);
private GroupRepository groupRepository;
public GroupController(GroupRepository groupRepository) {
this.groupRepository = groupRepository;
}
@GetMapping("/groups")
Collection<Group> groups() {
return groupRepository.findAll();
}
@GetMapping("/group/{id}")
ResponseEntity<?> getGroup(@PathVariable Long id) {
Optional<Group> group = groupRepository.findById(id);
return group.map(response -> ResponseEntity.ok().body(response))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
@PostMapping("/group")
ResponseEntity<Group> createGroup(@Valid @RequestBody Group group) throws URISyntaxException {
log.info("Request to create group: {}", group);
Group result = groupRepository.save(group);
return ResponseEntity.created(new URI("/api/group/" result.getId()))
.body(result);
}
@PutMapping("/group/{id}")
ResponseEntity<Group> updateGroup(@Valid @RequestBody Group group) {
log.info("Request to update group: {}", group);
Group result = groupRepository.save(group);
return ResponseEntity.ok().body(result);
}
@DeleteMapping("/group/{id}")
public ResponseEntity<?> deleteGroup(@PathVariable Long id) {
log.info("Request to delete group: {}", id);
groupRepository.deleteById(id);
return ResponseEntity.ok().build();
}
}
My pom.xml (commented out items are things I've tried that also didn't work):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.hibernate</groupId>-->
<!-- <artifactId>hibernate-core</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.hibernate.javax.persistence</groupId>-->
<!-- <artifactId>hibernate-jpa-2.1-api</artifactId>-->
<!-- <version>1.0.0.Final</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>javax.xml.bind</groupId>-->
<!-- <artifactId>jaxb-api</artifactId>-->
<!-- <version>2.3.0</version>-->
<!-- </dependency>-->
CodePudding user response:
You need to remove @RequiredArgsConstructor from entity, AllArgsConsructor NoArgsConstructor are enough.
Delete jakarta.persistence-api, hibernate-core, hibernate-entitymanager. Code doesn't contain hibernate imports, jakarta, it seems, included into boot by default.
CodePudding user response:
One thing i really don't understand that why are you initizing you bean using construtor? You should autowire your beans and let them manage by spring container. Remove below code from your controller for initializing the repository and use autowire it like beolw.
Remove it -
private GroupRepository groupRepository;
public GroupController(GroupRepository groupRepository) {
this.groupRepository = groupRepository;
}
And Add it -
@Autowired private GroupRepository groupRepository;