hibernate 映射问题

有表A(User)

id | ...



表B

id | content | userid



其中userid对应表A中的id



现要取回某个userid对应的表B中的数据,考虑用User.getB(), 但是这样hibernate会多一次查询

1) select xxx from A where id=?

2) select xxx from B where userid=?

对于1),这个查询就没有需要了



我的问题是能不能做一些配置之类的可以不产生1)的查询, 如果不可以的话,是不是拼HSQL是唯一的解决方式?

from B where userid=?

使用一对一的关系映射,具体配置给你一些参考
[code="java"]





<class name="IdCard" table="id_card">
    <id name="id">
        <generator class="foreign">
            <param name="property">person</param>
        </generator>
    </id>
    <property name="usefulLife" column="useful_life"></property>
    <one-to-one name="person" constrained="true"></one-to-one>
</class>[/code]

上面表示的是一个人和这个人的身份证的对应关系。
注意,我配置的人是主对象,这样你查询idcard的时候才会只产生一条select语句。
反之则不能。

你可以google一下,一对一关系,了解一下上面的配置。

看你的是1 to 1的吧。你查下 1对1 外键关联

楼主的需求仿佛是one-to-one的,User.getB()

如果是User.getBs()
可以设置one-to-many的属性为lazy="true" 和 fetch="join"
[url]http://hi.baidu.com/z57354658/blog/item/5dd7fc3b1f087def14cecb74.html[/url]
[url]http://www.blogjava.net/alex/archive/2007/08/27/84808.html?opt=admin[/url]
[url]http://spiritfrog.iteye.com/blog/197505[/url]

one-to-one的可能也可以这样设置,楼主可以试一下。