查询的结果控制还是用在具体的hql语句里面去吧,关于这个一对多的查询,我常用的一个方法是在hql中用 join fetch去做
下面是我的项目中用到的一句连接查询(其中一个CustomerInfor对象可以拥有多个MenInfor对象)String hql = "select infor from MenInfor as infor join fetch infor.customerInfor";
下面是我的项目中用到的一句连接查询(其中一个CustomerInfor对象可以拥有多个MenInfor对象)String hql = "select infor from MenInfor as infor join fetch infor.customerInfor";
user: user_id,user_name
user_detail: user_id,age,sex,memo;
这两个表通过user_id关联,按说表结构应该没问题。
xml配置如下
<many-to-one name="detail" class="user.UserDetail" fetch="select" lazy="false"
cascade="save-update" unique="true" />
这么配置的话,在HQL里始终查询User_.detail列,可事实上没有这一列。如果加上
column="user_id"又提示列重复了。
真不知道该怎么办好了。 name="detail" 这个是java 的属性名
db 的列名呢? 如果不一样要写出来特别注意大小写如
<many-to-one name="detail" column="*******" fetch="select" lazy="false"
cascade="save-update" unique="true" />
class="user.UserDetail" 不应写在这里就写在pojo里面
<one-to-one name="employee" class="Employee" constrained="true"/>
现在我们必须确保PERSON和EMPLOYEE中相关的字段是相等的。我们使用一个被成为foreign的特殊的hibernate标识符生成策略: <class name="person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="foreign">
<param name="property">employee</param>
</generator>
</id>
...
<one-to-one name="employee"
class="Employee"
constrained="true"/>
</class>
一个刚刚保存的Person实例被赋予和该Person的employee属性所指向的Employee实例同样的关键字值。
另一种方式是一个外键和一个惟一关键字对应,上面的Employee和Person的例子,如果使用这种关联方式,可以表达成: <many-to-one name="person" class="Person" column="PERSON_ID" unique="true"/>
如果在Person的映射加入下面几句,这种关联就是双向的: <one-to-one name"employee" class="Employee" property-ref="person"/>
<many-to-one name="detail" class="user.UserDetail" fetch="select" lazy="false"
cascade="save-update" unique="true" />
你这样写就是表示一对一
unique="true"就表示唯一约束
fetch="select"表示外连接抓取。
lazy="false"是否延迟加载
同时你在建UserDetail表时需要有个外键user_id指向user表
你也可以不在建表时设置级联既UserDetail表有user_id一列
然后在SQL或HQL语句中进行控制级联查询等
然后在HQL语句中进行控制级联查询
column="user_id"
class="user.UserDetail"
fetch="select" lazy="false"
cascade="save-update" unique="true" />
直接用hibernate annotation 就好了
<many-to-one name="detail" class="user.UserDetail" fetch="select" lazy="false"
cascade="save-update" unique="true" /> 这个是那个类的呢??
如果是 user 的话就应该是one-to-many
如果是 user_detail 的话 也不是像你现在写的这样。
lz自己慢慢 google去吧