hibernate.dialect=net.sf.hibernate.dialect.PostgreSQLDialect hibernate.connection.driver_class=org.postgresql.Driver hibernate.connection.url=jdbc:postgresql://devserver/devdb hibernate.connection.username=dbuserhibernate.connection.password=dbpassword hibernate.query.substitutions yes ‘Y‘ |
Properties props = new Properties(); try { props.load(props.getClass().getResourceAsStream("hibernate.properties")); }catch(Exception e){ System.out.println("Error loading hibernate properties."); e.printStackTrace(); System.exit(0); } String driver = props.getProperty("hibernate.connection.driver_class"); String connUrl = props.getProperty("hibernate.connection.url"); String username = props.getProperty("hibernate.connection.username"); String password = props.getProperty("hibernate.connection.password"); // In my examples, I use Postgres, but Hibernate // supports virtually every popular dbms out there.Class.forName("org.postgresql.Driver"); Connection conn = DriverManager.getConnection(connUrl, username, password); Configuration cfg = new Configuration(); cfg.setProperties( props ); SessionFactory sessions = cfg.buildSessionFactory(); Session session = sessions.openSession(conn); |
Configuration cfg = new Configuration(); cfg.addResource("hello/Message.hbm.xml"); cfg.setProperties( System.getProperties() ); SessionFactory sessions = cfg.buildSessionFactory(); |
String cp = System.getProperty("java.class.path"); String jarFile = null; List hbmList = null;String[] cparr = cp.split("\\:"); for(int j=0;j<cparr.length;j++){ // The following assumes our entities // are wrapped up in a jar file // called ‘dbobjs.jar‘ if(cparr[j].indexOf("dbobjs.jar") != -1) jarFile=(cparr[j]); } if(jarFile != null){ JarFile jar = new JarFile(new File(jarFile)); Enumeration e = jar.entries(); if(e.hasMoreElements()) { hbmList = new ArrayList(); while(e.hasMoreElements()){ // Object comes back as JarFile$JarFileEntry JarEntry entry = (JarEntry)e.nextElement(); if(entry.getName().indexOf(".hbm.xml") != -1) { hbmList.add(entry.getName()); } } }else { System.out.println("Error: The entity jar dbobjs.jar was not found in " + "classpath: " + cp); } } |
Configuration cfg = new Configuration(); Iterator iterator = hbmFileNames.iterator(); while(iterator.hasNext()){ cfg.addResource((String)iterator.next()); } |