• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Please HELP with Hibernate Mapping!

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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's just a flesh wound! Or a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic