0

我有两张简单的桌子。

    mysql> select * from filesshare;
+----+--------+--------+-------+
| id | userId | fileId | owner |
+----+--------+--------+-------+
|  1 |      2 |      1 |     1 |
|  2 |      3 |      1 |     1 |
|  3 |      4 |      2 |     1 |
|  4 |      5 |      2 |     1 |
+----+--------+--------+-------+
4 rows in set (0.00 sec)

mysql> select * from filesinfo;
+----+-----------------+-----------------------------------+
| id | name            | url                               |
+----+-----------------+-----------------------------------+
|  1 | dwnld_btn.png   | C:\shareapp\admin\dwnld_btn.png   |
|  2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
+----+-----------------+-----------------------------------+
2 rows in set (0.01 sec)

我现在将有两个值作为输入。1) 用户 ID 2) 所有者

现在假设 userId=3 和 owner=1。现在我想输出匹配文件共享表中给定值的所有行,在这种情况下,它的第二行是“2 | 3 | 1 | 1”现在我想要第三列的值现在是 1。然后使用该值,我想从 filesinfo 表中检索数据。在这种情况下,filesinfo 表的第一行将输出。像这样:

+----+-----------------+-----------------------------------+
| id | name            | url                               |
+----+-----------------+-----------------------------------+
|  1 | dwnld_btn.png   | C:\shareapp\admin\dwnld_btn.png   |
+----+-----------------+-----------------------------------+

是否可以使用休眠进行连接,一个简单的例子对我来说是一个启动。

我试过这个

mysql> Select fileid,name,url from filesshare fs,filesinfo fi where fs.fileid=fi
.id;
+--------+-----------------+-----------------------------------+
| fileid | name            | url                               |
+--------+-----------------+-----------------------------------+
|      1 | dwnld_btn.png   | C:\shareapp\admin\dwnld_btn.png   |
|      1 | dwnld_btn.png   | C:\shareapp\admin\dwnld_btn.png   |
|      1 | dwnld_btn.png   | C:\shareapp\admin\dwnld_btn.png   |
|      1 | dwnld_btn.png   | C:\shareapp\admin\dwnld_btn.png   |
|      2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
|      2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
|      2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
|      2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
+--------+-----------------+-----------------------------------+

谢谢并恭祝安康

4

3 回答 3

1

在休眠中,您可以通过两种方式处理它:

  • 使用普通的 sql

    例如: SQLQuery query = session.createSQLQuery("select fi.* from fileshare fs, filesinfo fi where fs.fileId =fi.id and fs.userId = :uid and fs.owner = :oid"); query.setInteger("uid", userid); query.setInteger("oid", ownerid);

       /*maybe deal with transformation */
       query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);)
       return  (List<Map<String,Object>>)query.list();   
    

    这是有关转换详细信息的链接:http: //docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-associations

  • 使用 hql 假设您有实体映射到

    例子:

      Criteria crit =  session.createCriteria(Filesinfo.class)
      crit.setFetchMode("fs.fileshare", FetchMode.JOIN);
      crit.add( Restrictions.eq("fs.userId", userid) Restrictions.eq("fs.owner", ownerid) );
      return crit.list();
    
    all info on this method is here: http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html
    
     and here is the details of teh ORM mapping via xml: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/xml.html
    
于 2013-04-26T18:03:25.867 回答
0

k 家伙这一个挠头后,当然也来自谷歌的帮助。

     mysql> select a.id, a.name, a.url from filesinfo a inner join filesshare b on 
a.id=b.fileid where b.userid=5 and b.owner=1;
    +----+-----------------+-----------------------------------+
    | id | name            | url                               |
    +----+-----------------+-----------------------------------+
    |  2 | dwnld_btn_1.png | C:\shareapp\admin\dwnld_btn_1.png |
    |  4 | loader3.gif     | C:\shareapp\admin\loader3.gif     |
    +----+-----------------+-----------------------------------+
    2 rows in set (0.02 sec)
于 2013-04-26T17:54:12.433 回答
0

选择 id,name,url from fileshare fs,filesinfo fi where fs.id=fs.id;

于 2013-04-26T16:13:09.463 回答