开发者

Java Hibernate service class

I have a painful problem regarding Java Hibernate. I have my own POJO classes in 开发者_StackOverflow中文版my modell, and I would like to submit query from an instance of that class, to a table of my relational DB. The problem is, that I want to submit several different queries to different tables. For example: I have a Bus class, representing buses. I have a BusesOnTheLine table, a BusesWaitingForRepair table. Now sometimes I have to add a new line to BusesOnTheLine table, sometimes to the other. Each table has its own scheme, so I do not have a (bijective) one-to-one correspondence. I guess there should be a service class like it is in .NET, that might has a method for each of these queries with HQL (Hibernate Query Langugage) but I am not able to find it. :( Any idea about the solution?


Sounds like a badly normalized design in your database to me. It's not a flaw in Hibernate.

I agree with a Bus object; I agree with a BUS table. But why do you have several tables that look like Buses in different states?

I'd recommend a BUS_STATUS table with rows like "ON_THE_LINE", "WAITING_FOR_REPAIR" and a foreign key in the BUS table that points to the primary key of the current status. Do a JOIN between the two.

Now your Bus class has a one-to-one mapping with its Status object and you're all set.


If you have several tables that all represent buses in different states then you are probably looking for an inheritance using table per sub class strategy. That is, each status of bus may be a sub class of bus, then hibernate does a insert into the right table for you when you save a bus. When querying you can query against the bus in which case you get all buses, or you can query against BussesOnTheLine in which case only get those buses.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜