List of usage examples for org.hibernate SessionFactory openSession
Session openSession() throws HibernateException;
From source file:ch.bbw.siegrist.hotel.persitance.JPAHibernateDB.java
public JPAHibernateDB(){ Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); SessionFactory factory = configuration.buildSessionFactory(ssrb.build()); session = factory.openSession(); }
From source file:ch.bbw.siegrist.hotel.persitance.JPAHibernateDB.java
public JPAHibernateDB() { Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory factory = configuration.buildSessionFactory(ssrb.build()); session = factory.openSession(); }/*from w ww .j av a 2s . co m*/
From source file:ch.qos.logback.audit.persistent.Persistor.java
License:Open Source License
/** * Open an Hibernate Session./*from w w w. j a va 2 s . c o m*/ * * @return A valid Session instance, never null. * @throws HibernateException * In case the session could not be created. */ protected static Session openSession() throws HibernateException { final SessionFactory sessionFactory = getSessionFactory(); return sessionFactory.openSession(); }
From source file:chat.Models.java
License:LGPL
/** @param test whether use the testing database */ public static AnnotationConfiguration build(boolean test) throws Exception { AnnotationConfiguration conf = new AnnotationConfiguration() { private static final long serialVersionUID = 1L; @Override//from ww w. j a va2 s . co m public SessionFactory buildSessionFactory() throws HibernateException { if (!"org.hsqldb.jdbcDriver".equals(getProperty(Environment.DRIVER))) return super.buildSessionFactory(); // fix the issue of hsqldb write delay stupid default value SessionFactory fac = super.buildSessionFactory(); try { SessionImpl hib = (SessionImpl) fac.openSession(); hib.beginTransaction(); Statement stat = hib.getJDBCContext().borrowConnection().createStatement(); stat.executeUpdate("SET WRITE_DELAY FALSE"); hib.getTransaction().commit(); stat.close(); hib.close(); LOG.info("SET WRITE_DELAY FALSE"); } catch (Exception e) { throw new Error(e); } return fac; } }; InputStreamReader connect = new InputStreamReader( Models.class.getResourceAsStream("/hibernate.connect.properties"), "UTF-8"); conf.getProperties().load(connect); connect.close(); conf.setNamingStrategy(new NamingStrategy() { @Override public String classToTableName(String entity) { return StringHelper.unqualify(entity); } @Override public String propertyToColumnName(String property) { return StringHelper.unqualify(property); } @Override public String tableName(String table) { return table; } @Override public String columnName(String column) { return column; } @Override public String collectionTableName(String ownerEntity, String ownerTable, String associatedEntity, String associatedTable, String property) { return ownerTable + "_" + StringHelper.unqualify(property); } @Override public String joinKeyColumnName(String joinedColumn, String joinedTable) { return joinedColumn; } @Override public String foreignKeyColumnName(String property, String propertyEntity, String propertyTable, String referencedColumn) { return property != null ? StringHelper.unqualify(property) : propertyTable; } @Override public String logicalColumnName(String column, String property) { return StringHelper.isEmpty(column) ? StringHelper.unqualify(property) : column; } @Override public String logicalCollectionTableName(String table, String ownerTable, String associatedTable, String property) { if (table != null) return table; return ownerTable + "_" + StringHelper.unqualify(property); } @Override public String logicalCollectionColumnName(String column, String property, String referencedColumn) { return StringHelper.isEmpty(column) ? property + "_" + referencedColumn : column; } }); for (Class<?> c : Class2.packageClasses(Id.class)) conf.addAnnotatedClass(c); if (!"false".equals(conf.getProperty(Environment.AUTOCOMMIT))) throw new RuntimeException(Environment.AUTOCOMMIT + " must be false"); if (test) conf.setProperty(Environment.URL, conf.getProperty(Environment.URL + ".test")); return conf; }
From source file:City.City.java
/** * @param args the command line arguments */// ww w .ja v a 2 s . c o m public static void main(String[] args) { // TODO code application logic here //creating configuration object //Configuration cfg=new Configuration(); //cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file //creating seession factory object //ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()). build(); //SessionFactory factory=cfg.buildSessionFactory(serviceRegistry); SessionFactory factory = new Configuration().configure().buildSessionFactory(); //creating session object String csvFile = "/Users/test/desktop/SC labs/geo.csv"; String line = ""; String cvsSplitBy = ","; try { BufferedReader br = new BufferedReader(new FileReader(csvFile)); br.readLine(); br.readLine(); //int count=1; while ((line = br.readLine()) != null) { Session session = factory.openSession(); //creating transaction object // use comma as separator String[] country = line.split(cvsSplitBy); //System.out.println(line); Transaction t = session.beginTransaction(); perCity c1 = new perCity(); c1.setId(Integer.valueOf(country[0])); // setting the value of id c1.setLat(Double.valueOf(country[5])); // setting the value of Latitude c1.setLon(Double.valueOf(country[6])); // setting the value of Longitude c1.setCountry(country[1]); // Srtting the value of Country code session.persist(c1);//persisting the object t.commit();//transaction is commited session.close(); // Closing the session after } } catch (IOException e) { e.printStackTrace(); } }
From source file:cl.model.dao.ArticuloDAO.java
public void ingresar(Articulo articulo) { SessionFactory sf = null; Session session = null;//from ww w.j a v a 2s.co m Transaction tx = null; try { sf = HibernateUtil.getSessionFactory(); session = sf.openSession(); tx = session.beginTransaction(); session.save(articulo); tx.commit(); session.close(); } catch (Exception ex) { tx.rollback(); throw new RuntimeException("No se pudo guardar el articulo"); } }
From source file:cl.model.dao.ArticuloDAO.java
public void modificar(Articulo articulo) { SessionFactory sf = null; Session session = null;//from ww w .j av a 2 s . c om Transaction tx = null; try { sf = HibernateUtil.getSessionFactory(); session = sf.openSession(); tx = session.beginTransaction(); session.save(articulo); tx.commit(); session.close(); } catch (Exception ex) { tx.rollback(); throw new RuntimeException("No se pudo modificar el articulo"); } }
From source file:cl.model.dao.ArticuloDAO.java
public Articulo consultar(int codigo) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.openSession(); Articulo articulo = (Articulo) session.get(Articulo.class, codigo); if (articulo != null) { return articulo; } else {/*from www .ja va 2 s.co m*/ return null; } }
From source file:cl.model.dao.ArticuloDAO.java
public List<Articulo> findAll() { SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.openSession(); Query query = session.createQuery("from Articulo"); List<Articulo> lista = query.list(); session.close();/*from w ww. j a va 2 s . c om*/ return lista; }
From source file:cl.model.dao.ArticuloDAO.java
public void eliminar(int codigo) { SessionFactory sf = null; Session session = null;/*from w w w . j a v a 2s . c o m*/ Transaction tx = null; try { sf = HibernateUtil.getSessionFactory(); session = sf.openSession(); tx = session.beginTransaction(); session.delete(codigo); tx.commit(); session.close(); } catch (Exception ex) { tx.rollback(); throw new RuntimeException("No se pudo eliminar el articulo"); } }