打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Hibernate 5.0.2加载hibernate.cfg.xml时mapping不生效
【鱼干注:在本人Hibernate项目上测试,方法完全可行。需要注意的是,此博主在HibernateUtil类上:Configuration cfg = new Configuration().configure();
并没有引入类的映射文件,而是加载了映射类反射方法。同时测试了加类的映射文件,发现都可以正确执行,使用Hibernate版本为
5.2.7。下面放上我和博主的代码
进行对比,经测试两种写法都是可行。】

【鱼干部分测试代码:
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
configuration.addClass(Student.class);


【博主部分测试代码:

Configuration cfg = new Configuration().configure(); //cfg.addClass(Group.class);//如果是5.0.2必须得有这句,否则就org.hibernate.MappingException: Unknown entity:




//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();    }}


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Hibernate4.3.x Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
hibernate入门案例代码的优化
Hibernate中SessionFactory配置
Hibernate Shards 数据的水平、垂直切割(二)- Hibernate Shards基本演示
Hibernate工作原理
Hibernate4实战之第一部分Hibernate入门
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服