hibernate Unknown entity错误,google,baidu后无法解决,求助

朔北冥 学生  2017-02-27 09:28:00
情况如图


代码:
hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=mysql</property>
<property name="connection.username">sa</property>
<property name="connection.password">12345</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="User.hbm.xml"/>
<mapping class="hibernate_test.User"/>
</session-factory>
</hibernate-configuration>

User.hbm.xml 代码:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 24, 2017 9:07:02 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="hibernate_test.User" table="USER">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
</class>
</hibernate-mapping>


HibernateUtil代码

package hibernate_test;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;


public class HibernateUtil {
private static final ThreadLocal<Session> threadlocal=new ThreadLocal<Session>();
private static SessionFactory sessionFactory=null;
private static ServiceRegistry serviceRegistry =null;
static{
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
System.out.println("Hibernate Configuration loaded");

// configuration.addClass(hibernate_test.User.class);
// Read mappings as a application resourceName
// addResource is for add hbml.xml files in case of declarative approach
configuration.addResource("User.hbm.xml"); //

//apply configuration property settings to StandardServiceRegistryBuilder
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
System.out.println("Hibernate serviceRegistry created");

sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
* attain session
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException{
Session session=(Session) threadlocal.get();
if(session==null||!session.isOpen()){
if(session==null){
rebuildSessionFactory();
}
session=(sessionFactory!=null)?sessionFactory.openSession():null;
threadlocal.set(session);
}
System.out.println("***crating session");
return session;
}
/**
* rebuild SessionFactory
*/
public static void rebuildSessionFactory(){
try{
Configuration configuration=new Configuration().configure();

sessionFactory=configuration .buildSessionFactory(serviceRegistry);
}catch(Exception e){
System.err.println("build sessionFactory fail");
e.printStackTrace( );
}
}
/**
* attain object of SessionFactory
* @return object of SessionFactory
*/
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
/**
* close Session
* @throws HibernateException
*/
public static void closeSession() throws HibernateException{
Session session=(Session) threadlocal.get();
threadlocal.set(null);
if(session!=null)
session.close();
}
}


MainTest代码:

package hibernate_test;

import org.hibernate.Session;

public class MainTest {

public static void main(String[] args) {
User user=new User();
user.setId(123);user.setName("Rosand");user.setPassword("admin");
Session session=null;
try{
session=HibernateUtil.getSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
System.out.println("success");
}catch(Exception e){
session.getTransaction().rollback();
System.out.println("fail");
e.printStackTrace( );
}finally{
HibernateUtil.closeSession();
}
}

}


...全文
106 点赞 收藏 4
写回复
4 条回复
朔北冥 2017年03月12日
还是别用最新的5版本。。。。改了许多,连官方教程都是错的。。。。
回复 点赞
一剑侵心 2017年02月28日
我只想说:现在都用注解了,配置文件太烦了
回复 点赞
朔北冥 2017年02月28日
引用 1 楼 pany1209 的回复:
<mapping class="hibernate_test.User"/>这个不用加
删除这句代码还是提示找不到user的实体
回复 点赞
李德胜1995 2017年02月27日
<mapping class="hibernate_test.User"/>这个不用加
回复 点赞
发动态
发帖子
Java EE
创建于2007-09-28

3.7w+

社区成员

22.5w+

社区内容

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区公告
暂无公告