六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 789|回复: 0

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

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-30 02:11:18 | 显示全部楼层 |阅读模式
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;}}

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();     }}

hibernate.cfg.xml配置内容:
<?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>
<property name="hibernate.hbm2ddl.auto">update</property>其中update表示加载hibernate自动更新数据库结构,其它选项参考hibernate手册。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表