List of usage examples for org.hibernate.cfg Configuration addAnnotatedClass
@SuppressWarnings({ "unchecked" }) public Configuration addAnnotatedClass(Class annotatedClass)
From source file:com.yahoo.elide.contrib.dropwizard.elide.SessionFactoryFactory.java
License:Apache License
private void addAnnotatedClasses(Configuration configuration, Iterable<Class<?>> entities) { final SortedSet<String> entityClasses = new TreeSet<>(); for (Class<?> klass : entities) { configuration.addAnnotatedClass(klass); entityClasses.add(klass.getCanonicalName()); }//from ww w . j a va2 s. c o m LOGGER.info("Entity classes: {}", entityClasses); }
From source file:cz.jirutka.rsql.hibernate.SessionFactoryInitializer.java
License:Open Source License
public static SessionFactory getSessionFactory() { if (instance != null) return instance; Configuration configuration = new Configuration(); configuration.setProperty(Environment.DRIVER, "org.hsqldb.jdbcDriver"); configuration.setProperty(Environment.URL, "jdbc:hsqldb:mem:ProductDAOTest"); configuration.setProperty(Environment.USER, "sa"); configuration.setProperty(Environment.DIALECT, HSQLDialect.class.getName()); configuration.setProperty(Environment.SHOW_SQL, "true"); configuration.addAnnotatedClass(Course.class); configuration.addAnnotatedClass(Department.class); configuration.addAnnotatedClass(Person.class); instance = configuration.buildSessionFactory(); return instance; }
From source file:dados.hibernate.GestorMusicaPersistentManager.java
@Override public Configuration createConfiguration() { Configuration configuration = new Configuration(); configuration.addAnnotatedClass(Artista.class); configuration.addAnnotatedClass(Album.class); configuration.addAnnotatedClass(Musica.class); configuration.buildMappings();/* w ww.j av a 2 s .co m*/ return configuration; }
From source file:dao.Teste.java
/** * @param args the command line arguments *//*from ww w . j a va 2 s . co m*/ public static void main(String[] args) { // TODO code application logic here //Configuration config = new Configuration(); /*Configuration config = new Configuration(); >>>>>>> origin/master ======= /*Configuration config = new Configuration(); >>>>>>> origin/master config.addAnnotatedClass(Autor.class); config.addAnnotatedClass(Categoria.class); config.addAnnotatedClass(Editora.class); config.addAnnotatedClass(Genero.class); config.addAnnotatedClass(Status.class); config.addAnnotatedClass(StatusLeitura.class); config.addAnnotatedClass(Usuario.class); config.addAnnotatedClass(Livro.class); <<<<<<< HEAD config.configure("hibernate.cfg.xml"); config.configure("hibernate.cfg.xml"); ======= //config.configure("hibernate.cfg.xml"); config.configure("hibernate.cfg.xml"); <<<<<<< HEAD >>>>>>> origin/master <<<<<<< HEAD new SchemaExport(config).create(true, true); Autor a = new Autor(0, "teste"); AutorDAO autordao = new AutorDAO(); autordao.salvar(a); ======= ======= >>>>>>>origin/master // PARA CRIAR AS TABELAS NO BANCO S DESCOMENTAR A LINHA ABAIXO -- PRECISA CRIAR O BANCO PRIMEIRO new SchemaExport(config).create(true, true); //E PARA ATUALIZAR, A LINHA ABAIXO new SchemaUpdate(config).execute(true, true);*/ //Autor a = new Autor(0, "teste"); //AutorDAO autordao = new AutorDAO(); //autordao.salvar(a); /*Testando o login //UsuarioController.salvar("teste", "teste", "teste", "img/fotopadrao.jpeg", "teste@teste"); if(UsuarioController.validaUsuario("teste", "teste")){ System.out.println("usuario logado com sucesso"); }else{ System.out.println("login ou senha incorreto"); } */ // new SchemaUpdate(config).execute(true, true); Configuration config = new Configuration(); config.addAnnotatedClass(Autor.class); config.addAnnotatedClass(Categoria.class); config.addAnnotatedClass(Editora.class); config.addAnnotatedClass(Genero.class); config.addAnnotatedClass(Status.class); config.addAnnotatedClass(StatusLeitura.class); config.addAnnotatedClass(Usuario.class); config.addAnnotatedClass(Livro.class); config.configure("hibernate.cfg.xml"); // PARA CRIAR AS TABELAS NO BANCO S DESCOMENTAR A LINHA ABAIXO -- PRECISA CRIAR O BANCO PRIMEIRO //new SchemaExport(config).create(true, true); //E PARA ATUALIZAR, A LINHA ABAIXO //new SchemaUpdate(config).execute(true, true); }
From source file:Database.Authentication.HibernateUtilUserAuthentication.java
License:Apache License
public static SessionFactory getSessionFactory() { if (sessionFactory == null) { // loads configuration and mappings Configuration configuration = new Configuration().configure("hibernate.userauthentication.cfg.xml"); configuration.addAnnotatedClass(User.class); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); // builds a session factory from the service registry sessionFactory = configuration.buildSessionFactory(serviceRegistry); }//from w w w . ja v a 2s . c o m return sessionFactory; }
From source file:database.service.DataBaseService.java
private Configuration getSqliteConfiguration() { Configuration configuration = new Configuration(); configuration.addAnnotatedClass(User.class); configuration.setProperty("hibernate.dialect", HIBERNATE_DIALECT); configuration.setProperty("hibernate.connection.driver_class", HIBERNATE_CONNECTION_DRIVER); configuration.setProperty("hibernate.connection.url", CONNECTION_URL); configuration.setProperty("hibernate.connection.username", DATABASE_USERNAME); configuration.setProperty("hibernate.connection.password", DATABASE_PASSWORD); configuration.setProperty("hibernate.show_sql", HIBERNATE_SHOW_SQL); configuration.setProperty("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO); configuration.setProperty("format_sql", "true"); return configuration; }
From source file:databaseUtility.NewHibernateUtil.java
public static SessionFactory getSessionFactory() { if (sessionFactory == null) { Configuration configuration = new Configuration(); configuration.addAnnotatedClass(Categoria.class).addAnnotatedClass(Evento.class) .addAnnotatedClass(Artista.class).addAnnotatedClass(Commento.class) .addAnnotatedClass(Utente.class).addAnnotatedClass(Provincia.class); configuration.configure();/* w w w .j a va2 s . co m*/ ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); // builds a session factory from the service registry sessionFactory = configuration.buildSessionFactory(serviceRegistry); } return sessionFactory; }
From source file:DataLayer.BaseDeDatos.java
public void inicializar() throws ParseException { Configuration config = new Configuration(); //config.addAnnotatedClass(Representacio.class); config.addAnnotatedClass(Seient.class); config.addAnnotatedClass(Local.class); config.configure("hibernate.cfg.xml"); new SchemaExport(config).create(true, true); StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(config.getProperties()).build(); SessionFactory factory = config.buildSessionFactory(serviceRegistry); Session session = factory.openSession(); session.beginTransaction();/*from w ww. j ava2 s . c o m*/ ArrayList<Seient> s = new ArrayList<>(); ArrayList<Seient> s2 = new ArrayList<>(); ArrayList<Seient> s3 = new ArrayList<>(); ArrayList<Seient> s4 = new ArrayList<>(); Local l = new Local(); l.setAdreca("Calle Desengao 21"); l.setNom("Teatre Tvoli"); l.setSeients(s); Local l2 = new Local(); l2.setAdreca("Calle del Ave del Paraiso 7"); l2.setNom("Palau Sant Jordi"); l2.setSeients(s2); Local l3 = new Local(); l3.setAdreca("Calle de la Energia 10"); l3.setNom("Barts"); l3.setSeients(s3); Local l4 = new Local(); l4.setAdreca("Calle Terol 26"); l4.setNom("Teatreneu"); l4.setSeients(s4); session.saveOrUpdate(l); session.saveOrUpdate(l2); session.saveOrUpdate(l3); session.saveOrUpdate(l4); for (int i = 1; i < 7; ++i) { for (int j = 1; j < 6; ++j) { Seient se = new Seient(i, j, l); Seient se2 = new Seient(i, j, l2); Seient se3 = new Seient(i, j, l3); Seient se4 = new Seient(i, j, l4); session.saveOrUpdate(se); session.saveOrUpdate(se2); session.saveOrUpdate(se3); session.saveOrUpdate(se4); l.setSeient(se); //s.add(se); l2.setSeient(se2); //s2.add(se2); l3.setSeient(se3); //s3.add(se3); l4.setSeient(se4); } } Sessio ses1 = new Sessio(TipusSessio.mati); Sessio ses2 = new Sessio(TipusSessio.tarda); Sessio ses3 = new Sessio(TipusSessio.nit); session.saveOrUpdate(ses1); session.saveOrUpdate(ses2); session.saveOrUpdate(ses3); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date d1 = sdf.parse("15/1/2016"); Date d2 = sdf.parse("20/1/2016"); Date d3 = sdf.parse("27/1/2016"); Date d4 = sdf.parse("30/1/2016"); Representacio r1 = new Representacio(ses1, l, 7.70f, 30, d1); Representacio r2 = new Representacio(ses1, l2, 7.70f, 30, d4); Representacio r3 = new Representacio(ses1, l3, 7.70f, 30, d3); Representacio r4 = new Representacio(ses2, l, 7.70f, 30, d2); Representacio r5 = new Representacio(ses2, l2, 7.70f, 30, d1); Representacio r6 = new Representacio(ses2, l3, 7.70f, 30, d4); Representacio r7 = new Representacio(ses3, l4, 8.50f, 30, d1); Estrena es1 = new Estrena(ses3, l, 9.70f, 30, d4, 1); Estrena es2 = new Estrena(ses3, l2, 7.70f, 30, d2, 2); Estrena es3 = new Estrena(ses3, l3, 7.70f, 30, d1, 2); ArrayList<Representacio> array1 = new ArrayList(); ArrayList<Representacio> array2 = new ArrayList(); ArrayList<Representacio> array3 = new ArrayList(); ArrayList<Representacio> array4 = new ArrayList(); ArrayList<Representacio> array5 = new ArrayList(); ArrayList<Representacio> array6 = new ArrayList(); array1.add(r1); array1.add(es1); array1.add(r7); array1.add(es3); array2.add(r2); array3.add(r3); array3.add(es2); array4.add(r4); array5.add(r5); array6.add(r6); Espectacle e1 = new Espectacle("Mamma Ma!", 3, array1); Espectacle e2 = new Espectacle("El Rey Len", 5, array2); Espectacle e3 = new Espectacle("Grease", 7, array3); Espectacle e4 = new Espectacle("Los miserables", 8, array4); Espectacle e5 = new Espectacle("Oliver Twist", 9, array5); Espectacle e6 = new Espectacle("Cats", 2, array6); r1.setEspectacle(e1); r2.setEspectacle(e2); r3.setEspectacle(e3); r4.setEspectacle(e4); r5.setEspectacle(e5); r6.setEspectacle(e6); r7.setEspectacle(e1); es1.setEspectacle(e1); es2.setEspectacle(e3); es3.setEspectacle(e1); session.saveOrUpdate(e1); session.saveOrUpdate(e2); session.saveOrUpdate(e3); session.saveOrUpdate(e4); session.saveOrUpdate(e5); session.saveOrUpdate(e6); /* ArrayList<Representacio> ses1Rep = new ArrayList<>(); ArrayList<Representacio> ses2Rep = new ArrayList<>(); ArrayList<Representacio> ses3Rep = new ArrayList<>(); ArrayList<Representacio> lRep = new ArrayList<>(); ArrayList<Representacio> l2Rep = new ArrayList<>(); ArrayList<Representacio> l3Rep = new ArrayList<>(); ArrayList<Representacio> e1Rep = new ArrayList<>(); ArrayList<Representacio> e2Rep = new ArrayList<>(); ArrayList<Representacio> e3Rep = new ArrayList<>(); ArrayList<Representacio> e4Rep = new ArrayList<>(); ArrayList<Representacio> e5Rep = new ArrayList<>(); ses1Rep.add(r1); ses1Rep.add(r2); ses1Rep.add(r3); ses2Rep.add(r4); ses2Rep.add(r5); ses2Rep.add(r6); ses3Rep.add(r7); ses1.setRepresentacions(ses1Rep); ses2.setRepresentacions(ses2Rep); ses3.setRepresentacions(ses3Rep); lRep.add(r1); lRep.add(r4); l2Rep.add(r2); l2Rep.add(r5); l2Rep.add(r7); l3Rep.add(r3); l3Rep.add(r6); l.setRepresentacions(lRep); l2.setRepresentacions(l2Rep); l3.setRepresentacions(l3Rep); ----Faltaria los arrays de espectaculo---- */ session.saveOrUpdate(r1); session.saveOrUpdate(r2); session.saveOrUpdate(r3); session.saveOrUpdate(r4); session.saveOrUpdate(r5); session.saveOrUpdate(r6); session.saveOrUpdate(r7); session.saveOrUpdate(es1); session.saveOrUpdate(es2); session.saveOrUpdate(es3); ArrayList<Moneda> canvis = new ArrayList<>(); canvis.add(Moneda.GBP); canvis.add(Moneda.USD); Showscom sc = new Showscom(1, 323243, "3394500", 3.0f, Moneda.EUR, canvis); session.saveOrUpdate(sc); ArrayList<SeientEnRepresentacio> SER1 = new ArrayList<>(); ArrayList<SeientEnRepresentacio> SER2 = new ArrayList<>(); ArrayList<SeientEnRepresentacio> SER3 = new ArrayList<>(); ArrayList<SeientEnRepresentacio> SER4 = new ArrayList<>(); ArrayList<SeientEnRepresentacio> SER5 = new ArrayList<>(); ArrayList<SeientEnRepresentacio> SER6 = new ArrayList<>(); ArrayList<SeientEnRepresentacio> SER7 = new ArrayList<>(); ArrayList<SeientEnRepresentacio> SER8 = new ArrayList<>(); ArrayList<SeientEnRepresentacio> SER9 = new ArrayList<>(); ArrayList<SeientEnRepresentacio> SER10 = new ArrayList<>(); for (int i = 0; i < 5 * 6; ++i) { if (s.get(i).getCKS().getLocal().equals(l)) { SeientEnRepresentacio r = new SeientEnRepresentacio(s.get(i), r1, Estat.lliure); session.saveOrUpdate(r); SeientEnRepresentacio rr = new SeientEnRepresentacio(s.get(i), r4, Estat.lliure); session.saveOrUpdate(rr); SeientEnRepresentacio rrr = new SeientEnRepresentacio(s.get(i), es1, Estat.lliure); session.saveOrUpdate(rrr); SER1.add(r); SER2.add(rr); SER3.add(rrr); } if (s2.get(i).getCKS().getLocal().equals(l2)) { SeientEnRepresentacio r11 = new SeientEnRepresentacio(s2.get(i), r2, Estat.lliure); session.saveOrUpdate(r11); SeientEnRepresentacio rr1 = new SeientEnRepresentacio(s2.get(i), r5, Estat.lliure); session.saveOrUpdate(rr1); SeientEnRepresentacio rrr1 = new SeientEnRepresentacio(s2.get(i), es2, Estat.lliure); session.saveOrUpdate(rrr1); SER4.add(r11); SER5.add(rr1); SER6.add(rrr1); } if (s3.get(i).getCKS().getLocal().equals(l3)) { SeientEnRepresentacio r22 = new SeientEnRepresentacio(s3.get(i), r3, Estat.lliure); session.saveOrUpdate(r22); SeientEnRepresentacio rr2 = new SeientEnRepresentacio(s3.get(i), r6, Estat.lliure); session.saveOrUpdate(rr2); SeientEnRepresentacio rr33 = new SeientEnRepresentacio(s3.get(i), es3, Estat.lliure); session.saveOrUpdate(rr33); SER7.add(r22); SER8.add(rr2); SER10.add(rr33); } if (s4.get(i).getCKS().getLocal().equals(l4)) { SeientEnRepresentacio r33 = new SeientEnRepresentacio(s4.get(i), r7, Estat.lliure); session.saveOrUpdate(r33); SER9.add(r33); } r1.setSER(SER1); r4.setSER(SER2); es1.setSER(SER3); r2.setSER(SER4); r5.setSER(SER5); es2.setSER(SER6); r3.setSER(SER7); r6.setSER(SER8); r7.setSER(SER9); es3.setSER(SER10); session.saveOrUpdate(r1); session.saveOrUpdate(r2); session.saveOrUpdate(r3); session.saveOrUpdate(r4); session.saveOrUpdate(r5); session.saveOrUpdate(r6); session.saveOrUpdate(r7); session.saveOrUpdate(es1); session.saveOrUpdate(es2); session.saveOrUpdate(es3); } session.getTransaction().commit(); session.close(); /* System.out.println("GET ADREA DEL LOCAL L2: " + l2.getAdreca()); System.out.println("GET NOM DEL LOCAL L2: " + l2.getNom()); System.out.println("GET SEIENTS DEL LOCAL L2:"); Iterator<Seient> it = l2.getSeients().iterator(); int i = 1; while(it.hasNext()){ System.out.println("Seient " + i); Seient s1 = it.next(); System.out.println("LOCAL DEL SEIENT: " + s1.getCompoundKeySeient().getLocal().getNom() + " FILA: " + s1.getCompoundKeySeient().getFila() + " COLUMNA: " + s1.getCompoundKeySeient().getColumna()); ++i; }*/ }
From source file:de.eod.jliki.db.DBManager.java
License:Open Source License
/** * Creates instance.<br/>//from ww w .j a va2s.c o m * @param tableClasses the classes (with annotations) that can be stored in the database */ public DBManager(final Class<?>[] tableClasses) { final String jdbcDriver = ConfigManager.getInstance().getConfig().getDbConfig().getDriver(); final String dbUrl = ConfigManager.getInstance().getConfig().getDbConfig().getUrl(); final String dbName = ConfigManager.getInstance().getConfig().getDbConfig().getDbName(); final String dbUser = ConfigManager.getInstance().getConfig().getDbConfig().getUser(); final String dbPass = ConfigManager.getInstance().getConfig().getDbConfig().getPassword(); final Map<String, String> addParams = ConfigManager.getInstance().getConfig().getDbConfig() .getAdditionalParams(); String connectUrl = dbUrl; if (!dbUrl.endsWith("/")) { connectUrl += "/"; } connectUrl += dbName; final Properties dbProps = new Properties(); dbProps.setProperty("hibernate.connection.driver_class", jdbcDriver); dbProps.setProperty("hibernate.connection.url", connectUrl); dbProps.setProperty("hibernate.connection.username", dbUser); dbProps.setProperty("hibernate.connection.password", dbPass); dbProps.setProperty("hibernate.c3p0.min_size", "5"); dbProps.setProperty("hibernate.c3p0.max_size", "20"); dbProps.setProperty("hibernate.c3p0.timeout", "1800"); dbProps.setProperty("hibernate.c3p0.max_statements", "50"); if (addParams != null) { for (final Map.Entry<String, String> entry : addParams.entrySet()) { dbProps.setProperty(entry.getKey(), entry.getValue()); } } final Configuration hibConfig = new Configuration(); hibConfig.setProperties(dbProps); for (final Class<?> clazz : tableClasses) { hibConfig.addAnnotatedClass(clazz); } this.hibSessionFactory = hibConfig.buildSessionFactory(); }
From source file:de.fau.osr.core.db.DBTestHelper.java
License:Open Source License
/** * creates configuration from original, and change connection to H2 db * sets "hibernate.hbm2ddl.auto" to "create" * @return configuration to work with H2 *///from w w w . ja va 2s . c om public static Configuration createH2Configuration() { // get original configuration Configuration configuration = new Configuration(); //add persistent classes for (Class<?> clazz : HibernateUtil.getPersistentClasses()) { configuration.addAnnotatedClass(clazz); } //change connection settings configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); configuration.setProperty("hibernate.connection.driver_class", "org.h2.Driver"); //reset schema configuration.setProperty("hibernate.default_schema", "PUBLIC"); //set db mode to create configuration.setProperty("hibernate.hbm2ddl.auto", "create"); //connection string configuration.setProperty("hibernate.connection.url", "jdbc:h2:mem:test"); return configuration; }