[code]package test.hibernate.annotations;
import javax.persistence.*;
/**
* @description
* @author Jason.T
* @project Dowork
* @date 2007-4-5
*/
@Entity
@Table(name="person") // name不指定表示类与表同名
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Integer personId;
@Column(name="name")
private String name;
@Column(name="sex")
private Boolean sex;
@Column(name="age")
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public Integer getPersonId() {
return personId;
}
public Boolean getSex() {
return sex;
}
public void setName(String name) {
this.name = name;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
}[/code]
[code]package test.hibernate.annotations;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* @description
* @author Jason.T
* @project Dowork
* @date 2007-4-5
*/
public class HibernateAnnotationDemo {
public static void main(String[] args) {
Configuration config = new AnnotationConfiguration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Person person = new Person();
person.setAge(new Integer(25));
person.setName("jason");
person.setSex(new Boolean(true));
Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
session.save(person);
tx.commit();
session.close();
sessionFactory.close();
}
}[/code]
hibernate.cfg.xml配置内容:
[code]<?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>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.InformixDialect</property>
<property name="connection.driver_class">com.informix.jdbc.IfxDriver</property>
<property name="connection.url">
jdbc:informix-sqli://jians:3333/jasun:INFORMIXSERVER=cibcs;IFX_LOCK_MODE_WAIT=30;DB_LOCALE=zh_cn.gb
</property>
<property name="connection.username">informix</property>
<property name="connection.password">cloud123</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 以下设置对象与数据库表格映像类别 -->
<mapping class="test.hibernate.annotations.Person"/>
</session-factory>
</hibernate-configuration>[/code]
<property name="hibernate.hbm2ddl.auto">update</property>其中update表示加载hibernate自动更新数据库结构,其它选项参考hibernate手册。
import javax.persistence.*;
/**
* @description
* @author Jason.T
* @project Dowork
* @date 2007-4-5
*/
@Entity
@Table(name="person") // name不指定表示类与表同名
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private Integer personId;
@Column(name="name")
private String name;
@Column(name="sex")
private Boolean sex;
@Column(name="age")
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public Integer getPersonId() {
return personId;
}
public Boolean getSex() {
return sex;
}
public void setName(String name) {
this.name = name;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
}[/code]
[code]package test.hibernate.annotations;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* @description
* @author Jason.T
* @project Dowork
* @date 2007-4-5
*/
public class HibernateAnnotationDemo {
public static void main(String[] args) {
Configuration config = new AnnotationConfiguration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Person person = new Person();
person.setAge(new Integer(25));
person.setName("jason");
person.setSex(new Boolean(true));
Session session = sessionFactory.openSession();
Transaction tx= session.beginTransaction();
session.save(person);
tx.commit();
session.close();
sessionFactory.close();
}
}[/code]
hibernate.cfg.xml配置内容:
[code]<?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>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.InformixDialect</property>
<property name="connection.driver_class">com.informix.jdbc.IfxDriver</property>
<property name="connection.url">
jdbc:informix-sqli://jians:3333/jasun:INFORMIXSERVER=cibcs;IFX_LOCK_MODE_WAIT=30;DB_LOCALE=zh_cn.gb
</property>
<property name="connection.username">informix</property>
<property name="connection.password">cloud123</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 以下设置对象与数据库表格映像类别 -->
<mapping class="test.hibernate.annotations.Person"/>
</session-factory>
</hibernate-configuration>[/code]
<property name="hibernate.hbm2ddl.auto">update</property>其中update表示加载hibernate自动更新数据库结构,其它选项参考hibernate手册。
本文介绍了一个使用Hibernate注解实现的简单示例,包括实体类定义、配置文件详解及核心代码展示。

被折叠的 条评论
为什么被折叠?



