hibernate 教程

原理:

http://wenku.baidu.com/view/3f2848eef8c75fbfc77db231.html

 

 

实践:

结合两个例子实践:

 

http://developer.51cto.com/art/200906/126554.htm

http://wenku.baidu.com/view/3182112acfc789eb172dc886.html

 

出的问题:

a.

(转自他人的)

Java代码   收藏代码
  1. <span style= "color: #ff0000;" >org.hibernate.HibernateException:  'hibernate.dialect'  must be set when no Connection available </span>  

 这个问题是比较奇怪的,让人有点郁闷,检查了配置文件,根本没有错,也就说现在问题不是出在这个地方了。而是出现在了其他的地方。网上找了很多,很失望,没有找到正确的。

 

这个时候得看其他的方面。我出现的问题是

 

Java代码   收藏代码
  1. cfg= new  AnnotationConfiguration();  
  2. sf=cfg.buildSessionFactory();  

 

在这里没有写

 

Java代码   收藏代码
  1. 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是大写开头的,就对象而不是表名。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值