I’m trying to develop a simple API using Spring Boot v3.2.5 and Jakarta Persistence (which replaces javax.persistence).
I have a UserEntity
with its own ID and a CandidateEntity
with the ID mapped as OneToOne to the UserEntity
so in the DB table of the CandidateEntity
, its PK is also a FK referencing UserEntity
:
- BaseEntity.class:
@Getter
@Setter
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@JsonSerialize
@CreationTimestamp
@Column(name = "created_at", updatable = false)
public ZonedDateTime createdDate;
@JsonSerialize
@UpdateTimestamp
@Column(name = "updated_at")
public ZonedDateTime updatedDate;
}
- UserEntity.class:
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Users")
public class UserEntity extends BaseEntity implements Serializable {
// Other fields
@OneToOne(mappedBy = "userId")
private CandidateEntity candidate;
// Other fields
}
- CandidateEntity.class:
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Candidates")
public class CandidateEntity implements Serializable {
@Id
@MapsId
@OneToOne
@JoinColumn(name = "user_id", nullable = false, unique = true)
private UserEntity userId;
// Other fields
}
This works as is and creates the desired DB tables and columns. Now I proceed to create a repository for each of the entities I have, including the CandidateEntity
:
- CandidateEntityRepository.class:
public interface CandidateEntityRepository extends JpaRepository<CandidateEntity, UserEntity> {
}
Re-running the project yields this error message now:
[ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'welcomePageHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Unsatisfied dependency expressed through method 'welcomePageHandlerMapping' parameter 1: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception with message: Error creating bean with name 'jsonSchemaConverter' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Unsatisfied dependency expressed through method 'jsonSchemaConverter' parameter 1: Error creating bean with name 'associationLinks' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Unsatisfied dependency expressed through method 'associationLinks' parameter 0: Error creating bean with name 'resourceMappings' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Unsatisfied dependency expressed through method 'resourceMappings' parameter 0: Error creating bean with name 'repositories' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception with message: Error creating bean with name 'candidateEntityRepository' defined in com.example.simplevotingapi.repository.CandidateEntityRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: This class [class com.example.simplevotingapi.entity.CandidateEntity] does not define an IdClass
Adding @IdClass(UserEntity.class)
to the CandidateEntity
gets me the same error message, as if I don’t have it.
Removing the repository works. I still need to have a repository to manage the candidates independent of the users.
- build.gradle:
plugins {
id 'idea'
id 'java'
id 'org.springframework.boot' version '3.2.5'
id 'io.spring.dependency-management' version '1.1.5'
id 'org.hibernate.orm' version '6.5.1.Final'
id 'org.graalvm.buildtools.native' version '0.10.1'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
idea {
module {
downloadJavadoc = true
downloadSources = true
}
}
java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
implementation 'org.projectlombok:lombok-mapstruct-binding:0.2.0'
implementation 'org.mapstruct.extensions.spring:mapstruct-spring-annotations:1.1.1'
annotationProcessor 'org.mapstruct.extensions.spring:mapstruct-spring-extensions:1.1.1'
implementation 'io.github.linpeilie:mapstruct-plus-spring-boot-starter:1.4.0'
annotationProcessor 'io.github.linpeilie:mapstruct-plus-processor:1.4.0'
}
tasks.named('test') {
useJUnitPlatform()
}