Hibernate annotations 入门
来源:互联网 发布:网址制作软件 编辑:程序博客网 时间:2024/05/01 03:02
首先确保你的classpath中加入了 hibernate-annotations.jar ejb3-persistence.jar hibernate-commons-annotations.jar以及hibernate的其他jar包;
hibernate.cfg.xml 的内容:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/db
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="annotations.Flight" />
</session-factory>
</hibernate-configuration>
我们的要映射的实体类: Flight
/*
************************************************************
*本文件归属于软件有限公司,未经许可,不得擅自使用、拷贝和修改
*项目组:ESB小组
*版 本: SychroESB1.0
*日 期:2007-11-20
*时 间:上午08:22:50
*作 者:Administrator
***********************************************************
*/
package annotations;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;
@Entity
public class Flight implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
Long id;
String name;
int age;
// @Lob
//
@Transient
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Basic(fetch = FetchType.LAZY)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
helper类: HibernateUtil
/*
************************************************************
*本文件归属于软件有限公司,未经许可,不得擅自使用、拷贝和修改
*项目组:ESB小组
*版 本: SychroESB1.0
*日 期:2007-11-20
*时 间:上午08:31:43
*作 者:Administrator
***********************************************************
*/
package annotations;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure(
"hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
// Log exception!
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
}
}
测试类:
package annotations;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class Main {
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception {
long st = System.currentTimeMillis();
Session s = HibernateUtil.getSession();
s.beginTransaction();
Flight f=new Flight();
f.setId(new Long(100));
f.setName("n");
s.save(f);
s.flush();
s.getTransaction().commit();
s.close();
long end = System.currentTimeMillis();
System.out.println("用时: ");
System.out.print(end - st);
}
}
- Hibernate annotations 入门
- Hibernate 3 Annotations 入门
- hibernate annotations
- hibernate annotations
- hibernate annotations
- Hibernate Annotations
- Hibernate Annotations
- Hibernate Annotations
- Hibernate Annotations
- Hibernate annotations
- Hibernate Annotations Reference Guide
- Hibernate 3 Annotations 进阶
- Hibernate 3 Annotations 进阶
- hibernate-annotations总结
- hibernate annotations讲解
- Hibernate annotations 配置
- Hibernate Annotations--实体Bean
- Hibernate Annotations 注解
- SqlHelper:
- 指定下标删除 vector 中的元素
- 回帖参与国是:鼠标和键盘就是手中仅有的一点权力
- 中文汉字到URL编码的转换
- vs 2005 恢复初始设置
- Hibernate annotations 入门
- PHPer如何找工作
- ASP.NET中引用dll“找不到指定模块"的完美解决办法 -=--转
- 美伊战争小学版
- OLEDB SqlHelper
- 遍历页面上的所有控件
- 对于javascript的function的总结
- ASP.NET AJAX入门系列(1):概述
- Visual Studio 2008英文正式版已经发布在MSDN