org.hibernate.MappingException: Unknown entity: com.text.po.News

凯文_张 发布于 2016/10/11 17:38
阅读 126
收藏 0
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test;

import com.text.po.News;

public class NewsController {

@Test
public void test(){
SessionFactory sf=null;
//Configuration  对应hibernate基本配置信息和对象关系映射信息
Configuration conf=new Configuration().configure();
//5.0之后使用的StandardServiceRegistryBuilder来获取
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
sf =conf.buildSessionFactory(serviceRegistry);
Session session=sf.openSession();
Transaction ta=session.beginTransaction();

News n=new News("kk",20);
session.save(n);
   ta.commit(); 
   session.close();
   sf.close();
}
}

<?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 2016-10-10 23:38:04 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="com.text.po.News" table="NEWS">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="AGE" />
        </property>
    </class>
</hibernate-mapping>

<?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.username">root</property>
    <property name="connection.password">901103</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="connection.username">root</property>
    <property name="format_sql">show</property>
    <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    
    <mapping class="com.text.po.*.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

没有使用注解,cfg中也映入的hbm配置文件
加载中
返回顶部
顶部