Search code examples
javaspringhibernateentity

How to fix " Error creating bean with name 'entityManagerFactory' defined in class path resource"


I'm just learning spring and I have a problem in the relationship of databases ended with error:

"org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unknown mappedBy in: Varqina.WebReptile.models.entity.UserEntity.userPropertiesEntity, referenced property unknown: Varqina.WebReptile.models.entity.UserPropertiesEntity.user"

UserProperies:
import lombok.Data;
import javax.persistence.*;
import java.time.LocalDateTime;
@Table(name = "user")
@Data
@Entity
public class UserEntity {
    public enum AccountStatus{
        ACTIVE, PREMIUM, NOT_ACTIVE;
    }
    @Id @GeneratedValue private Integer id;
    private String nickname;
    private String password;
    private @Column(name = "creation_time") LocalDateTime creationTime;
    @Enumerated(EnumType.STRING)
    private AccountStatus status;
    private String email;

    @OneToOne(mappedBy = "user",fetch = FetchType.LAZY,cascade = {})
    private UserPropertiesEntity userPropertiesEntity;

}
UserPropertiesEntity:
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
@Table(name = "user_properties")
public class UserPropertiesEntity {
    private @Id @GeneratedValue Integer id;
    private String city;
    private String nation;
    private @Column(name = "birth_day") String birthDay;
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private UserEntity nickname;




Solution

  • The mappedBy attribute in the @OneToOne relationship must correspond to the attribute name in the destination Entity.

    So it should be "nickname" not "user"

    @OneToOne(mappedBy = "nickname",fetch = FetchType.LAZY,cascade = {})
    private UserPropertiesEntity userPropertiesEntity;