无法确定以下类型:
  com.brainstormers.justlearnit.models.UserDetail,在表上:users,用于
  列:[org.hibernate.mapping.Column(userDetail)]


这是我的User类:

@Entity
@Table(name = "users")
public class User {
    private String username;
    private String password;
    private int enabled;
    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL,
            fetch = FetchType.LAZY, optional = false)
    private UserDetail userDetail;

    public User() {
    }

    @Id
    @Column(name = "username", nullable = false, length = 50)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Basic
    @Column(name = "password", nullable = false, length = 50)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Basic
    @Column(name = "enabled", nullable = false)
    public int getEnabled() {
        return enabled;
    }

    public void setEnabled(int enabled) {
        this.enabled = enabled;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return enabled == user.enabled &&
                Objects.equals(username, user.username) &&
                Objects.equals(password, user.password);
    }

    @Override
    public int hashCode() {

        return Objects.hash(username, password, enabled);
    }


    public UserDetail getUserDetail() {
        return userDetail;
    }

    public void setUserDetail(UserDetail userDetail) {
        this.userDetail = userDetail;
    }
}


和UserDetail类:

@Entity
@Table(name = "user_detail")
public class UserDetail {
    private String username;
    private String firstName;
    private String lastName;
    private String email;
    private String country;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "userDetail")
    @JoinColumn(name = "username")
    private User user;


    public UserDetail() {
    }

    @Id
    @Column(name = "username", nullable = false, length = 50)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Basic
    @Column(name = "first_name", nullable = false, length = 50)
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Basic
    @Column(name = "last_name", nullable = false, length = 50)
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Basic
    @Column(name = "email", nullable = false, length = 50)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Basic
    @Column(name = "country", nullable = false, length = 50)
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserDetail that = (UserDetail) o;
        return Objects.equals(username, that.username) &&
                Objects.equals(firstName, that.firstName) &&
                Objects.equals(lastName, that.lastName) &&
                Objects.equals(email, that.email) &&
                Objects.equals(country, that.country);
    }

    @Override
    public int hashCode() {

        return Objects.hash(username, firstName, lastName, email, country);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


}


它是由IntelliJ IDEA中的持久性框架生成的。我尝试自己编写实体代码,但是得到了相同的结果。

最佳答案

OneToOne#mappedBy的文档中:


  (可选)拥有关系的字段。仅此元素
  在关联的反向(非所有权)侧指定。


因此,删除mappedBy = "userDetail"中的UserDetail

关于java - @OneToOne JPA hibernate 无法确定其类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50496327/

10-08 23:02