原理:
http://wenku.baidu.com/view/3f2848eef8c75fbfc77db231.html
?
?
实践:
结合两个例子实践:
?
http://developer.51cto.com/art/200906/126554.htm
http://wenku.baidu.com/view/3182112acfc789eb172dc886.html
?
出的问题:
a.
(转自他人的)
- <span?style= "color:?#ff0000;" >org.hibernate.HibernateException:? 'hibernate.dialect' ?must?be?set?when?no?Connection?available?</span>??
?这个问题是比较奇怪的,让人有点郁闷,检查了配置文件,根本没有错,也就说现在问题不是出在这个地方了。而是出现在了其他的地方。网上找了很多,很失望,没有找到正确的。
?
这个时候得看其他的方面。我出现的问题是
?
- cfg= new ?AnnotationConfiguration();??
- sf=cfg.buildSessionFactory();??
?
在这里没有写
?
- cfg= new ?AnnotationConfiguration().configure();??
?? ? ??缺少的是configure();
?添加这句话之后程序OK.
?
?
?new Configuration()默认是读取hibernate.properties
?所以使用new Configuration().configure()来读取hibernate.cfg.xml文件
?
?
?
?
?
?
b.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
?
c.
<hibernate-configuration>
??? <session-factory>
??? ??? <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
??????? <property name="hibernate.connection.url">jdbc:mysql://localhost/test
</property>
?????? ??? <property name="hibernate.connection.username">root</property>
?? ??? ??? ?<property name="hibernate.connection.password">1234</property>
??????? <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
??????? <property name="show_sql">true</property>
??????? <mapping resource="com/wshiw/model/User.hbm.xml" />
??? </session-factory>
</hibernate-configuration>
<hibernate-mapping>
??? <class name="com.wshiw.model.User" table="user">
??? ??? <id name="id" type="java.lang.Integer">
??? ??? ??? <column name="id" />
??? ??? ??? <generator class="increment" />
??? ??? </id>
??? ??? <property name="name" column="name" type="java.lang.String" >
??? ??? </property>
??? ??? <property name="age"? column="age" type="java.lang.Integer" >
??? ??? </property>
??? </class>
</hibernate-mapping>
?
?
?
d.
Query query = session.createQuery("from User where username = ? and password = ?");
注意User是大写开头的,就对象而不是表名。
?