我只是在学习spring,而数据库之间的关系却出现了问题,并以错误结束:
“ org.springframework.beans.factory.BeanCreationException:错误 在类路径中创建名称为“ entityManagerFactory”的bean 资源 [org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]: 调用init方法失败;嵌套异常为 org.hibernate.AnnotationException:未知的映射于: Varqina.WebReptile.models.entity.UserEntity.userPropertiesEntity, 引用的属性未知: 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;
答案 0 :(得分:0)
@OneToOne关系中的mappedBy属性必须与目标实体中的属性名称相对应。
因此它应该是“昵称”而不是“用户”
@OneToOne(mappedBy = "nickname",fetch = FetchType.LAZY,cascade = {})
private UserPropertiesEntity userPropertiesEntity;