锁定老帖子 主题:请问这样的关系如何设计,谁给支一招?
该帖已经被评为精华帖
|
|
---|---|
作者 | 正文 |
发表时间:2004-11-21
public class Table{ private Person east; private Person west; private Person south; private Person north; private String tableid; } public class Person{ private Table table; private String personid; } 在这里Person与Table是做面one-to-one 还是many-to-one呢,映射文件该怎么写呢。先谢了! 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2004-11-22
其实楼主的class 属性就暗示了这种关系。
|
|
返回顶楼 | |
发表时间:2004-11-22
看你的系统的控制范围是什么?如果是只在这个桌子,那么就是Person0...1--------4...0Table。
如果是多张桌子,那么就是Person0...*---------4...0Table。 |
|
返回顶楼 | |
发表时间:2004-11-22
怪我没表达清楚,我是问这种关系该如何用HIBERNATE进行映射,在定义Person.hbm.xml文件时,table属性是定义成多对一还是一对一,映射文件该怎么写。
|
|
返回顶楼 | |
发表时间:2004-11-22
jasonhsu,你是把一张桌子的每一边做为了Table的一个属性,所有没有什么桌子同人的关系,而是桌子的每一边同人的关系。
一张桌子的每一边(east、west、south、north)只能坐一个人;一个人只能坐在一张桌子的一边——就是这个关系。 |
|
返回顶楼 | |
发表时间:2004-11-22
楼上的,关系映射怎么写呀
|
|
返回顶楼 | |
发表时间:2004-11-22
larlf,你分析得很对,可是我就是问对这种关系进行映射时该怎么写啊
|
|
返回顶楼 | |
发表时间:2004-11-22
你的代码显然是一对一啊。共享primary key的one-to-one方式明显不合适这个例子,那就用many-to-one.
|
|
返回顶楼 | |
发表时间:2004-11-22
我的设计是Table对象的每个方向属性对Person对象是many-to-one关系,而每个Person对象对Table对象也是many-to-one关系。相关代码如下:
Table类Java代码: public class Table { private Long id; private String name; private Person east; private Person west; private Person north; private Person south; /** * @return Returns the east. */ public Person getEast() { return east; } /** * @param east The east to set. */ public void setEast(Person east) { this.east = east; east.setTable(this); east.setPosition("east"); } /** * @return Returns the id. */ public Long getId() { return id; } /** * @param id The id to set. */ public void setId(Long id) { this.id = id; } /** * @return Returns the name. */ public String getName() { return name; } /** * @param name The name to set. */ public void setName(String name) { this.name = name; } /** * @return Returns the north. */ public Person getNorth() { return north; } /** * @param north The north to set. */ public void setNorth(Person north) { this.north = north; north.setTable(this); north.setPosition("north"); } /** * @return Returns the south. */ public Person getSouth() { return south; } /** * @param south The south to set. */ public void setSouth(Person south) { this.south = south; south.setTable(this); south.setPosition("south"); } /** * @return Returns the west. */ public Person getWest() { return west; } /** * @param west The west to set. */ public void setWest(Person west) { this.west = west; west.setTable(this); west.setPosition("west"); } } 映射文件: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Table" table="T_TABLE"> <id name="id" unsaved-value="null"> <generator class="native"/> </id> <property name="name"/> <many-to-one name="south" class="Person" column="south_id" unique="true" cascade="all"/> <many-to-one name="west" class="Person" column="west_id" unique="true" cascade="all"/> <many-to-one name="east" class="Person" column="east_id" unique="true" cascade="all"/> <many-to-one name="north" class="Person" column="north_id" unique="true" cascade="all"/> </class> </hibernate-mapping> Person类代码: public class Person { private Long id; private String name; private Table table; private String position; /** * @return Returns the name. */ public String getName() { return name; } /** * @param name The name to set. */ public void setName(String name) { this.name = name; } /** * @return Returns the position. */ public String getPosition() { return position; } /** * @param position The position to set. */ public void setPosition(String position) { this.position = position; } /** * @return Returns the id. */ public Long getId() { return id; } /** * @param id The id to set. */ public void setId(Long id) { this.id = id; } /** * @return Returns the table. */ public Table getTable() { return table; } /** * @param table The table to set. */ public void setTable(Table table) { this.table = table; } } 映射文件: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Person" table="T_PERSON"> <id name="id" unsaved-value="null"> <generator class="native"/> </id> <property name="name"/> <property name="position"/> <many-to-one name="table" class="Table" column="table_id" /> </class> </hibernate-mapping> |
|
返回顶楼 | |
发表时间:2004-11-22
给某张桌子的某个位置安排打牌的人:
Person robbin = new Person(); p.setName("robbin"); session.save(robbin); Person jasonhsu = new Person(); p.setName("jasonhsu"); session.save(jasonhsu); Table table = (Table) session.load(Table.class, id); table.setEast(robbin); table.setWest(jasonhsu); session.update(table); 忙着搬家,没有办法仔细推敲测试了,仅供参考。 |
|
返回顶楼 | |