• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please HELP with Hibernate Mapping!

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table call CATS, a POJO represent all the field getter/setter for all columns in CATS table, a mapping Cat.hbm.xml.
I need to have a query like this


How do i resolved this problem, if I add <property name="cattype" column="cattype" /> in the mapping, then it is not rigth bc cattype is not a column of the CATS table.

Please help.
Greately appreciated
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is an odd data model. If a cat can only have one type (as your bit of SQL implies), why is cattype not related directly to cat?
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I can do the join like this


Thank you for your hints and advise.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still an odd data model to my mind. Does a cat have a type? Or multiple types?

If it is multiple types, then you need to map cattypes too. Then you need to map an association using some sort of Collection. Have a read of the Hibernate documentation for mapping associations, you'll see a few ways of doing this.
[ February 06, 2008: Message edited by: Paul Sturrock ]
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul
Yeap the cat has 1-N types, so you are sugesstion have a Set inside the Cat.java (POJO class)?

I read the documents, but not sure which one to use, any more hints or light will be really helpful.

Regards
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A set would do (unless of course the cat can be associated with the same type more then ones, which is allowed by your data model).

Have a look at the Collections mapping section. Depending on your model one of the Collections will do.
 
Gaurav Bharadwaj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try creating a constructor parametrised constructor of cats

like
cats(birthdate,id,color,sex,weight,cattype){
...
...
}

and then firing HQL query like

select new *.*.*.cats(cats.birthdate,
cats.id,
etc,
(select type from cattype where cattypeid in cats.id) as cattype
)
from cats

I am using this type of queries in my application and they works...
however you might face problem as these objects are not properly associated and cached in session....
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for suggestion, I will try Gaurav Bharadwaj ' way, and let's see how it works out.
Again, many thnks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic