log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.MappingException: An association from the table VF_USER_ROLE_MAP refers to an unmapped class: com.database.persistence.VfRole
java.lang.ExceptionInInitializerError
at com.common.util.HibernateUtil. <clinit>(HibernateUtil.java:31)
Caused by: org.hibernate.MappingException: An association from the table VF_USER_ROLE_MAP refers to an unmapped class: com.database.persistence.VfRole
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1176)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1094)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1210)
at com.common.util.HibernateUtil. <clinit>(HibernateUtil.java:22)
Exception in thread "main"
源代码
package com.common.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
public class HibernateUtil {
public static String DEFAULT_DATABASE;
private static final String fileprefix = "hibernate";
private static final String filetype = ".cfg.xml";
private static final HashMap <String,SessionFactory> sessionFactorys=new HashMap <String,SessionFactory>();
static{
try {
// Create the SessionFactory from *.cfg.xml
ArrayList <String> filelist =getHibernateCFGFile("/");
for(String file : filelist){
//System.out.println(file);
SessionFactory sessionFactory = new Configuration().configure(file).buildSessionFactory();
sessionFactorys.put(file.toLowerCase().replaceAll(filetype,""),sessionFactory);
// System.out.println(file.toLowerCase().replaceAll(filetype,""));
}
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
// System.out.println(ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory(String dbname) {
dbname = dbname.toLowerCase();
return sessionFactorys.containsKey(dbname)?sessionFactorys.get(dbname):null;
}
private static ArrayList <String> getHibernateCFGFile(String path){
ArrayList <String> al = new ArrayList <String>();
URL url = Configuration.class.getResource(path);
File file = new File (url.getFile());
if(file.isDirectory()){
File[] fl = file.listFiles();
for(File f :fl){
if(f.isFile()&&validateFileName(f.getName(),filetype,""))
//al.add(url.getPath()+f.getName());
al.add(f.getName());
}
}else{
if(validateFileName(file.getName(),filetype,""))
//al.add(url.getPath());
al.add(file.getName());
}
return al;
}
private static boolean validateFileName(String filename,String filetype,String fileprefix){
return filename.toLowerCase().startsWith(fileprefix)&&filename.toLowerCase().endsWith(filetype);