Hibernate4 比hibernate3 有很大的改变,学习官网文档入门实例如下:
1. 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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/docsearch?useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="hibernate.connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="hibernate/hbm/Document.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2. Document.java
public class Document implements Serializable {
private Integer docId;
private String docName;
private Byte docType;
private Date addDatetime;
private Date modifyDatetime;
private String docLocation;
private String uploadAuthor;
private String anthor;
public Document() {}
public Integer getDocId() {
return docId;
}
public void setDocId(Integer docId) {
this.docId = docId;
}
public String getDocName() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
public Byte getDocType() {
return docType;
}
public void setDocType(Byte docType) {
this.docType = docType;
}
public Date getAddDatetime() {
return addDatetime;
}
public void setAddDatetime(Date addDatetime) {
this.addDatetime = addDatetime;
}
public Date getModifyDatetime() {
return modifyDatetime;
}
public void setModifyDatetime(Date modifyDatetime) {
this.modifyDatetime = modifyDatetime;
}
public String getDocLocation() {
return docLocation;
}
public void setDocLocation(String docLocation) {
this.docLocation = docLocation;
}
public String getUploadAuthor() {
return uploadAuthor;
}
public void setUploadAuthor(String uploadAuthor) {
this.uploadAuthor = uploadAuthor;
}
public String getAnthor() {
return anthor;
}
public void setAnthor(String anthor) {
this.anthor = anthor;
}
@Override
public String toString() {
return "Document [docId=" + docId + ", docName=" + docName
+ ", docType=" + docType + ", addDatetime=" + addDatetime
+ ", modifyDatetime=" + modifyDatetime + ", docLocation="
+ docLocation + ", uploadAuthor=" + uploadAuthor + ", anthor="
+ anthor + "]";
}
}
3. 实例
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
SessionFactory sessionFactory = null;
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration().configure("hibernate/hibernate.cfg.xml");
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
StandardServiceRegistryImpl registry = (StandardServiceRegistryImpl) builder
.build();
sessionFactory = configuration.buildSessionFactory(registry);
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
return sessionFactory;
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Document document = (Document) session.get(Document.class, new Integer(1));
System.out.println(document);
session.getTransaction().commit();
}
}
分享到:
相关推荐
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端...
SpringMVC+Spring3.2.8+Hibernate4.3.5带简单的分页实例,集成时使用hibernate-core-4.3.5.Final.jar时会报错, (网上说该Jar包中某个类的包路径改了,Spring找不到,所以换成了Hibernate4.2 的Core包了)这是BUG么...
hibernate官网提供的下载地址无法下载。。 为了方便大家,转在csdn一份。... hibernate-release-4.3.5.Final 注意: 由于CSDN限制只能上传60M的文件, 这个里面【不包含文档】,需要文档的话请到我的另一个资源中下载
从官网上下的hibernate4.3.5的jar包以及文档api参考
hibernate4.3.5references chm文档
自己整理的hibernate4.3.5 core参考文档,详细的开发文档,chm格式,看着更加方便
hibernate-release-4.3.5.Final包含jar包、相关的api
hibernate4.3.5 + Hibernate-EHCache-4.3.5+C3P0.0.9+oracle 10g测试用例 http://blog.csdn.net/alanchen520/article/details/34072353
一下午才整合成功,书中的bulidSessionFactory()方法在hibernate4.3.5中已经deprecated,这里面有方法实现,用完请给好评。
目前最新版本的S2SH框架(struts2.3.16.3+spring4.0.4+hibernate4.3.5)的搭建DEMO,里面的配置齐全,xml都有配置demo,lib包下为整合的最小jar包,需要其他功能的自行添加,myeclipse项目,MVC三层都有示例class,可以直接...
这是目前最新的Spring库文件,以及Hibernate库文件,并提供DBCP2为数据源,加以Pool2。
http://blog.csdn.net/alanchen520/article/details/34424577
需要整合SSH框架最新jar包的朋友可以下载
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端...
本代码包含了hibernate 的一对多的双向关联,实现级联保存,并整合了Hibernate的过滤器功能、c30的数据库连接池功能。
hibernate-release-4.3.5.Final,权限不足,分两次传
hibernate-release-4.3.5.Final的英文帮助文档
hibernate-release-4.3.5.Final.zip 最新 稳定 最终版
hibernate-4.3.5 与Spring(Spring MVC4.0.4)注解方式集成 样例源码