Grant value byte in a set

3

We have a DB with a User table that has a column of type Tinyint (Since it is a boolean) and we are working with Hibernate. My User model has a variable, autogenerated, Byte type to designate that user value (setters & getters).

The fact is that within this method:

    public boolean CreateJudge(int idusuario){
    boolean newJ = false;

    try{
        newJ = true;
        this.session = Hibernate.getSessionFactory().getCurrentSession();
        Usuario newJudge = new Usuario(master.setJuez((byte) 1), idusuario);
        session.save(newJudge);
        session.close();

    }catch (Exception e){ 
        e.printStackTrace();
    }     
    return newJ;
}

What I want is to be able to give that value 1 to the byte and thus force that user to be a judge.

Any ideas? The proposed code does not work and I've already tried:

   Usuario newJudge = new Usuario(master.setJuez(1), idusuario);
    
asked by Francrt 20.04.2018 в 10:24
source

1 answer

2

After a while of thinking about it I found the solution to my problem myself (something very basic):

    public boolean CreateJudge(){
    boolean newJ = false;

    try{
        newJ = true;
        Byte juez = 1;
        this.session = Hibernate.getSessionFactory().getCurrentSession();
        Usuario newJudge = new Usuario(master.getDni(), master.getTelefono(), master.getNickname(), master.getPassword(), master.getEmail(), master.getAdmin(), juez);
        session.save(newJudge);
        session.close();

    }catch (Exception e){ 
        e.printStackTrace();
    }     
    return newJ;
}

We generate a new user in this case: newJudge; and we force a byte juz = 1, knowing that whenever we invoke the CreateJudge () method; what we are doing is creating a user who is a judge.

I add:

In case anyone would like a way to update an existing User object within the DB, they would simply have to do the following:

      public boolean UpdateToJudge(int nickname){
    boolean upUj = false;       
    this.session = Hibernate.getSessionFactory().getCurrentSession();
    org.hibernate.Transaction tx = session.beginTransaction();

    try{
        upUj = true;
        Usuario improve;
        Query update = session.createQuery("from Usuario as usser where usser.nickname='"+nickname+"'");
        improve = (Usuario)update.uniqueResult();
        improve.setJuez((byte) 1);
        session.saveOrUpdate(improve);
        tx.commit();
        session.close();

    }catch(Exception e){
        e.printStackTrace();
        tx.rollback();
    }
    return upUj;
}

As you can see, we extract the object from the database, save it temporarily in our method and change the value of Judge to 1, thus achieving the update.

    
answered by 20.04.2018 / 10:39
source