成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Hibernate入門教程 Hibernate關系映射詳解

開發 后端
Hibernate的關系映射是1對1的關系。本文將結合具體的實例代碼,向您介紹Hibernate關系映射中的1對1關系

Hibernate關系映射是1對1one-to-one。

1對1的關系在現實中很常見。比方說:人和身份證。1個身份證對應著一個身份證,一個身份證對應著一個人。那么,我們就以此為原型。進行代碼編寫。

建立實體模型如右:

Hibernate教程 
根據模型,創建數據庫:
    useHibernateQuickUse;
droptableifexistsPerson;
droptableifexistsCard;
createtableCard(
idvarchar(32)primarykey,
cardDescvarchar(128)notnull
);
createtablePerson(
idvarchar(32)primarykey,
namevarchar(32)notnull,
card_idvarchar(32)notnull,
foreignkey(card_id)referencesCard(id)
);


Java代碼如下:

Person類

   packageorg.py.hib.relation.one2one;
/**
*Personentity.
*/
@SuppressWarnings("serial")
publicclassPersonimplementsjava.io.Serializable
{
privateStringid;
privateStringname;
privateCardcard;
publicPerson()
{
}
publicStringgetId()
{
returnthis.id;
}
publicvoidsetId(Stringid)
{
this.id=id;
}
publicCardgetCard()
{
returnthis.card;
}
publicvoidsetCard(Cardcard)
{
this.card=card;
}
publicStringgetName()
{
returnthis.name;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
}

Card類:
    packageorg.py.hib.relation.one2one;
/**
*Cardentity.
*/
@SuppressWarnings("serial")
publicclassCardimplementsjava.io.Serializable
{
privateStringid;
privateStringcardDesc;
publicCard()
{
}
publicStringgetId()
{
returnthis.id;
}
publicvoidsetId(Stringid)
{
this.id=id;
}
publicStringgetCardDesc()
{
returncardDesc;
}
publicvoidsetCardDesc(StringcardDesc)
{
this.cardDesc=cardDesc;
}
}


XML映射文件如下:

Person.hbm.xml

   
"


cascade="all"column="card_id"/>


今天講的是one-to-one配置。但是,此處用的是many-to-one,這個是什么原因呢?其實,one-to-one就是特殊的many-to-one。

Card.hbm.xml:

   
"

 

#p#

測試代碼如下:

One2OneTest.java

    packageorg.py.hib.relation.one2one;
importjunit.framework.Assert;
importjunit.framework.TestCase;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuration;
importorg.junit.After;
importorg.junit.Before;
publicclassOne2OneTestextendsTestCase
{
privateSessionFactoryfactory;
privateStringm_name="ryanpoy";
privateStringm_name2="ryanpoy2";
privateStringm_cardDesc1="desc_1";
privateStringm_cardDesc2="desc_2";
@Before
publicvoidsetUp()throwsException
{
Configurationconf=newConfiguration().configure();
factory=conf.buildSessionFactory();
}
/**
*測試添加
*@throwsException
*/
publicvoidtestSave()throwsException
{
System.out.println("\n===testsave===");
Cardcard=newCard();
card.setCardDesc(m_cardDesc1);
Personperson=newPerson();
person.setName(m_name);//設置用戶名=m_name
person.setCard(card);
Sessionsession=null;
Transactiontran=null;
try
{
session=factory.openSession();
tran=session.beginTransaction();
session.save(person);
tran.commit();
Assert.assertEquals(person.getId()!=null,true);
Assert.assertEquals(card.getId()!=null,true);
}catch(Exceptionex)
{
tran.rollback();
throwex;
}finally
{
if(session!=null)
{
try
{
session.close();
}catch(Exceptionex)
{
//nothingtodo
}finally
{
if(session!=null)
session=null;
}
}
}
}
/**
*測試查詢
*@throwsException
*/
publicvoidtestFind()throwsException
{
System.out.println("\n===testfind===");
Sessionsession=null;
try
{
session=factory.openSession();
Personperson=(Person)session.createQuery("fromPerson").list().get(0);
Assert.assertEquals(true,person.getId()!=null);
Assert.assertEquals(m_name,person.getName());
Assert.assertEquals(true,person.getCard().getId()!=null);
Assert.assertEquals(m_cardDesc1,person.getCard().getCardDesc());
}catch(Exceptionex)
{
throwex;
}finally
{
if(session!=null)
{
try
{
session.close();
}catch(Exceptionex)
{
//nothingtodo
}finally
{
if(session!=null)
session=null;
}
}
}
}
/**
*測試修改
*@throwsException
*/
publicvoidtestModify()throwsException
{
System.out.println("\n===testmodify===");
Sessionsession=null;
Transactiontran=null;
try
{
session=factory.openSession();
tran=session.beginTransaction();
Personperson=(Person)session.createQuery("fromPerson").list().get(0);
person.setName(m_name2);//修改用戶名=m_name2.(原來用戶名=m_name)
person.getCard().setCardDesc(m_cardDesc2);//修改cardDesc為m_cardDesc2(原來是:m_cardDesc1)
tran.commit();
}catch(Exceptionex)
{
throwex;
}finally
{
if(session!=null)
{
try
{
session.close();
}catch(Exceptionex)
{
//nothingtodo
}finally
{
if(session!=null)
session=null;
}
}
}
/*
*修改后再查詢
*/
System.out.println("\n===testfindaftermodify===");
try
{
session=factory.openSession();
Personperson=(Person)session.createQuery("fromPerson").list().get(0);
Assert.assertEquals(true,person.getId()!=null);
Assert.assertEquals(m_name2,person.getName());
Assert.assertEquals(true,person.getCard().getId()!=null);
Assert.assertEquals(m_cardDesc2,person.getCard().getCardDesc());
}catch(Exceptionex)
{
throwex;
}finally
{
if(session!=null)
{
try
{
session.close();
}catch(Exceptionex)
{
//nothingtodo
}finally
{
if(session!=null)
session=null;
}
}
}
}
/**
*測試刪除
*@throwsException
*/
publicvoidtestDelete()throwsException
{
System.out.println("\n===testdelete===");
Sessionsession=null;
Transactiontran=null;
try
{
session=factory.openSession();
tran=session.beginTransaction();
Personperson=(Person)session.createQuery("fromPerson").list().get(0);
session.delete(person);
tran.commit();
}catch(Exceptionex)
{
throwex;
}finally
{
if(session!=null)
{
try
{
session.close();
}catch(Exceptionex)
{
//nothingtodo
}finally
{
if(session!=null)
session=null;
}
}
}
/*
*刪除后再查詢
*/
System.out.println("\n===testfindafterdelete===");
try
{
session=factory.openSession();
Integernum=(Integer)session.createQuery("fromPerson").list().size();
Assert.assertEquals(0,num.intValue());
num=(Integer)session.createQuery("fromCard").list().size();
Assert.assertEquals(0,num.intValue());
}catch(Exceptionex)
{
throwex;
}finally
{
if(session!=null)
{
try
{
session.close();
}catch(Exceptionex)
{
//nothingtodo
}finally
{
if(session!=null)
session=null;
}
}
}
}
/**
*
*/
@After
publicvoidtearDown()throwsException
{
factory.close();
}
}


運行test,測試成功.

在Hibernateone-to-one關系映射中,其實還有一種方式,即:唯一主見關聯。但是,我一直傾向于上面的這種形式,所以,唯一主見關聯的舊部介紹了。

您正在閱讀: Hibernate入門教程 Hibernate關系映射詳解

【編輯推薦】

  1. Hibernate單元測試的方法:HSQLDB
  2. Hibernate的兩種配置文件格式
  3. Hibernate/JPA成功使用的十點心得
責任編輯:張攀 來源: 教程資料網
相關推薦

2009-09-23 13:26:10

Hibernate對象

2009-09-25 12:59:52

Hibernate映射

2009-06-18 14:22:06

Hibernate多對Hibernate

2012-02-08 12:17:38

HibernateJava

2012-02-02 16:13:29

HibernateJava

2009-09-22 15:10:22

Hibernate映射

2009-09-23 17:34:18

Hibernate映射

2009-09-25 10:00:47

Hibernate映射

2012-02-03 11:17:33

HibernateJava

2012-02-03 10:07:04

HibernateJava

2012-05-30 15:03:43

ibmdw

2012-02-08 13:34:08

HibernateJava

2009-09-21 17:33:50

Hibernate基礎

2009-09-24 17:24:20

Hibernate S

2012-02-03 10:54:50

HibernateJava

2009-07-02 09:40:14

Hibernate的繼

2009-09-25 12:31:13

Hibernate映射

2009-09-25 14:20:28

Hibernate繼承映射

2009-09-25 09:46:02

Hibernate高級

2009-09-27 10:02:29

定制Hibernate
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久草免费视 | 亚洲精品中文字幕 | 日韩一二三区视频 | 狠狠的操 | 国产免费福利小视频 | 日韩中文字幕免费 | 日韩高清国产一区在线 | 国产成人在线一区二区 | 国产精品入口麻豆www | 日韩亚洲欧美一区 | 日本在线播放一区二区 | 欧美黄页 | 亚洲美女网站 | 国产精品178页 | 精品亚洲一区二区三区 | 中文二区 | 三极网站 | 午夜影院在线免费观看视频 | 色资源在线视频 | 日韩播放 | 免费精品在线视频 | 在线成人免费视频 | www.99热这里只有精品 | 99久久精品一区二区毛片吞精 | 91精品中文字幕一区二区三区 | 黄色大片网 | 99精品一区二区三区 | 亚洲国产成人av好男人在线观看 | 中文字幕日韩欧美一区二区三区 | 午夜99| 日本欧美国产在线 | 亚洲一区二区三区视频 | 欧美综合国产精品久久丁香 | 日韩精品在线免费 | 一级毛片观看 | 7777精品伊人久久精品影视 | 97人人草| 红桃视频一区二区三区免费 | 色婷婷精品国产一区二区三区 | 久久一区二区三区四区 | 中文字幕在线一区二区三区 |