當我將Hibernate 3.2.7遷移到Hibernate 4.3.5,將Java7遷移到Java8並將Tomcat7遷移到Tomcat8時,我得到了下面的2個錯誤。請爲這個問題提供一些解決方案。SessionFactory類型的openSession()方法不適用於參數(Connection)
The method openSession() in the type SessionFactory is not applicable for the arguments (AuditLogInterceptor)
HibernateUtil.javaThe method connection() is undefined for the type Session
- AuditLogInterceptor.java
HibernateUtil.java
public class HibernateUtil {
private static Configuration configuration;
private static ServiceRegistry serviceRegistry;
private static SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
private static final ThreadLocal threadInterceptor = new ThreadLocal();
static {
try {
System.out.println("inside hibernate util before calling annotation configuration");
configuration = new AnnotationConfiguration();
System.out.println("inside hibernate util after calling annotation configuration before building sessionFactory");
// sessionFactory = configuration.configure().buildSessionFactory();
// hibernate 3.5.6 jar update version code
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
System.out.println("inside hibernate util after calling annotation configuration after building sessionFactory");
System.out.println("static intializer of HibenrateUtil");
if(sessionFactory == null){
System.out.println("session factory is null");
}else{
System.out.println("session factory is not null");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static Session getSession() throws HibernateException{
//System.out.println("inside getSession method");
Session s = (Session) threadSession.get();
try {
//System.out.println("inside try getSession method");
//if(s == null){
//System.out.println("session is null");
//}
if (s == null || !s.isOpen()) {
//System.out.println("Opening new Session for this thread.");
AuditLogInterceptor interceptor = new AuditLogInterceptor();
**s = sessionFactory.openSession(interceptor);**
interceptor.setSession(s);
}
threadSession.set(s);
//System.out.println("session :"+s);
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
return s;
}
AuditLogUtil.ja VA
public class AuditLogUtil{
public static void LogIt(String action,
IAuditLog entity, Connection conn) throws Exception{
SessionServiceImpl sessionServiceImpl=new SessionServiceImpl();
TbMasUsers tbMasUsers=(TbMasUsers) sessionServiceImpl.getUserInSession();
Integer loingEmpId=Integer.parseInt(tbMasUsers.getIntEmpId().getStrEmpId());
Session tempSession = HibernateUtil.getSessionFactory().openSession(conn);
try {
@SuppressWarnings("null")
AuditLog auditRecord = new AuditLog(action,entity.getLogDeatil()
, new Date(),entity.getId(), entity.getClass().toString(),loingEmpId);
tempSession.save(auditRecord);
tempSession.flush();
} finally {
tempSession.close();
}
}
}
嗯你配置sessionFactory的方式看起來不太合適,你是在描述某個地方的驅動類名嗎?或者至少是最初的數據源?因爲休眠,將啓動之間映射的POJO和相應的表之間執行一些驗證,從您的配置不會發生,因爲它沒有正確的連接 – AntJavaDev