Why I get message on composite class?

100 views Asked by At

I want to store user that was logged last time in a separate table. I created log_info table and for it LogInfo class:

@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;
    }

}

My Accountant class is:

@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;
    }

}

This is database structure:

enter image description here

log_info table will always have just one record of user that was logged in last. When I tried running my app I got exception: org.hibernate.MappingException: Composite-id class must implement Serializable: home.accounting.model.LogInfo Why I'm getting this exception even though I have only one column?

1

There are 1 answers

0
Przemyslaw Zawadka On

According to Hibernate documentation (http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite)

The primary key class must be serializable.

It's defined by JPA specification.