参考一下
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.
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.
怎么个生成法?
用一个sql查询出标的结构 这个很容易根据返回的结果动态生成一个类 不是很难的问题哪里会有问题
即时生成,就是我想,能不能构造一个类的源代码,然后保存为java文件,再调用编译器编译了它。不知道能否实现阿to: jokerjava(冷血)
我要的是不用sql的方法。比如用hibernate等,我认为应该有方法动态控制某个参数,如tablename,来使得某个类对应于某个表。
懒得去想,太麻烦,得不偿失。
那有什么办法能实现类似的东西?动态使得某个vo/po类对应到新表
userSessionFactory = cfg.buildSessionFactory();2,CGLIB可以动态的建立一个bean
> 如有很多种产品, 放在不同产品表中, 产品和属性字断的名称存在名称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具有极大的可扩展性,例如支持存储过程,视图的调用,支持动态表名,动态表字段等等。
比如,我们做一个新闻网站,有很多种新闻,娱乐的体育的社会的国内的等。
要放入不同的表,但是每个表结构都一样。那么,我们首先建立一个News.class
然后建立多个SportNews.class等,来继承News.class
为每个写一个hbm.xml,这样完全可以满足一个新闻系统仅对News.class
进行所有操作。但是,还是不能满足我们在Runtime时生成的新表。
看来还是比较复杂。只好去研究ClassPersister了
正常
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.
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.
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
他说:This is possible in Hibernate 2.2. (Already implemented in CVS.) 也就是说,对于一个类对应于多个结构相似的表,在2.2版本已经实现
可能就是根据某种参数来动态指定的
相信2-3个月就能够看到最终版本了ok,结账了