hibernate.dialect=net.sf.hibernate.dialect.PostgreSQLDialect hibernate.connection.driver_class=org.postgresql.Driver hibernate.connection.url=jdbc:postgresql://devserver/devdb hibernate.connection.username=dbuser hibernate.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 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()); } |
使用Session
关于这方面的具体内容,因为可以找到各种与Hibernate、持久化和查询对象有关的文章、教程或者大量关于使用事务的例子,所以我不再介绍任何细节。而是考虑要用实体作什么,以及它们是如何影响Hibernate Session对象的。能否使用现有的业务对象甚至数据访问对象?在设置数据层时,我使用Spring以及它所提供的一些管理连接、事务和Session的类。这些对象都使用与Spring紧密集成的各种规则和关系定义在XML配置文件中。首先,DAO对象通过Spring的依赖注入(参见Bruce Tate的《Five Things I Love About Spring》)注入到服务中,然后配置服务以捕获特定的DAO异常(在XML配置文件中),这种异常是Spring可以正确处理的。虽然我觉得将Spring集成到数据加载应用程序中的工作量很大,我还是对DAO对象进行了细微的调整。这样,这些对象就可以在web应用程序之外使用了。
假设我在PersonDAO中有一个用来保存person对象的方法。由于容器已经设置好了Hibernate Session,我就不能在容器外重用这一DAO方法,因为它需要使用已经存在并完全配置好的Session对象。下面的代码是使用Spring容器所提供的Session支持后的典型PersonDAO:
import org.springframework.orm.hibernate.HibernateTemplate; import test.pojos.Person; public class PersonDAO extends HibernateTemplate { public PersonDAO(){} public Person save(Person aPerson) { if(aPerson != null) super.save(person); return person; } } |
import org.springframework.orm.hibernate.HibernateTemplate; import net.sf.hibernate.Session; import test.pojos.Person; public class PersonDAO extends HibernateTemplate { public PersonDAO(){} public void setExternalSessionFactory(Session aSession){ setSessionFactory(session.getSessionFactory()); } public Person save(Person aPerson) { if(aPerson != null) super.save(person); return person; } } |
import net.sf.hibernate.Session; public class PersonDAO { // This example assumes that there is a Hibernate // Session object at the following JNDI location // on a Tomcat 5.5 server: // java:/comp/env/obj/hibernateSession private Session session; public PersonDAO(){ try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); session = (Session)envCtx. lookup("obj/hibernateSession"); }catch(Exception e) { e.printStackTrace(); } } public Person save(Person aPerson) { if(aPerson != null) session.save(person); return person; } } |
import net.sf.hibernate.Session; public class PersonDAO { // This example assumes that there is a Hibernate // Session object at the following JNDI location // on a Tomcat 5.5 server: // java:/comp/env/obj/hibernateSession private Session session; public PersonDAO(){ try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); session = (Session)envCtx. lookup("obj/hibernateSession"); }catch(Exception e) { e.printStackTrace(); } } public PersonDAO(Session aSession){ session = aSession; } public Person save(Person aPerson) { if(aPerson != null) session.save(person); return person; } } |