【问题标题】:Why I get message on composite class?为什么我会收到关于复合类的消息?
【发布时间】: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


【解决方案1】:

根据 Hibernate 文档 (http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite)

主键类必须是可序列化的。

它是由 JPA 规范定义的。

【讨论】:

    猜你喜欢
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 2011-08-07
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多