Hibernate: 如何能够凭空生成一个VO或者一个VO对应多个表?

icecloud 2003-12-05 01:06:11
最近在设计中遇到一个问题

旧有的系统是把某个表,按照年份分成多个表来保存
比如,2002年度的公文,2003年度的公文

表结构完全一样,系统中用SQL根据表名TBL_XXX_2002不同来查询
如果是新的一年,就建立一个新表
原来的系统用的BusinessObject,将tablename传递进入类中

这样就出现一个问题,如果我用hibernate等orm来处理持久层
如何才能只建立一个VO Class,而对应到多个TABLE呢?

换一个角度说,就是如何才能为一个新的表动态生成一个class文件呢?
因为要跟旧有的表兼容,不得不考虑这个问题
...全文
320 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
icecloud 2003-12-10
  • 打赏
  • 举报
回复
我这贴才叫解决问题呢吧!

看了很多乌烟瘴气说不清楚问题也回答不到点上的帖子

俺这贴应该精华吧
icecloud 2003-12-10
  • 打赏
  • 举报
回复
最新的答复是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,结账了


sankxuan 2003-12-09
  • 打赏
  • 举报
回复
哦,说错了,不好意思

是Posted By: Howard Hill on December 07, 2003 @ 08:07 AM in response to Message

给出的, 用不同的discriminator value分辩不同的子类

而且subclass和baseclass都map在一张表中

值得考虑

sankxuan 2003-12-09
  • 打赏
  • 举报
回复
HIBERNATE.org Forum给的方法很好

面向一个接口,生成不同的子类

可能不够动态,不过也可以用了


icecloud 2003-12-09
  • 打赏
  • 举报
回复
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
Expert



Joined: 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 User



Joined: 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 User



Joined: 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
Expert



Joined: 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 Team



Joined: 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 User



Joined: 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 User



Joined: 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 Team



Joined: 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

icecloud 2003-12-09
  • 打赏
  • 举报
回复
以上是TSS给的答案
icecloud 2003-12-09
  • 打赏
  • 举报
回复
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.
icecloud 2003-12-09
  • 打赏
  • 举报
回复
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.
icecloud 2003-12-07
  • 打赏
  • 举报
回复
Oh!! Shit!! 居然要实现几十个method

真麻烦!!!
icecloud 2003-12-07
  • 打赏
  • 举报
回复
up...
icecloud 2003-12-07
  • 打赏
  • 举报
回复
另外已经提交了JIRA给hibernate,希望开发人员回应一下,并能够在今后的版本中实现dynamic table name
icecloud 2003-12-07
  • 打赏
  • 举报
回复
再等几天看看TSS有没有人回复~~ 然后再结帖
miwoo 2003-12-07
  • 打赏
  • 举报
回复
>居然要实现几十个method<
正常
icecloud 2003-12-06
  • 打赏
  • 举报
回复
上面的robbin的方案是可行的。但是做起来比较麻烦的样子。一会儿看看ClassPersiter的相关东西。

在forum.hibernate.org有人回复我,说是用view,一个大表建立view。还有人说,用subclass.

具体的subclass方法我想了一下,如果是用于静态的表是可以实现的。
比如,我们做一个新闻网站,有很多种新闻,娱乐的体育的社会的国内的等。
要放入不同的表,但是每个表结构都一样。

那么,我们首先建立一个News.class
然后建立多个SportNews.class等,来继承News.class
为每个写一个hbm.xml,这样完全可以满足一个新闻系统仅对News.class
进行所有操作。

但是,还是不能满足我们在Runtime时生成的新表。
看来还是比较复杂。

只好去研究ClassPersister了
icecloud 2003-12-06
  • 打赏
  • 举报
回复
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具有极大的可扩展性,例如支持存储过程,视图的调用,支持动态表名,动态表字段等等。
icecloud 2003-12-06
  • 打赏
  • 举报
回复
to miwoo(miwoo)
那有什么办法能实现类似的东西?动态使得某个vo/po类对应到新表
thuers 2003-12-06
  • 打赏
  • 举报
回复
qiang.


只有学习。
asdmonster 2003-12-06
  • 打赏
  • 举报
回复
mark
icecloud 2003-12-06
  • 打赏
  • 举报
回复
现在的问题是,如果我像上边那样重新buildsession那我如何实现两个表的union查询?
icecloud 2003-12-06
  • 打赏
  • 举报
回复
在官方论坛找到了几个回复,不知道行不行。继续关注ing

1,修改mapping的table,但是每次还要重新建立session

cfg.getClassMapping(...).getTable().setName(...);
userSessionFactory = cfg.buildSessionFactory();

2,CGLIB可以动态的建立一个bean

加载更多回复(11)

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧