H
H
have_a_questions2016-04-19 18:52:57
Hibernate
have_a_questions, 2016-04-19 18:52:57

How to insert an int value into a field that refers to another object in hibernate mapping?

Hello. There are two entities from which one refers to the other. In the hibernate mapping, the settings are set and, accordingly, the id_users field is represented in InfoUsers as users so that when selecting, the Users class is returned by the corresponding id and not vice versa.

public class InfoUsers {
    private int id;
    private Users  users;
    private String idUuid;
    private String name;
    private String surname;
    private String email;

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @JoinColumn(name = "id_user",referencedColumnName = "id")
    public Users getIdUser() {
        return users;
    }

    public void setIdUser(Users user) {
        this.users = users;
    }

    @Basic
    @Column(name = "id_uuid")
    public String getIdUuid() {
        return idUuid;
    }

    public void setIdUuid(String idUuid) {
        this.idUuid = idUuid;
    }


@Entity
public class Users {
    private int id;
    private String login;
    private String password;
    private byte enabled;

    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "login")
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

How to make it so that when inserting, that is, when infoUsers.setIdUser(addUser(user)); sessionFactory.getCurrentSession().save(infoUsers); the id of the users object was inserted into the database. I just don’t know how the hibernate box is implemented and, accordingly, I am waiting for your help.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question