Hibernate: Increment from a specific value

Asked

Viewed 106 times

1

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

You can do that with Hibernate?

2 answers

1

I’m not sure I understand, but come on. I will assume that you already have a running Mysql database and that you will need to recover the last (highest) enrollment used and use it to determine which registration number of the new registration you are creating.

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

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 that, call this method inside your "persist" method or something of the type and use the number returned +1 to determine the number of the new enrollment.

Any questions, comment here.

0

I got it! I followed your idea and I did so:

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


}

Then I called this method in my "persist":

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();

}

Thanks for the help!

  • Did you mean Obrigado pela ajuda! or De nada pela ajuda! ? rs.. Because apparently you solved the problem of the author of the question, or am I mistaken?

  • 1

    It was "Thanks for the help" even, rsrs, because I am the author of the question, but when I asked, was not yet registered. I asked as a guest, so the name is different. I posted the solution to help in case someone needs.

  • You can merge your accounts by contacting us at: http://answall.com/contact so you can regain authorship of the question and accept the answer that helped you most, if you consider it so.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.