myeclipse中报错 Initial SessionFactory creation failed.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.menglei.shili.Categroy"/>[/color]
这是我的hibernate.cfg.xml中的代码<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mysql</property>
<property name="connection.username">root</property>
<property name="connection.password">meng</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create </property>
<property name="current_session_context_class">thread</property>
<!--这是实例 -->
<mapping resource="com.menglei.shili/msg.hbm.xml" />
<mapping resource="com.menglei.shili/categroy.hbm.xml" />
<mapping resource="com.menglei.shili/topic.hbm.xml" /> </session-factory></hibernate-configuration>
我用 Annotation还能生成表呢,但是我改用xxx.hbm.xml就出错误,下面是sessionfactory的代码
package com.menglei.HibernateUtil;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
// private static Configuration configuration = new AnnotationConfiguration();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; static
{
try
{
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e)
{
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory()
{
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen())
{
if (sessionFactory == null)
{
System.out.println("session没有建成功");
rebuildSessionFactory();
} if (sessionFactory != null)
{
System.out.println("session建成功");
}
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);
} return session;
}
public static void rebuildSessionFactory()
{
try
{
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e)
{
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException
{
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null)
{
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory()
{
return sessionFactory;
}
public static void setConfigFile(String configFile)
{
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration()
{
return configuration;
}}
我测试了一下 session老是空的不知道为什么?能不能说下QQ号啊 方便学习
这是我的hibernate.cfg.xml中的代码<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/mysql</property>
<property name="connection.username">root</property>
<property name="connection.password">meng</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create </property>
<property name="current_session_context_class">thread</property>
<!--这是实例 -->
<mapping resource="com.menglei.shili/msg.hbm.xml" />
<mapping resource="com.menglei.shili/categroy.hbm.xml" />
<mapping resource="com.menglei.shili/topic.hbm.xml" /> </session-factory></hibernate-configuration>
我用 Annotation还能生成表呢,但是我改用xxx.hbm.xml就出错误,下面是sessionfactory的代码
package com.menglei.HibernateUtil;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
// private static Configuration configuration = new AnnotationConfiguration();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; static
{
try
{
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e)
{
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory()
{
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen())
{
if (sessionFactory == null)
{
System.out.println("session没有建成功");
rebuildSessionFactory();
} if (sessionFactory != null)
{
System.out.println("session建成功");
}
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);
} return session;
}
public static void rebuildSessionFactory()
{
try
{
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e)
{
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException
{
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null)
{
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory()
{
return sessionFactory;
}
public static void setConfigFile(String configFile)
{
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration()
{
return configuration;
}}
我测试了一下 session老是空的不知道为什么?能不能说下QQ号啊 方便学习
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货