【魚干注:在本人Hibernate項目上測試,方法完全可行。需要注意的是,此博主在HibernateUtil類上: Configuration cfg = new Configuration().configure(); 并沒有引入類的映射文件,而是加載了映射類反射方法。同時測試了加類的映射文件,發(fā)現(xiàn)都可以正確執(zhí)行,使用Hibernate版本為 5.2.7。下面放上我和博主的代碼 進行對比,經(jīng)測試兩種寫法都是可行?!?br> 【魚干部分測試代碼: 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./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./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();
}
}
|