我的项目中的一对一外键关系映射存在问题。
User.java
@Access(AccessType.FIELD)
@Entity
@Table(name = "users")
public class User {
public Role ro;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int user_id;
@Column(name = "username")
public String username;
@Column(name = "password")
public String password;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "username")
public Role getRo() {
return ro;
}
public void setRo(Role ro) {
this.ro = ro;
}
@Column(name = "enabled")
public int enabled;
public User() {
}
public int getUser_id() {
return user_id;
}
public User(String username, String password, int enabled) {
this.username = username;
this.password = password;
this.enabled = enabled;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getEnabled() {
return enabled;
}
public void setEnabled(int enabled) {
this.enabled = enabled;
}
@Override
public String toString() {
return String.format(
"student[id=%d, name='%s', age='%d']",
ro.getUsername(), username, ro.getUsername());
}
}
Role.java
import javax.persistence.*;
@Access(AccessType.FIELD)
@Entity
@Table(name = "user_roles")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int user_role_id;
@Column(name = "username")
public String username;
@Column(name = "role")
public String role;
public User user;
@OneToOne(mappedBy = "ro")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Role(String username, String role) {
this.username = username;
this.role = role;
}
public Role() {
}
public int getUser_role_id() {
return user_role_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
这是错误:
引起:org.hibernate.MappingException:无法确定:com.company.project.models.Role的类型,在表:users,对于列:[org.hibernate.mapping.Column(ro)] 在org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.mapping.Property.isValid(Property.java:225)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.mapping.RootClass.validate(RootClass.java:265)~ [hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final] 在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] ...省略了22个常见帧
答案 0 :(得分:2)
您应该在变量声明或@Column
上注释所有@OneToOne
/ getter()
。这就是为什么hibernate在Role
中找不到class User
。