Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Hibernate and JPA Training" Question: Hibernate Meta Model

 
atul khot
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
We have a very big table with hundreds of fields...
We team members end up writing JPQL queries for fetching subsets of these columns...
For example, C1, C2, .... C200..
In one context you need C1, C9, C22
In another context you need C2, C5, C9, C29, C52

These queries are growing as we are making more progress...

I was thinking of solving this problem generically - instead of writing separate queries -

Searched around - and came upon Hibernate Meta Model...

Q1. Is this the right approach for the problem (or is there something better out there?)

Q2. Why is this called meta model? (I read about it - but somehow have a feeling I did not understand it that well)...

Any gotchas to be aware of...

Would the course cover it...
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Metamodels are static so that would be the opposite of what you are trying to achieve as you would then be writing queries against specific columns still. They are intended to be type safe, so for example you won't be able to query for a column called CC100 if no such column exists. If you give up type safety then you can write your own generic search method that searches for any combination of columns from any tables. Others have already done it and you can use their code or adapt it to your needs. Here is one example http://code.google.com/p/hibernate-generic-dao/
 
Richard Chesterwood
Virtual Pair Programmers Rep
Greenhorn
Posts: 29
7
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, good answer above. We do cover meta model in the Criteria API chapter of the course but it doesn't sound like that's what you need.

If the problem is you've hundreds of columns in a table and you're having to write different queries to access different subsets of your data, I'd be interested to know what value you're getting from hibernate here. You haven't mentioned objects and classes at all, so I'd like to ask how you are mapping this mammoth table to classes?

Off the top of my head I'm not aware of any framework for generating the queries you need, but I will have a think.
 
atul khot
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the answers! These are static columns - and we have some 200+ columns - sorry if I came across as far too many columns...

Richard> If the problem is you've hundreds of columns in a table and you're having to write different queries to access
Richard> different subsets of your data, I'd be interested to know what value you're getting from hibernate here.

It helps us to think in terms of Java objects and OO - There are other entities/tables there -
It is a Spring ROO application - and it is just a snap to add columns...
We set the hibernate.hbm2ddl.auto property to update
And the ROO shell makes it super easy ;-)

Using hibernate helps a lot with writing integration tests... (We use the active record pattern a lot).

Caching other often needed entities...
and related optimizations (we might want to add the second level cache sometime in the future)...

Richard> You haven't mentioned objects and classes at all,
Richard> so I'd like to ask how you are mapping this mammoth table to classes?

Just one class - I have a strong hunch - this is really not a RDBMS problem (as many columns are always left null) -
we are soon going to NoSql (cassandra/Riak) - and use Spring Data...

But as a first cut solution it is working fine - there are other non-technical factors (out of my control ;-)
- for not taking the NoSql route right away...

--- regards atul
 
Richard Chesterwood
Virtual Pair Programmers Rep
Greenhorn
Posts: 29
7
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, thanks for the additional information. It does sound from what you've said that the design has gone astray, a class with 200 attributes is something of a red flag! Plus the null columns.

> In one context you need C1, C9, C22
> In another context you need C2, C5, C9, C29, C52

This is a clue, it suggests that you need one class mapped to C1, C9, C22, and a second class mapped to C2, C5 ,C29, C52. Then (I suspect) your queries will end up much simpler. The presence of null columns suggests some inheritance might be helpful also.

However, it's the real world and if your first cut solution is working fine, it might just be a case of accepting the pain of writing the large collection of queries?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic