[翻版]学习Hibernate with Annotations第一个实例

[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手册。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值