Hibernate 4.3.5 入门实例

[b]
Hibernate4 比hibernate3 有很大的改变,学习官网文档入门实例如下:
[/b]
[b]
1. hibernate.cfg.xml 配置文件配
[/b]

<?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>

[b]
2. Document.java
[/b]

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 + "]";
}
}

[b]
3. 实例
[/b]

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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值