Hibernate 5.0.2加载hibernate.cfg.xml时mapping不生效

//Group类
package com.jingtai;

public class Group {
    private int groupId;
    private String groupName;
    
    public void setGroupId(int id){
        groupId = id;
    }
    public int getGroupId(){
        return groupId;
    }
    
    public void setGroupName(String name){
        groupName = name;
    }
    public String getGroupName(){
        return groupName;
    }
}

Group.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 2015-10-2 19:20:37 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.test.Group" table="t_group">
        <id name="groupId" type="int">
            <column name="n_group_id" />
            <generator class="identity" />
        </id>
        <property name="groupName" type="java.lang.String">
            <column name="c_group_name" />
        </property>
    </class>
</hibernate-mapping>

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">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
       
        <property name="hibernate.connection.url">jdbc:sqlserver://192.168.1.101:1433;DatabaseName=db_test;SelectMethod=cursor</property>
        
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.connection.password">password</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
         <property name="myeclipse.connection.profile">sqlserver</property>
         
        <mapping resource="com/test/Group.hbm.xml" />
    </session-factory>
</hibernate-configuration>

HibernateUtil:

public class HibernateUtil
{
    private static final SessionFactory sessionFactory;
    static
    {
        try
        {
            Configuration cfg = new Configuration().configure();
            //cfg.addClass(Group.class);//如果是5.0.2必须得有这句,否则就org.hibernate.MappingException: Unknown entity:
            
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
            sessionFactory = cfg.buildSessionFactory(serviceRegistry);
        }
        catch (Throwable e)
        {
            throw new ExceptionInInitializerError(e);
        }
        
    }
    private HibernateUtil()
    {
    }
    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }
}

测试程序:

import org.hibernate.Session;

import com.jingtai.hibernate.util.HibernateUtil;

import jingtai.Group;

public class App {

    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Group group = new Group();
        group.setGroupName("admin");
        session.save(group);
        session.getTransaction().commit();
    }

}


你可能感兴趣的:(Hibernate 5.0.2加载hibernate.cfg.xml时mapping不生效)