Hibernate入门教程 Hibernate关系映射详解

Hibernate关系映射是1对1one-to-one。

1对1的关系在现实中很常见。比方说:人和身份证。1个身份证对应着一个身份证,一个身份证对应着一个人。那么,我们就以此为原型。进行代码编写。

建立实体模型如右:

 

根据模型,创建数据库:

    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

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



     
      

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

今天讲的是one-to-one配置。但是,此处用的是many-to-one,这个是什么原因呢?其实,one-to-one就是特殊的many-to-one。

Card.hbm.xml:

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





   

#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成功使用的十点心得

网页题目:Hibernate入门教程 Hibernate关系映射详解
网页地址:http://www.shufengxianlan.com/qtweb/news26/367826.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联