Hibernate: Increment from a specific value

1

I created an attribute of type Integer called registration and I want it, when the user registers, the value of this registration is generated automatically and saved in mysql from a certain value (Ex: 95000) and as others go if you register, it will be the last number used + 1 (95001, 95002 ...).

Can you do this with Hibernate?

    
asked by anonymous 18.07.2016 / 23:20

2 answers

1

I do not know if I got it right but come on. I will assume that you already have a MySQL database running and that you will need to retrieve the last (highest) registration used and use it to determine the registration number of the new registration you are creating.

First of all, use a query to retrieve the values of the matrices and compare them to each other to find out which one is the largest.

private int getUltimaMatricula() {
  EntityManager em = createEntityManagerFactory();
  em.getTransaction().begin();
    Query q = em.createQuery("SELECT t.coluna1 from tabela t where t.campo1 = :campo1");

    // campo1 e o que voce ira utilizar para recuperar as matriculas, utilizando jpql 
    // neste caso.
    q.setParameter("campo1", campo1);

    List<Integer> result = q.getResultList();

  int maiorNumero = Integer.MIN_VALUE;

  for(i=0; i<result.size(); i++){
    if (result.get(i) > maiorNumero)
      maiorNumero = result.get(i);
  }

  em.getTransaction().commit();
  em.close();

  return maiorValor;
}

After this, call this method within your "persist" method or something and use the number returned +1 to determine the number of the new license plate.

Any questions, please comment here.

    
18.07.2016 / 23:39
0
___ erkimt ___ Hibernate: Increment from a specific value ______ qstntxt ___

I created an attribute of type %code% called registration and I want it, when the user registers, the value of this registration is generated automatically and saved in mysql from a certain value (Ex: 95000) and as others go if you register, it will be the last number used + 1 (95001, 95002 ...).

Can you do this with Hibernate?

    
______ azszpr141349 ___

I do not know if I got it right but come on. I will assume that you already have a MySQL database running and that you will need to retrieve the last (highest) registration used and use it to determine the registration number of the new registration you are creating.

First of all, use a query to retrieve the values of the matrices and compare them to each other to find out which one is the largest.

private void gerarMatricula(Aluno a) {

    session = HibernateUtil.getSessionFactory().openSession();
    transaction = session.beginTransaction();
    query = session.createQuery("select matricula from Aluno");

    List<Integer> result = query.list();
    if (result.isEmpty()) {
        a.setMatricula(45100); //O primeiro aluno cadastrado receberá a matricula 45100, 

    } else{
        int maiorNumero = Integer.MIN_VALUE;

        for (int i = 0; i < result.size(); i++) {
            if (result.get(i) > maiorNumero)
                maiorNumero = result.get(i);

        }
        a.setMatricula(maiorNumero + 1);
    }


}

After this, call this method within your "persist" method or something and use the number returned +1 to determine the number of the new license plate.

Any questions, please comment here.

    
______ ___ azszpr141729

I did it! Follow your idea and did so:

public void create(Aluno a) throws Exception {

    session = HibernateUtil.getSessionFactory().openSession();
    transaction = session.beginTransaction();
    gerarMatricula(a);
    CriptografarSenha cs = new CriptografarSenha();
    cs.criptografia(a); //Criptografar a senha
    session.save(a);
    transaction.commit();
    session.close();

}

Then I called this method in my "persist":

private void gerarMatricula(Aluno a) {

    session = HibernateUtil.getSessionFactory().openSession();
    transaction = session.beginTransaction();
    query = session.createQuery("select matricula from Aluno");

    List<Integer> result = query.list();
    if (result.isEmpty()) {
        a.setMatricula(45100); //O primeiro aluno cadastrado receberá a matricula 45100, 

    } else{
        int maiorNumero = Integer.MIN_VALUE;

        for (int i = 0; i < result.size(); i++) {
            if (result.get(i) > maiorNumero)
                maiorNumero = result.get(i);

        }
        a.setMatricula(maiorNumero + 1);
    }


}

Thanks for the help!

    
___
20.07.2016 / 20:10