Hibernate3.3.2_多对一单向关联

本文详细介绍了数据库操作及实体关系建模的过程,通过实际案例展示了如何使用Hibernate框架进行对象关系映射。

package com.bjsxt.hibernate;

public class Group {
private int id;
private String name;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}



package com.bjsxt.hibernate;

public class User {
private int id;
private String name;
private Group group;

public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
[color=red]User(many)---Group(one)[/color]



Group.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="com.bjsxt.hibernate.Group" table="t_group">
<id name="id">
<generator class="native"></generator>
</id>

<property name="name"></property>

</class>

</hibernate-mapping>



User.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="com.bjsxt.hibernate.User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>

<property name="name"></property>
<many-to-one name="group" column="groupId" />
</class>

</hibernate-mapping>



14:27:21,165 INFO org.hibernate.tool.hbm2ddl.SchemaExport:154 - Running hbm2ddl schema export
14:27:21,168 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:170 - import file not found: /import.sql
14:27:21,168 INFO org.hibernate.tool.hbm2ddl.SchemaExport:179 - exporting generated schema to database
14:27:21,323 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 -
alter table t_user
drop
foreign key FKCB63CCB6C3D18669
14:27:21,332 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:288 - Unsuccessful: alter table t_user drop foreign key FKCB63CCB6C3D18669
14:27:21,333 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:289 - Table 'hibernate.t_user' doesn't exist
14:27:21,333 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 -
drop table if exists t_group
14:27:21,337 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 -
drop table if exists t_user
14:27:21,340 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 -
create table t_group (
id integer not null auto_increment,
name varchar(255),
primary key (id)
)
14:27:21,479 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 -
create table t_user (
id integer not null auto_increment,
name varchar(255),
groupId integer,
primary key (id)
)
14:27:21,589 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 -
alter table t_user
add index FKCB63CCB6C3D18669 (groupId),
add constraint FKCB63CCB6C3D18669
foreign key (groupId)
references t_group (id)
14:27:21,884 INFO org.hibernate.tool.hbm2ddl.SchemaExport:196 - schema export complete


inverse知识点
http://lijiejava.iteye.com/blog/776587
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值