views:

80

answers:

2

Dear All!

I have a painful problem regarding Java Hibernate. I have my own POJO classes in 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?

+1  A: 

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.

duffymo
Yeah, but the thing is, that I have to store other things also, like timestamp for entering the line, etc...You say, I should just generate subclasses of Bus, and use them to store and retrieve data from the DB? I am just not happy with the idea to create an instance of these subclasses to save data to the DB. I hoped there is a service class-like solution in Hibernate, where I can pass a Bus to one of its method, and that method does the DB query for me.
gmate
No, I didn't say subclasses at all. One class, Bus, with a status that can change over time. If you have a requirement to maintain a history of status for a particular Bus, then create a Status object that has a value and a timestamp and and let it have a 1:m relationship with Bus. Hibernate is about persistence; services are a separate matter, IMO.
duffymo
A: 

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.

Vincent Ramdhanie