首先确保你的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);
}
}