<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Vincent

    Vicent's blog
    隨筆 - 74, 文章 - 0, 評論 - 5, 引用 - 0
    數據加載中……

    Hibernate Annotations 實戰

    任何獲得Matrix授權的網站,轉載請保留以下作者信息和鏈接:
    作者:icess(作者的blog:http://blog.matrix.org.cn/page/icess)
    關鍵字:Hibernate Validator

    下面讓我們先看一個通常用 hbm.xml 映射文件的例子. 有3個類 .HibernateUtil.java 也就是 Hibernate文檔中推薦的工具類,Person.java 一個持久化的類, Test.java 測試用的類.都在test.hibernate 包中. 每個類的代碼如下:

    01?package?test.hibernate;
    02?
    03?import?org.hibernate.HibernateException;
    04?import?org.hibernate.Session;
    05?import?org.hibernate.SessionFactory;
    06?import?org.hibernate.cfg.Configuration;
    07?
    08?public?class?HibernateUtil?{
    09???public?static?final?SessionFactory?sessionFactory;
    10???
    11???static?{
    12?????try?{
    13???????sessionFactory?=?new?Configuration()
    14???????????????.configure()
    15???????????????.buildSessionFactory();
    16?????}?catch?(HibernateException?e)?{
    17???????//?TODO?Auto-generated?catch?block
    18???????
    19???????e.printStackTrace();
    20???????throw?new?ExceptionInInitializerError(e);
    21?????}
    22???}
    23???
    24???public?static?final?ThreadLocal<Session>?session?=?new?ThreadLocal<Session>();
    25???
    26???public?static?Session?currentSession()?throws?HibernateException?{
    27?????Session?s?=?session.get();
    28?????
    29?????if(s?==?null)?{
    30???????s?=?sessionFactory.openSession();
    31???????session.set(s);
    32?????}
    33?????
    34?????return?s;
    35???}
    36???
    37???public?static?void?closeSession()?throws?HibernateException?{
    38?????Session?s?=?session.get();
    39?????if(s?!=?null)?{
    40???????s.close();
    41?????}
    42?????session.set(null);
    43???}
    44?}

     

    01?package?test.hibernate;
    02?
    03?import?java.util.LinkedList;
    04?import?java.util.List;
    05?
    06?/**
    07??*?
    08??*/
    09?
    10?@SuppressWarnings("serial")
    11?public?class?Person?implements?java.io.Serializable?{
    12?
    13???//?Fields
    14?
    15???private?Integer?id;
    16?
    17???private?String?name;
    18?
    19???private?String?sex;
    20?
    21???private?Integer?age;
    22?
    23???private?List?list?=?new?LinkedList();
    24?
    25???//?Collection?accessors
    26?
    27???public?List?getList()?{
    28?????return?list;
    29???}
    30?
    31???public?void?setList(List?list)?{
    32?????this.list?=?list;
    33???}
    34?
    35???/**?default?constructor?*/
    36???public?Person()?{
    37???}
    38?
    39???/**?constructor?with?id?*/
    40???public?Person(Integer?id)?{
    41?????this.id?=?id;
    42???}
    43?
    44???//?Property?accessors
    45?
    46???public?Integer?getId()?{
    47?????return?this.id;
    48???}
    49?
    50???public?void?setId(Integer?id)?{
    51?????this.id?=?id;
    52???}
    53?
    54???public?String?getName()?{
    55?????return?this.name;
    56???}
    57?
    58???public?void?setName(String?name)?{
    59?????this.name?=?name;
    60???}
    61?
    62???public?String?getSex()?{
    63?????return?this.sex;
    64???}
    65?
    66???public?void?setSex(String?sex)?{
    67?????this.sex?=?sex;
    68???}
    69?
    70???public?Integer?getAge()?{
    71?????return?this.age;
    72???}
    73?
    74???public?void?setAge(Integer?age)?{
    75?????this.age?=?age;
    76???}
    77?
    78?}

     

    01?/*
    02??*?Created?on?
    03??*?@author?
    04??*/
    05?package?test.hibernate;
    06?
    07?import?java.sql.SQLException;
    08?
    09?import?org.hibernate.FlushMode;
    10?import?org.hibernate.HibernateException;
    11?import?org.hibernate.Session;
    12?import?org.hibernate.Transaction;
    13?
    14?public?class?Test?{
    15???
    16???public?static?void?main(String?[]?args)?{
    17?????Session?s?=?HibernateUtil.currentSession();
    18?????
    19?????Transaction?tx?=?s.beginTransaction();????
    20?????
    21?//????Person?p?=?(Person)?s.load(Person.class,?1);
    22?//????System.out.println(p.getName());
    23?????Person?p?=?new?Person();
    24?????
    25?????p.setAge(19);
    26?????p.setName("icerain");
    27?????p.setSex("male");
    28?????s.save(p);
    29?????s.flush();
    30?????/*
    31?????Person?p2?=?(Person)?s.get(Person.class,?new?Integer(1));
    32?????System.out.println(p2.getName());
    33?????p2.setName("ice..");
    34?????s.saveOrUpdate(p2);
    35?????s.flush();
    36?????Person?p3?=?(Person)?s.get(Person.class,?new?Integer(2));
    37?????System.out.println(p3.getName());
    38?????s.delete(p3);
    39?????*/
    40?????
    41?????tx.commit();??
    42?????try?{
    43???????System.out.println(p.getName());
    44?????}?catch?(Exception?e)?{
    45???????//?TODO?Auto-generated?catch?block
    46???????e.printStackTrace();
    47?????}
    48?????
    49?????HibernateUtil.closeSession();
    50???}
    51?}

    hibernate.cfg.xml 配置文件如下,利用mysql 數據庫.

    <? 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 = "hibernate.connection.driver_class" > org.gjt.mm.mysql.Driver </ property >

    < property name = "hibernate.connection.password" >你的數據庫密碼</ property >

    < property name = "hibernate.connection.url" > jdbc:mysql://localhost/數據庫名 </ property >

    < property name = "hibernate.connection.username" >用戶名</ property >

    < property name = "hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ property >

    < property name = "show_sql" > true </ property >

    < property name = "hibernate.transaction.factory_class" > org.hibernate.transaction.JDBCTransactionFactory </ property >

    < property name = "hibernate.transaction.auto_close_session" > false </ property >

    <property name="hibernate.hbm2ddl.auto">update</property>

    < mapping resource = "test/hibernate/annotation/Person.hbm.xml" />

    </ session-factory >

    </ hibernate-configuration >

    其中 配置了<property name="hibernate.hbm2ddl.auto">update</property>屬性 自動導入數據庫ddl.生產的ddl sql語句如下

    create table person (id integer not null auto_increment, name varchar(255), sex varchar(255), age integer, person integer, primary key (id))

    alter table person add index FKC4E39B5511C4A5C2 (person), add constraint FKC4E39B5511C4A5C2 foreign key (person) references person (id)

    而Person.hbm.xml 文件如下:

    <? xml version = "1.0" ?>

    <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

    < hibernate-mapping >

    < class name = "test.hibernate.Person" table = "person" >

    < id name = "id" type = "integer" >

    < column name = "id" />

    < generator class = "native" ></ generator >

    </ id >

    < property name = "name" type = "string" >

    < column name = "name" />

    </ property >

    < property name = "sex" type = "string" >

    < column name = "sex" />

    </ property >

    < property name = "age" type = "integer" >

    < column name = "age" />

    </ property >

    < bag name = "list" cascade = "all" >

    < key column = "person" ></ key >

    < one-to-many class = "test.hibernate.Person" />

    </ bag >

    </ class >

    </ hibernate-mapping >

    下面讓我們看看利用 Hibernate Annotations 如何做,只要三個類 不再需要 hbm.xml配置文件:

    還要把用到的兩個jar文件 放入的類路徑中. 具體如何做,請參考? Hibernate Annotations 中文文檔

    http://hb.6644.net

    HibernateUtil.java 也就是 Hibernate文檔中推薦的工具類,Person.java 一個持久化的類, Test.java 測試用的類.都在test.hibernate.annotation 包中. 每個類的代碼如下:

    01?package?test.hibernate.annotation;
    02?
    03?import?org.hibernate.HibernateException;
    04?import?org.hibernate.Session;
    05?import?org.hibernate.SessionFactory;
    06?import?org.hibernate.cfg.AnnotationConfiguration;
    07?import?org.hibernate.cfg.Configuration;
    08?
    09?public?class?HibernateUtil?{
    10???public?static?final?SessionFactory?sessionFactory;
    11???
    12???static?{
    13?????try?{
    14???????sessionFactory?=?new?AnnotationConfiguration()?? //注意: 建立 SessionFactory于前面的不同
    15?????????????????.addPackage("test.hibernate.annotation")
    16?????????????????.addAnnotatedClass(Person.class)
    17?????????????????
    18?????????????????.configure()
    19?????????????????.buildSessionFactory();
    20?????????//new?Configuration().configure().buildSessionFactory();
    21?????}?catch?(HibernateException?e)?{
    22???????//?TODO?Auto-generated?catch?block
    23???????
    24???????e.printStackTrace();
    25???????throw?new?ExceptionInInitializerError(e);
    26?????}
    27???}
    28???
    29???public?static?final?ThreadLocal<Session>?session?=?new?ThreadLocal<Session>();
    30???
    31???public?static?Session?currentSession()?throws?HibernateException?{
    32?????Session?s?=?session.get();
    33?????
    34?????if(s?==?null)?{
    35???????s?=?sessionFactory.openSession();
    36???????session.set(s);
    37?????}
    38?????
    39?????return?s;
    40???}
    41???
    42???public?static?void?closeSession()?throws?HibernateException?{
    43?????Session?s?=?session.get();
    44?????if(s?!=?null)?{
    45???????s.close();
    46?????}
    47?????session.set(null);
    48???}
    49?}

     

    01?package?test.hibernate.annotation;
    02?
    03?import?java.util.LinkedList;
    04?import?java.util.List;
    05?
    06?import?javax.persistence.AccessType;
    07?import?javax.persistence.Basic;
    08?import?javax.persistence.Entity;
    09?import?javax.persistence.GeneratorType;
    10?import?javax.persistence.Id;
    11?import?javax.persistence.OneToMany;
    12?import?javax.persistence.Table;
    13?import?javax.persistence.Transient;
    14?
    15?/**
    16??*?
    17??*/
    18?
    19?@SuppressWarnings("serial")
    20?@Entity(access?=?AccessType.PROPERTY) //定義該類為實體類
    21?@Table?? //映射表
    22?public?class?Person?implements?java.io.Serializable?{
    23?
    24???//?Fields
    25?
    26???private?Integer?id;
    27?
    28???private?String?name;
    29?
    30???private?String?sex;
    31?
    32???private?Integer?age;
    33?
    34???private?List?list?=?new?LinkedList();
    35?
    36???//?Constructors
    37???/**?default?constructor?*/
    38???public?Person()?{
    39???}
    40?
    41???/**?constructor?with?id?*/
    42???public?Person(Integer?id)?{
    43?????this.id?=?id;
    44???}
    45?
    46???//?Property?accessors
    47???@Id
    48???public?Integer?getId()?{
    49?????return?this.id;
    50???}
    51?
    52???public?void?setId(Integer?id)?{
    53?????this.id?=?id;
    54???}
    55?
    56???@Basic
    57???public?String?getName()?{
    58?????return?this.name;
    59???}
    60?
    61???public?void?setName(String?name)?{
    62?????this.name?=?name;
    63???}
    64?
    65???@Basic
    66???public?String?getSex()?{
    67?????return?this.sex;
    68???}
    69?
    70???public?void?setSex(String?sex)?{
    71?????this.sex?=?sex;
    72???}
    73?
    74???@Basic
    75???public?Integer?getAge()?{
    76?????return?this.age;
    77???}
    78?
    79???public?void?setAge(Integer?age)?{
    80?????this.age?=?age;
    81???}
    82???@Transient? //由于本例不打算演示集合映射 所有聲明該屬性為 Transient?
    83???public?List?getList()?{
    84?????return?list;
    85???}
    86?
    87???public?void?setList(List?list)?{
    88?????this.list?=?list;
    89???}
    90?
    91?}

    注意該實體類中的屬性都使用了默認值.

    Test.java 代碼同上

    不需要了 hbm.xml 映射文件, 是不是簡單了一些 .給人認為簡化了一些不是主要目的.主要是可以了解一下 EJB3 的持久化機制 ,提高一下開發效率才是重要的.

    好了 .本例就完了 . 感覺怎么樣了 .歡迎你來批批.

    PS:

    生成的數據庫表 和 程序執行后的 數據庫情況如下

    mysql> describe person;
    +--------+--------------+------+-----+---------+----------------+
    | Field? | Type???????? | Null | Key | Default |????????? Extra |
    +--------+--------------+------+-----+---------+----------------+
    | id???? | int(11)????? | NO?? | PRI | NULL??? | auto_increment |
    | name?? | varchar(255) | YES? |???? | NULL??? |??????????????? |
    | sex??? | varchar(255) | YES? |???? | NULL??? |??????????????? |
    | age??? | int(11)????? | YES? |???? | NULL??? |??????????????? |
    | person | int(11)????? | YES? | MUL | NULL??? |??????????????? |
    +--------+--------------+------+-----+---------+----------------+
    5 rows in set (0.00 sec)

    mysql> select * from person;
    +----+---------+------+------+--------+
    | id | name??? |? sex |? age | person |
    +----+---------+------+------+--------+
    |? 1 | icerain | male |?? 19 |?? NULL |
    +----+---------+------+------+--------+
    1 row in set (0.03 sec)

    posted on 2006-09-01 13:59 Binary 閱讀(406) 評論(0)  編輯  收藏 所屬分類: Hibernate

    主站蜘蛛池模板: 黄色免费在线网址| 国产激情免费视频在线观看 | 国产精品无码一二区免费 | 午夜成人免费视频| 日本高清不卡中文字幕免费| 亚洲欧洲日韩不卡| 免费观看国产小粉嫩喷水| 免费视频精品一区二区三区 | 亚洲影视自拍揄拍愉拍| 亚洲中文字幕无码专区| 国产片AV片永久免费观看 | 亚洲精品免费视频| 久久亚洲欧美国产精品| 内射少妇36P亚洲区| 亚洲国产午夜福利在线播放| 综合在线免费视频| 热久久这里是精品6免费观看| 亚洲色图激情文学| 亚洲AV无码1区2区久久| 免费中文字幕在线| 无码人妻一区二区三区免费手机| 中国一级特黄高清免费的大片中国一级黄色片 | 亚洲福利视频一区二区| 成人免费a级毛片| 久热免费在线视频| jizz日本免费| 日韩亚洲综合精品国产| 亚洲丰满熟女一区二区v| 久久91亚洲人成电影网站| 四虎影视在线永久免费看黄| 精品香蕉在线观看免费| 中文字幕乱码一区二区免费| 成年免费a级毛片| 亚洲欧美第一成人网站7777| 亚洲性色高清完整版在线观看| 国产成人精品日本亚洲| 亚洲午夜爱爱香蕉片| 免费国产高清视频| 四虎影院免费视频| 国产又黄又爽又猛免费app| 亚洲精品在线免费看|