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()對配置文件進行初始化工作。
?
總結
以上是生活随笔為你收集整理的Hibernate学习之二-------搭建环境时应注意的地方的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zData上新丨2000/5000系列数
- 下一篇: 第50周收录98起融资,国内低靡,国外智