参考一下
8.3.6. Queries in native SQL
You may express a query in SQL, using createSQLQuery(). You must enclose SQL aliases in braces. List cats = session.createSQLQuery(
    "SELECT {cat.*} FROM CAT AS {cat} WHERE ROWNUM<10", 
    "cat",
    Cat.class
).list();
List cats = session.createSQLQuery(
    "SELECT {cat}.ID AS {cat.id}, {cat}.SEX AS {cat.sex}, {cat}.MATE AS {cat.mate}, {cat}.SUBCLASS AS {cat.class}, ... " +
    "FROM CAT AS {cat} WHERE ROWNUM<10", 
    "cat",
    Cat.class
).list()
SQL queries may contain named and positional parameters, just like Hibernate queries. 

解决方案 »

  1.   

    但是这样相当于又一次把sql嵌入到DAO中了这样做是否返回的CAT表都对应到Cat.class呢?
      

  2.   

    查询时可以动态指定查哪张表(比如CAT_1,CAT_2...),但返回的都是同一个类(CAT)的对象。
      

  3.   

    >即时生成类<
    怎么个生成法?
      

  4.   

    应该可以啊
    用一个sql查询出标的结构  这个很容易根据返回的结果动态生成一个类  不是很难的问题哪里会有问题
      

  5.   

    to  miwoo(miwoo) 
    即时生成,就是我想,能不能构造一个类的源代码,然后保存为java文件,再调用编译器编译了它。不知道能否实现阿to: jokerjava(冷血) 
    我要的是不用sql的方法。比如用hibernate等,我认为应该有方法动态控制某个参数,如tablename,来使得某个类对应于某个表。
      

  6.   

    >即时生成,就是我想,能不能构造一个类的源代码,然后保存为java文件,再调用编译器编译了它。不知道能否实现阿<
    懒得去想,太麻烦,得不偿失。
      

  7.   

    to  miwoo(miwoo) 
    那有什么办法能实现类似的东西?动态使得某个vo/po类对应到新表
      

  8.   

    查询可以像我上面说的那样做,但更新我就不清楚了,难,http://forum.hibernate.org/
      

  9.   

    在官方论坛找到了几个回复,不知道行不行。继续关注ing1,修改mapping的table,但是每次还要重新建立sessioncfg.getClassMapping(...).getTable().setName(...); 
    userSessionFactory = cfg.buildSessionFactory();2,CGLIB可以动态的建立一个bean
      

  10.   

    现在的问题是,如果我像上边那样重新buildsession那我如何实现两个表的union查询?
      

  11.   

    hibernate.org.cn的robbin提出过一个解决方法:> 我们知道, 在有些大系统中, 都会有一个数据字典, 可用于记录各个表,字端的的名称, 比
    > 如有很多种产品, 放在不同产品表中, 产品和属性字断的名称存在名称reference表中, 程
    > 序可通过查reference表得到相应的表和字断, 从而进行下一步的操作, 这是典型的数
    > dimension & fact数据库设计, 
    > 但hibernate目前不支持动态表名, Gavin King说通过写一个custom ClassPersister可能会实现这个功能, 
    > 谁了解custom ClassPersister. 给详细说说 ClassPersister的实现类是EntityPersister(实现1表对1PO的映射)和NormalizedEntityPersister(1表对子PO的映射),是把对表的操作转换为PO的操作的类,你可以自己实现ClassPersister,然后在hbm里面指定,那么就可以改变默认的映射规则,比如说不再是字段和属性的映射,可以是输入参数和属性的映射,来支持存储过程的调用。Hibernate2.0.3源代码里面有一个简单的例子net.sf.hibernate.test.CustomPersister,这个例子也是改变了默认的映射规则,不是表映射PO,而是一个Hashtable对象映射PO,可以参考一下。 可以通过自己实现ClassPersiter接口,让Hibernate具有极大的可扩展性,例如支持存储过程,视图的调用,支持动态表名,动态表字段等等。 
      

  12.   

    上面的robbin的方案是可行的。但是做起来比较麻烦的样子。一会儿看看ClassPersiter的相关东西。在forum.hibernate.org有人回复我,说是用view,一个大表建立view。还有人说,用subclass.具体的subclass方法我想了一下,如果是用于静态的表是可以实现的。
    比如,我们做一个新闻网站,有很多种新闻,娱乐的体育的社会的国内的等。
    要放入不同的表,但是每个表结构都一样。那么,我们首先建立一个News.class
    然后建立多个SportNews.class等,来继承News.class
    为每个写一个hbm.xml,这样完全可以满足一个新闻系统仅对News.class
    进行所有操作。但是,还是不能满足我们在Runtime时生成的新表。
    看来还是比较复杂。只好去研究ClassPersister了
      

  13.   

    Oh!! Shit!! 居然要实现几十个method真麻烦!!!
      

  14.   

    >居然要实现几十个method<
    正常
      

  15.   

    再等几天看看TSS有没有人回复~~ 然后再结帖
      

  16.   

    另外已经提交了JIRA给hibernate,希望开发人员回应一下,并能够在今后的版本中实现dynamic table name
      

  17.   

    Posted By: Howard Hill on December 07, 2003 @ 08:07 AM in response to Message #103547. 
    You are right about making a generic News.class, this class can be the base class 
    and you have your subclass like entertaiment, sport, government, techinical. Hibernate allows you have a base class and its subclass mapped in one News.hbm. 
    This feature is enhanced by using a discriminator value.(Read the Documenation @hibernate.org). This will help you with the different types of news. so you can create an instance of and entertaiment news, sports etc and persist them, instead of having multiple hbm.xml files. One more thing, why do you have for each year of news a seperate table ? 
    I dont this is a good database design. 
    Does'nt each news item have a date, so your can query by date , or whatever you get the idea.
      

  18.   

    Posted By: Balamurugan K on December 08, 2003 @ 10:19 PM in response to Message #103547. 
    by using JDO(java Data Objects) u can convert XML into objects and objects into XML files.By using jdo u can merge serveral tables,u don't worry abt forming XML data ,the JDO will take care of it.
      

  19.   

    HIBERNATE.org Forum给的回复 
     
     
     
     
     
     
     Posted: Sat Dec 06, 2003 11:31 am    Post subject: HOW can I make a class associate with multi-table DYNAMIC!   
      
    It is a big problem for me. my project is a refactoring project to a old system. 
    in the old system, we create a table by each year. such as: TBL_NEWS_2003, TBL_NEWS_2002 so when we need search in years, we use UNION to select from all table. BUT how can i do this in hibernate?? I create a class named: News, 
    and , actually I must make a News.hbm.xml for it. 
    and i wrtie table name in the XML. I need associate the New.class to multi-table 
    when i query some news, i can specify a tablename or modualname parameter to the session and get the same class from different table. can i make myself understand? I NEED HELP. 
     --------------------------------------------------------------------------------
     
    epbernard 
    ExpertJoined: 14 Sep 2003
    Posts: 557
    Location: France 
     
     
     
     
     
     
     Posted: Sat Dec 06, 2003 12:16 pm    Post subject:    
      
    Unions are not doable AFAIK Try to do this kind of mapping: 
    Create a News class, create News2002, News2003 as subclasses. 
    Map as table-per-concrete class (map News2002 and News 2003 but not News). And play with that. This solution has many drawbacks, and probably don't be appropriate in your case. Submit a request to JIRA for you initial feature, Gavin will tell you if you can expect that in a future Hibernate.
    _________________
    Emmanuel 
     --------------------------------------------------------------------------------
     
    ibingyun 
    New UserJoined: 06 Dec 2003
    Posts: 4
     
     
     
     
     
     
     
     Posted: Sat Dec 06, 2003 1:42 pm    Post subject:    
      
    epbernard wrote: 
    Unions are not doable AFAIK Try to do this kind of mapping: 
    Create a News class, create News2002, News2003 as subclasses. 
    Map as table-per-concrete class (map News2002 and News 2003 but not News). And play with that. This solution has many drawbacks, and probably don't be appropriate in your case. Submit a request to JIRA for you initial feature, Gavin will tell you if you can expect that in a future Hibernate. 
    I don't know what's JIRA?? 
    how can i contact Gavin to suggest my advice as below? When one develop a website such as a news website. 
    It its very easy to consider making a generic News.class to 
    fit for all kinds of news. 
    eg. A news may have title, content, date ... so we develop a News.class. 
    but we have different news, like entertaiment, sport, government, techinical ..... so, we must have dynamically mapping to News.class News.class should associate to several *.hbm.xml file, each file figure to a table. 
    like News.sport.hbm.xml, News.entertaiment.hbm.xml (May be we can specify the different table name just in the filename) and the Configuaration can read all xml 
    and one can query News.class in all different table or only one table. That's what i except mostly of the future hibernate. 
     --------------------------------------------------------------------------------
     
    baliukas 
    Hibernate UserJoined: 28 Aug 2003
    Posts: 90
    Location: Vilnius, Lithuania 
     
     
     
     
     
     
     Posted: Sat Dec 06, 2003 2:07 pm    Post subject: Re: HOW can I make a class associate with multi-table DYNAMI   
      
    ibingyun wrote: so when we need search in years, we use UNION to select from all table. 
    BUT how can i do this in hibernate?? 
     CREATE VIEW AS ... 
     --------------------------------------------------------------------------------
     
    epbernard 
    ExpertJoined: 14 Sep 2003
    Posts: 557
    Location: France 
     
     
     
     
     
     
     Posted: Sat Dec 06, 2003 2:32 pm    Post subject:    
      
    Well I don't understand your pb. You told me you where in a 1 table per year issue, and then you seems to have simple table-per-hierarchy will. Map news then map subclasses of News (SportNews, etc..) as <subclass> of News. Hibernate auready do that. JIRA is the issue tracking facility. 
    http://opensource.atlassian.com/projects/hibernate/secure/Dashboard.jspa or the left link in Hibernate web site. 
    But clarify your though before posting an enhancement request. CREATE VIEW AS ... 
    I'm not familiar with all DBMS but I'm not sure this kind of VIEW be updateable/ insertable.
    _________________
    Emmanuel 
     --------------------------------------------------------------------------------
     
    max 
    Hibernate TeamJoined: 26 Aug 2003
    Posts: 229
     
     
     
     
     
     
     
     Posted: Sat Dec 06, 2003 3:27 pm    Post subject: Re: HOW can I make a class associate with multi-table DYNAMI   
      
    Map your class to the current year table, and then just use createSQLQuery() to write your own custom union query to read from all tables. This will work as long as you only want to add/update into the current year table and just need to read from the past.
    _________________
    /max 
     --------------------------------------------------------------------------------
     
    ibingyun 
    New UserJoined: 06 Dec 2003
    Posts: 4
     
     
     
     
     
     
     
     Posted: Sat Dec 06, 2003 3:53 pm    Post subject: Re: Still not work   
      
    max wrote: 
    Map your class to the current year table, and then just use createSQLQuery() to write your own custom union query to read from all tables. This will work as long as you only want to add/update into the current year table and just need to read from the past. Thank's max. Your advice solve part of my problem. but the BIGGEST problem is, My recently year table is create by program automatic. I can not have a PO class to map it. So I'd like to suggest to Hibernate group adding a dynamic table name mapping function. by epbernard's method: 
    I use News.class' subclass SportNews, It's a good solution for the System 
    not creating table by program itself. It's fit for static tables. I heard that Gavin King said a custom ClassPersister can deal with the dynamic table name problem. But ... big trouble... 
     --------------------------------------------------------------------------------
     
    baliukas 
    Hibernate UserJoined: 28 Aug 2003
    Posts: 90
    Location: Vilnius, Lithuania 
     
     
     
     
     
     
     Posted: Sun Dec 07, 2003 7:49 pm    Post subject:    
      
    Common solution for this kind of problems are views. 
    It is possible to use system tables for metadata, stored procedures, query rewrite to make logical view of data. 
    But it is very common to ignore RDMS features and to handle logical view of data in application too :) 
     --------------------------------------------------------------------------------
     
    max 
    Hibernate TeamJoined: 26 Aug 2003
    Posts: 229
     
     
     
     
     
     
     
     Posted: Sun Dec 07, 2003 8:16 pm    Post subject:    
      
    btw. this precise usecase is now solvable via the new "entity-name" feature in v22branch in hibernate cvs! Look at the devel-mailing list for details ;)
    _________________
    /max 
     
      

  20.   

    HIBERNATE.org Forum给的方法很好面向一个接口,生成不同的子类可能不够动态,不过也可以用了
      

  21.   

    哦,说错了,不好意思是Posted By: Howard Hill on December 07, 2003 @ 08:07 AM in response to Message 给出的, 用不同的discriminator value分辩不同的子类而且subclass和baseclass都map在一张表中值得考虑
      

  22.   

    最新的答复是Gavin King [ 09/Dec/03 04:27 PM ] Hibernate的开发者回复的。
    他说:This is possible in Hibernate 2.2. (Already implemented in CVS.)  也就是说,对于一个类对应于多个结构相似的表,在2.2版本已经实现
    可能就是根据某种参数来动态指定的
    相信2-3个月就能够看到最终版本了ok,结账了