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

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

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

  建立实体模型如右:

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

  根据模型创建数据库:

useHibernateQuickUse;
droptableexistsPerson;
droptableexistsCard;

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")
publicPersonimplementsjava.io.Serializable
{
privateStringid;

privateStringname;

privateCardcard;

publicPerson
{
}

publicStringgetId
{
this.id;
}

publicvoidId(Stringid)
{
this.id=id;
}

publicCardgetCard
{
this.card;
}

publicvoidCard(Cardcard)
{
this.card=card;
}

publicStringgetName
{
this.name;
}

publicvoidName(Stringname)
{
this.name=name;
}

}


  Card类:   packageorg.py.hib.relation.one2one;

/**
*Cardentity.
*/

@SuppressWarnings("serial")
publicCardimplementsjava.io.Serializable
{
privateStringid;

privateStringcardDesc;

publicCard
{
}

publicStringgetId
{
this.id;
}

publicvoidId(Stringid)
{
this.id=id;
}

publicStringgetCardDesc
{
cardDesc;
}

publicvoidCardDesc(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">

  测试代码如下:

  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;

publicOne2OneTestextendsTestCase
{
privateSessionFactoryfactory;

privateStringm_name="ryanpoy";

privateStringm_name2="ryanpoy2";

privateStringm_cardDesc1="desc_1";

privateStringm_cardDesc2="desc_2";

@Before
publicvoidUpthrowsException
{
Configurationconf=Configuration.configure;
factory=conf.buildSessionFactory;
}

/**
*测试添加
*@throwsException
*/
publicvoidtestSavethrowsException
{
.out.prln("n=testsave=");

Cardcard=Card;
card.CardDesc(m_cardDesc1);

Personperson=Person;
person.Name(m_name);//设置用户名=m_name
person.Card(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
{
(session!=null)
{
try
{
session.close;
}catch(Exceptionex)
{
//nothingtodo
}finally
{
(session!=null)
session=null;
}
}
}
}

/**
*测试查询
*@throwsException
*/
publicvoidtestFindthrowsException
{
.out.prln("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
{
(session!=null)
{
try
{
session.close;
}catch(Exceptionex)
{
//nothingtodo
}finally
{
(session!=null)
session=null;
}
}
}
}

/**
*测试修改
*@throwsException
*/
publicvoidtestModythrowsException
{
.out.prln("n=testmody=");
Sessionsession=null;
Transactiontran=null;
try
{
session=factory.openSession;
tran=session.beginTransaction;

Personperson=(Person)session.createQuery("fromPerson").list.get(0);
person.Name(m_name2);//修改用户名=m_name2.(原来用户名=m_name)
person.getCard.CardDesc(m_cardDesc2);//修改cardDesc为m_cardDesc2(原来是:m_cardDesc1)
tran.commit;

}catch(Exceptionex)
{
throwex;
}finally
{
(session!=null)
{
try
{
session.close;
}catch(Exceptionex)
{
//nothingtodo
}finally
{
(session!=null)
session=null;
}
}
}

/*
*修改后再查询
*/
.out.prln("n=testfindaftermody=");
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
{
(session!=null)
{
try
{
session.close;
}catch(Exceptionex)
{
//nothingtodo
}finally
{
(session!=null)
session=null;
}
}
}
}

/**
*测试删除
*@throwsException
*/
publicvoidtestDeletethrowsException
{
.out.prln("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
{
(session!=null)
{
try
{
session.close;
}catch(Exceptionex)
{
//nothingtodo
}finally
{
(session!=null)
session=null;
}
}
}

/*
*删除后再查询
*/
.out.prln("n=testfindafterdelete=");
try
{
session=factory.openSession;

Integernum=(Integer)session.createQuery("fromPerson").list.size;
Assert.assertEquals(0,num.Value);

num=(Integer)session.createQuery("fromCard").list.size;
Assert.assertEquals(0,num.Value);

}catch(Exceptionex)
{
throwex;
}finally
{
(session!=null)
{
try
{
session.close;
}catch(Exceptionex)
{
//nothingtodo
}finally
{
(session!=null)
session=null;
}
}
}
}

/**
*
*/
@After
publicvoidtearDownthrowsException
{
factory.close;
}

}




  运行test测试成功.

  在Hibernateone-to-one关系映射中其实还有种方式即:唯主见关联但是直倾向于上面这种形式所以主见关联旧部介绍了



Tags:  hibernate映射视图 hibernate动态映射 hibernate映射文件 hibernate映射

延伸阅读

最新评论

发表评论