Package br.com.caelum.agiletickets

Source Code of br.com.caelum.agiletickets.PreencheBanco

package br.com.caelum.agiletickets;

import java.math.BigDecimal;

import javax.persistence.EntityManager;

import org.joda.time.DateTime;

import br.com.caelum.agiletickets.models.Espetaculo;
import br.com.caelum.agiletickets.models.Estabelecimento;
import br.com.caelum.agiletickets.models.Sessao;
import br.com.caelum.agiletickets.models.TipoDeEspetaculo;
import br.com.caelum.vraptor.util.jpa.EntityManagerCreator;
import br.com.caelum.vraptor.util.jpa.EntityManagerFactoryCreator;

public class PreencheBanco {

  // ALUNO: Não apague essa classe
  public static void main(String[] args) {
    EntityManagerFactoryCreator creator = new EntityManagerFactoryCreator();
    creator.create();
    EntityManagerCreator managerCreator = new EntityManagerCreator(creator.getInstance());
    managerCreator.create();
    EntityManager manager = managerCreator.getInstance();

    manager.getTransaction().begin();
    manager.createQuery("delete from Sessao").executeUpdate();
    manager.createQuery("delete from Espetaculo").executeUpdate();
    manager.createQuery("delete from Estabelecimento").executeUpdate();
    Estabelecimento estabelecimento = new Estabelecimento();
    estabelecimento.setNome("Casa de shows");
    estabelecimento.setEndereco("Rua dos Silveiras, 12345");

    Espetaculo espetaculo = new Espetaculo();
    espetaculo.setEstabelecimento(estabelecimento);
    espetaculo.setNome("Depeche Mode");
    espetaculo.setTipo(TipoDeEspetaculo.SHOW);

    manager.persist(estabelecimento);
    manager.persist(espetaculo);

    for (int i = 0; i < 10; i++) {
      Sessao sessao = new Sessao();
      sessao.setEspetaculo(espetaculo);
      sessao.setInicio(new DateTime().plusDays(7+i));
      sessao.setDuracaoEmMinutos(60 * 3);
      sessao.setTotalIngressos(100);
      sessao.setIngressosReservados(i < 5 ? 100 - i : 0);
      sessao.setPreco(new BigDecimal("50"));
      manager.persist(sessao);
    }

    manager.getTransaction().commit();
    manager.close();
  }
}
TOP

Related Classes of br.com.caelum.agiletickets.PreencheBanco

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.