下面的两个class是原始的和数据库表的列完全一致的,比如
class Person {
int personId;
string personName;
int teamId;
}
class Team {
int teamId;
string teamName;
}映射文件为person.hbm.xml和team.hbm.xml,映射文件中没有实现关联。
<!--person.hbm.xml-->
<class name="Person" table="TBL_PERSON">
<id name="personId" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="personName" column="PERSON_NAME"/>
<property name="teamId" column="TEAM_ID"/>
</class>
<!--team.hbm.xml-->
<class name="Team" table="TBL_TEAM">
<id name="teamId" column="TEAM_ID">
<generator class="native"/>
</id>
<property name="teamName" column="TEAM_NAME"/>
</class>DAO中取person的时候用load(Person.class, personId)。现在想取person的时候,同时根据teamId把team对象也一块取出来(左连接)。但不想修改Person类,于是新建一个类PersonWithTeam如下:
class PersonWithTeam extends Person {
Team team;
}
这个时候映射文件应该如何修改写啊?取得时候最好load(Person.class, personId)和load(PersonWithTeam.class, personId)都可以用,前者为不带team属性的person对象,后者为带team属性的person对象。
class Person {
int personId;
string personName;
int teamId;
}
class Team {
int teamId;
string teamName;
}映射文件为person.hbm.xml和team.hbm.xml,映射文件中没有实现关联。
<!--person.hbm.xml-->
<class name="Person" table="TBL_PERSON">
<id name="personId" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="personName" column="PERSON_NAME"/>
<property name="teamId" column="TEAM_ID"/>
</class>
<!--team.hbm.xml-->
<class name="Team" table="TBL_TEAM">
<id name="teamId" column="TEAM_ID">
<generator class="native"/>
</id>
<property name="teamName" column="TEAM_NAME"/>
</class>DAO中取person的时候用load(Person.class, personId)。现在想取person的时候,同时根据teamId把team对象也一块取出来(左连接)。但不想修改Person类,于是新建一个类PersonWithTeam如下:
class PersonWithTeam extends Person {
Team team;
}
这个时候映射文件应该如何修改写啊?取得时候最好load(Person.class, personId)和load(PersonWithTeam.class, personId)都可以用,前者为不带team属性的person对象,后者为带team属性的person对象。
解决方案 »
- hibernate 中多对多关系映射:session中不能save()两个对象
- hibernate配置多个数据库链接的问题
- Hibernate-Many-to-one
- hibernate many to many 设置二级缓存问题
- 谁知道那里有基与struts的oa办公系统源代码么
- 请教高手关于j2eesdk-1_4_2005Q1-windows.exe和eclipse的问题
- hibernate:on-to-many 里面有详细代码(郁闷好长时间了,解决了马上结贴,没有解决最晚周日晚上结贴)
- jtable的问题,高手帮帮我
- 如何使用多个xslt转换同一个xml的不同节点
- 这是为什么?Can't make static reference to method void makeXMLTree(org.w3c.dom.Element, java.lang.
- 求DAT音频格式转化为WMV格式的代码java
- 三张表查询语句
我写了下面这样的,不行。
<!--PersonWithTeam.hbm.xml-->
<class name="PersonWithTeam" table="TBL_PERSON">
<id name="personId" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="personName" column="PERSON_NAME"/>
<property name="teamId" column="TEAM_ID"/>
<many-to-one name="team" fetch="join" class="Team" column="TEAM_ID">
</class>
会报错,说TEAM_ID重复定义了。如果把<property name="teamId" column="TEAM_ID"/>这行删除,倒是可以了。但是PersonWithTeam的teamId属性会是空。这一点可以在程序里面手动设置,但是很傻的样子,有没有什么好办法?还有就是配置文件中的Person和PersonWithTeam的共同属性能不能共用(即配置文件中不要写两遍,如personName属性),关于这个有没有什么好方法阿?谢谢
PersonDao类里面 建两个方法, 一个调用TeamDao ,一个不调用TeamDao
class Person {
int personId;
string personName;
Team team;
}
class Team {
int teamId;
string teamName;
List person;
}映射文件为person.hbm.xml和team.hbm.xml,映射文件中没有实现关联。
<!--person.hbm.xml-->
<class name="Person" table="TBL_PERSON">
<id name="personId" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="personName" column="PERSON_NAME"/>
<many-to-one name="team" class="*.Team" column="teamId">
</class>
<!--team.hbm.xml-->
<class name="Team" table="TBL_TEAM">
<id name="teamId" column="TEAM_ID">
<generator class="native"/>
</id>
<property name="teamName" column="TEAM_NAME"/>
<set name="person" cascade="all" inverse="true">
<key column="teamId"></key>
<one-to-many class="*.person"></one-to-many>
</set
我的理解是实体之间关系很复杂,才不关联,自己手写关系