| | |
| | | package com.vci.server.base.persistence.dao; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | import java.sql.Connection; |
| | | import java.sql.DatabaseMetaData; |
| | | import java.sql.SQLException; |
| | | import java.util.Enumeration; |
| | | import java.util.List; |
| | | import java.util.ArrayList; |
| | | import org.dom4j.Attribute; |
| | | import org.dom4j.Document; |
| | | import org.dom4j.DocumentException; |
| | |
| | | */ |
| | | //private static final Log log = LogFactory.getLog(HibernateSessionFactory.class); |
| | | private static String CONFIG_FILE_LOCATION="properties/hibernate.cfg.xml"; |
| | | //private static String MAP_FILE_RESOURCE="/properties/hibernate.map.xml"; |
| | | private static String MAP_FILE_RESOURCE="hibernate.map.xml"; |
| | | private static String MAP_FILE_RESOURCE="/properties/hibernate.map.xml"; |
| | | //private static String MAP_FILE_RESOURCE="hibernate.map.xml"; |
| | | |
| | | private static final ThreadLocal<VciSession> threadLocal = new ThreadLocal<VciSession>(); |
| | | private static Configuration configuration = new Configuration(); |
| | |
| | | |
| | | configuration.configure(file); |
| | | |
| | | //loadHbmFiles(); |
| | | ServerWithLog4j.logger.info("加载Hibernate对象映射:Begin"); |
| | | loadHbmFiles(); |
| | | ServerWithLog4j.logger.info("加载Hibernate对象映射:End"); |
| | | |
| | | sessionFactory = configuration.buildSessionFactory(); |
| | | } catch (Exception e) { |
| | |
| | | File file = new File(CONFIG_FILE_LOCATION); |
| | | configuration.configure(file); |
| | | |
| | | ServerWithLog4j.logger.info("加载Hibernate对象映射:Begin"); |
| | | loadHbmFiles(); |
| | | ServerWithLog4j.logger.info("加载Hibernate对象映射:End"); |
| | | |
| | | sessionFactory = configuration.buildSessionFactory(); |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | |
| | | private static void loadHbmFiles() { |
| | | ServerWithLog4j.logger.info("加载Hibernate对象映射"); |
| | | try { |
| | | List<InputStream> iss = loadResources(MAP_FILE_RESOURCE, HibernateSessionFactory.class.getClassLoader()); |
| | | //InputStream is = HibernateSessionFactory.class.getResourceAsStream(MAP_FILE_RESOURCE); |
| | | //if (is != null) |
| | | for (InputStream is : iss) |
| | | //List<InputStream> iss = loadResources(MAP_FILE_RESOURCE, HibernateSessionFactory.class.getClassLoader()); |
| | | //List<InputStream> iss = loadResources(MAP_FILE_RESOURCE, null); |
| | | InputStream is = HibernateSessionFactory.class.getResourceAsStream(MAP_FILE_RESOURCE); |
| | | //for (InputStream is : iss) |
| | | if (is != null) |
| | | { |
| | | SAXReader reader = new SAXReader(); |
| | | Document doc; |
| | | doc = reader.read(is); |
| | | doc = reader.read(is); |
| | | Element root = doc.getRootElement(); |
| | | List<?> lst = root.elements("mapping"); |
| | | |
| | |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | ServerWithLog4j.logger.error("加载对象映射失败", e); |
| | | } catch (IOException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | private static List<InputStream> loadResources(final String name, final ClassLoader classLoader) throws IOException { |
| | | final List<InputStream> list = new ArrayList<InputStream>(); |
| | | final Enumeration<URL> systemResources = |
| | | (classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader) |
| | | .getResources(name); |
| | | while (systemResources.hasMoreElements()) { |
| | | list.add(systemResources.nextElement().openStream()); |
| | | } |
| | | return list; |
| | | } |
| | | // private static List<InputStream> loadResources(final String name, final ClassLoader classLoader) throws IOException { |
| | | // final List<InputStream> list = new ArrayList<InputStream>(); |
| | | // final Enumeration<URL> systemResources = |
| | | // (classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader) |
| | | // .getResources(name); |
| | | // while (systemResources.hasMoreElements()) { |
| | | // ServerWithLog4j.logger.info(MAP_FILE_RESOURCE + ":" + systemResources.nextElement().getPath()); |
| | | // |
| | | // list.add(systemResources.nextElement().openStream()); |
| | | // } |
| | | // return list; |
| | | // } |
| | | } |