• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Different Objects from one table?

 
J Sellers
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ganesh Shirsat
Ranch Hand
Posts: 33
Google Web Toolkit Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind, it appears I've figured it out (at least for now)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic