File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Different Objects from one table? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Different Objects from one table?" Watch "Different Objects from one table?" New topic
Author

Different Objects from one table?

J Sellers
Greenhorn

Joined: Mar 06, 2009
Posts: 10
Hello all, I'm working on a new project to help me come to terms with Hibernate/JPA and had a question. I currently have ClassA which uses the "object" table:


This works fine and all is well.

My issue is that inside of this table there is a column called "type" so the rows inside of this table can actually be one of three types (A, B or C). Right now they all get lumped under ClassA. I'd like to make a Class for each "type" though, is there a way using annotations that I can kind of filter out the results of this table based on the "type" column?

So I'd like to make ClassA, ClassB, and ClassC that all use table "object" but filter based on the column "type". They'll all have the same attributes but each might have different methods associated with them because they each do different things.

I'm working with an existing database/application so I can't change the actual database layout or anything, this is basically a read only type app.

I hope that makes sense.

TIA
Ran Pleasant
Ranch Hand

Joined: Jan 16, 2003
Posts: 75
Inheritance in JPA or Hibernate will take care of what you need. Just make A, B, and C all subclasses of an Abstract class and map them in.

Of course, if A, B, and C cannot logically be subclasses of the same super class then you need to reconsider the design of the table.


Ran
Ganesh Shirsat
Ranch Hand

Joined: Jan 10, 2010
Posts: 33

Hi

you can used the discriminator concept of hibernate.. "multiple class for one table"

using discriminator you can specify the object type and accordingly that hibernate will fetch the record for your type


Thanks
Ganesh Shirsat
J Sellers
Greenhorn

Joined: Mar 06, 2009
Posts: 10
Ganesh Shirsat wrote:Hi

you can used the discriminator concept of hibernate.. "multiple class for one table"

using discriminator you can specify the object type and accordingly that hibernate will fetch the record for your type


Thanks
Ganesh Shirsat


Ganesh, this is what I was looking for, thanks.

I do have an issue though now. I've used some of the examples I've found but they don't seem to be working. The first class will pull all the records from the table despite their "type" and the second class that extends that first class pulls nothing. Below is the code I'm using, do you see anything wrong with it?

I'm doing just a simple Hibernate createCriteria(ObjOne.class).list() to test this if that helps.





J Sellers
Greenhorn

Joined: Mar 06, 2009
Posts: 10
Never mind, it appears I've figured it out (at least for now)
 
wood burning stoves
 
subject: Different Objects from one table?