Hibernate学习之二-------搭建环境时应注意的地方



 1.使用的库的版本:Hibernate Core 3.2.4. Myeclipse 7.5中自带的.需要用到的jar包有:

  

不同的版本用到的包会不一样,也有可能出现包冲突的问题。

当使用Hibernate 3.3.2 ga时,将lib下面的required中的jar包和Hibernate.jar加到类路径中时,此时运行是会出现异常:找不到一个类。这是由于还少了一个jar包:slf4j-log4j12-1.5.2.jar(日志处理用到的),把这个包加上去就不抛异常了。

 

2.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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

	<session-factory>  如果有name属性,hibernate会试图把这个sessionfacotry注册到jndi中去,就会出现Could not bind factory to JNDI的错误
  
		<property name="dialect">
			org.hibernate.dialect.SQLServerDialect
		</property>
		<property name="connection.url">
			jdbc:sqlserver://localhost:1433;databaseName=hibernatetest
		</property>
		<property name="connection.username">AUTEK</property>
		<property name="connection.password">FLYVIDEO</property>
		<property name="connection.driver_class">
			com.microsoft.sqlserver.jdbc.SQLServerDriver
		</property>
		<property name="myeclipse.connection.profile">
			MSSQL2005
		</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>注意create和update的区别
		<mapping resource="com/zhsh/hibernate/entity/Message.hbm.xml" />


	</session-factory>

</hibernate-configuration>

 

3.mapping files

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zhsh.hibernate.entity"><!-- 一定不能掉,不然就找不到类了 -->
   <class name="Message" table="message">
     <id name="id" column="id">
        <generator class="native"></generator>
     </id>
     <property name="message"></property>
   </class>
</hibernate-mapping>

 具体属性的配置可以参考:《Hibernate参考文档 3.2》

 

4.POJO类

Each class that will be persisted by Hibernate is required to have a default constructor with at least package scope.They should have get and set methods for all of the attributes that are to be persisted. We will provide each with an id field, allowing this to be the primary key in our database (we prefer the use of surrogate keys, as changes to business rules can make the use of direct keys risky).

POJO类中必须得有一个默认的构造函数,至少是包访问级别的。

 

5.还有一个地方要注意的是:

private static final SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();

并非是new Configuration().buildSessionFactory();   configure()对配置文件进行初始化工作。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值