`

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()对配置文件进行初始化工作。

 

  • 大小: 44.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics