【发布时间】:2017-08-31 12:54:13
【问题描述】:
我想将上次登录的用户存储在单独的表中。我创建了 log_info 表并为其创建了 LogInfo 类:
@Entity
@Table(name="log_info")
public class LogInfo {
@Id
@OneToOne
@JoinColumn(name="user_id")
private Accountant id;
public LogInfo(){}
public LogInfo(Accountant user){
id = user;
}
public Accountant getId() {
return id;
}
public void setId(Accountant userId) {
this.id = userId;
}
}
我的会计类是:
@Entity
@Table(name="accountant")
public class Accountant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="accountant_id")
private int id;
@Column(name="name")
private String name;
@Column(name="surname")
private String surname;
@Column(name="mail")
private String mail;
@Column(name="avatar")
private String avatar;
@Column(name="phone")
private String phone;
@ManyToOne
@JoinColumn(name="interface_lang_id")
private Languages interfaceCode;
public Accountant(){}
public Accountant(String name, String surname, String mail, String phone, Languages interfaceLang, String avatar){
this.name = name;
this.surname = surname;
this.mail = mail;
this.avatar = avatar;
this.phone = phone;
this.interfaceCode = interfaceLang;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Languages getInterfaceCode() {
return interfaceCode;
}
public void setInterfaceCode(Languages interfaceCode) {
this.interfaceCode = interfaceCode;
}
}
这是数据库结构:
log_info 表将始终只有一条最后登录的用户记录。当我尝试运行我的应用程序时出现异常:org.hibernate.MappingException: Composite-id class must implement Serializable: home.accounting.model.LogInfo 为什么即使我只有一列也会出现此异常?
【问题讨论】:
-
但我只有一个
@Id -
您在
LogInfo中的Id 列的类型为Accountant -
不应该?我认为这是休眠对象的工作方式
-
你知道的,你所说的一切其实都可以作为答案,因为它完全涵盖了我的问题
标签: java database hibernate hibernate-mapping hibernate-annotations