hibernate实验报告Word格式.docx

上传人:b****6 文档编号:21912643 上传时间:2023-02-01 格式:DOCX 页数:21 大小:44.03KB
下载 相关 举报
hibernate实验报告Word格式.docx_第1页
第1页 / 共21页
hibernate实验报告Word格式.docx_第2页
第2页 / 共21页
hibernate实验报告Word格式.docx_第3页
第3页 / 共21页
hibernate实验报告Word格式.docx_第4页
第4页 / 共21页
hibernate实验报告Word格式.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

hibernate实验报告Word格式.docx

《hibernate实验报告Word格式.docx》由会员分享,可在线阅读,更多相关《hibernate实验报告Word格式.docx(21页珍藏版)》请在冰豆网上搜索。

hibernate实验报告Word格式.docx

privateIntegerage;

Student.hbm.xml

<

hibernate-mapping>

<

classname="

model.Student"

table="

student"

>

<

idname="

id"

column="

type="

int"

<

generatorclass="

increment"

/generator>

/id>

propertyname="

username"

name"

string"

/property>

password"

age"

/class>

/hibernate-mapping>

Util包HibernateUtil.java

packageutil;

importorg.hibernate.Session;

importorg.hibernate.SessionFactory;

importorg.hibernate.cfg.Configuration;

publicclassHibernateUtil

{

privatestaticSessionFactorysessionFactory;

static

{

try

{

//获取sessionFactory

sessionFactory=newConfiguration().configure().buildSessionFactory();

}

catch(Exceptione)

System.err.println("

构造sessionFactory出错:

"

+e.getMessage());

}

publicstaticSessioncurrentSession()

//获取session

Sessionsession=sessionFactory.openSession();

returnsession;

//执行完后关闭session

publicstaticvoidcloseSession(Sessionsession)

if(null!

=session)

session.close();

}

}

Hibernate.cfg.xml

hibernate-configuration>

session-factory>

connection.url"

jdbc:

mysql:

//localhost:

3306/user

connection.driver_class"

com.mysql.jdbc.Driver

connection.username"

root<

connection.password"

dialect"

org.hibernate.dialect.MySQLDialect

show_sql"

true<

mappingresource="

Student.hbm.xml"

/>

/session-factory>

/hibernate-configuration>

Test1.java

packagetest;

importmodel.Student;

importorg.hibernate.Transaction;

importutil.HibernateUtil;

publicclassTest1

publicstaticvoidmain(String[]args)

Sessionsession=HibernateUtil.currentSession();

Transactiontx=session.beginTransaction();

Studentstu1=newStudent();

Try

stu1.setUsername("

xiaoming"

);

stu1.setPassword("

5732"

stu1.setAge(24);

session.save(stu1);

mit();

}catch(Exceptione)

{

tx.rollback();

}finally

实验结果:

实验二对象映射

1掌握OID用法

2掌握一对一关联、一对多关联、多对多关联用法

3掌握集合对象的映射用法

4掌握继承的映射

1、一个学生拥有一张借书卡,现在学生类和借书卡的定义如下,请将它们配置成一对一的双向关联。

publicclassStudent{

privateStringsex;

privateStuIdCardbookCard;

……

publicclassbookCard{

privateStringnum;

privateStudentstudent;

配置:

2、一个顾客在网上商店购物时,可以有多份订单,而一份订单只属于一个顾客。

其类的定义如下:

publicclassCustomerimplementsSerializable{

privateSetorders=newHashSet();

privateDoublemoney;

publicclassOrderimplementsSerializable{

privateLongid;

privateStringorderNumber;

privateDoubleprice;

privateCustomercustomer;

……}

请配置它们的xml。

3、每个人都有当前住址,其定义如下:

publicclassperson{

privateaddressaddress;

publicclassaddress{

privateStringdetail;

privateStringstreet;

请把address作为person的表字段进行配置,该如何配置?

4、如下类,

privateSet<

Teacher>

teachers=newHashSet<

();

ublicclassTeacher{

Student>

students=newHashSet<

请将其关系进行映射

一、Com.hibernate.pojo包student.java

privateIntegersid;

privateStringname;

privateSetstuidcards=newHashSet();

stuidcard.java

privateStudentstudent;

privateStringnum;

com.hibernate.pojo.Student"

catalog="

user"

sid"

java.lang.Integer"

columnname="

java.lang.String"

setname="

stuidcards"

inverse="

true"

key>

pk"

not-null="

/key>

one-to-manyclass="

com.hibernate.pojo.Stuidcard"

/set>

Stuidcard.hbm.xml

stuidcard"

many-to-onename="

class="

fetch="

select"

/many-to-one>

num"

二、order.java

privateIntegerorderid;

privateIntegernumber;

privateIntegerprice;

customer.java

privateIntegercid;

privateStringmoney;

privateSetorders=newHashSet(0);

Order.hbm.xml

com.hibernate.pojo.Order"

order"

orderid"

customer"

com.hibernate.pojo.Customer"

op"

number"

price"

Customer.hbm.xml

cid"

money"

orders"

三、Address.hbm.xml

pojo.Address"

address"

addressId"

/>

native"

detail"

street"

Person.hbm.xml

pojo.Person"

person"

personId"

cascade="

all"

Address"

四、Student.hbm.xml

pojo.Student"

studentid"

teacher"

studentteacher"

keycolumn="

many-to-manycolumn="

teacherid"

pojo.Teacher"

Teacher.hbm.xml

student2"

studentId"

实验三事务处理

1、掌握事务的处理机制

2、掌握对象的三种状态

3、掌握并发访问方式

4、掌握数据持久层的设计方式

实验内容:

已知account类的定义如下:

packagemypack;

importjava.io.Serializable;

importjava.util.Set;

publicclassAccountimplementsSerializable{

privatedoublebalance;

publicAccount(Stringname,doublebalance){

this.name=name;

this.balance=balance;

publicAccount(){

publicLonggetId(){

returnthis.id;

publicvoidsetId(Longid){

this.id=id;

returnthis.name;

publicdoublegetBalance(){

returnthis.balance;

publicvoidsetBalance(doublebalance){

1、

(1)请以数据库系统的独占锁--悲观锁方式读取一个account账户实例

(2)请将account改写,利用Hibernate的版本控制实现乐观锁

1.独占锁方式读取一个account账户实例

测试类:

SessionFactorysessionFactory=new

Configuration().configure().buildSessionFactory();

Sessionsession=sessionFactory.openSession();

Transactiontx=session.beginTransaction();

Accountaccount=(Account)session.get(Account.class,1L,LockMode.NONE);

System.out.println(account.getId()+"

"

+account.getName());

mit();

session.close();

sessionFactory.close();

Account.hbm.xml

com.test.bean.Account"

a

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 外语学习 > 其它语言学习

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1