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

How to obtain configuration object?

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JBOSS4.2 + JPA

I need to check if name query exists? i saw that configuration object has this data. i wonder i can i obtain this object in JPA?


Thank you
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about called getNamedQuery, or whatever the actual method name is on EntityManager, and if it returns null, then you know it doesn't exist.

You should be able to get the Configuration object from the EntityManagerFactory, although again, I am not positive that that method is in JPA, I am too used to Hibernate's classes, and haven't gotten the time to memorize the apis. Sorry.

Mark
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getNameQuery throw an exception in case that the query dosnt exists. i need to check if query exists.

entitymanagerfactory or sessionfactory dosnt have get for configuration.
do you know how in hibernate i can obtain it?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by avihai marchiano:
getNameQuery throw an exception in case that the query dosnt exists. i need to check if query exists.

entitymanagerfactory or sessionfactory dosnt have get for configuration.
do you know how in hibernate i can obtain it?


So then catch the exception and if it is thrown you know the query doesn't exist.

Mark
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the exception cause to rollback.
what i did is un ugly trick i get in reflection (private field )this field from seesionfactoryimpl
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by avihai marchiano:
the exception cause to rollback.
what i did is un ugly trick i get in reflection (private field )this field from seesionfactoryimpl


I was actually thinking about your issue today while Snowboarding. I was just finding it odd that your might ever be passing a String for a named query that doesn't exist. Like a user types in a name in some UI and it is just passed.

Anyway, Looking at the Javadoc for Configuration, and why you can't get to it, is

"The Configuration is meant only as an initialization-time object. SessionFactorys are immutable and do not retain any association back to the Configuration."

So it actually no longer exists as a Configuration object after it has already loaded the information. It also appears that the mapping is then stored in ClassMetaData and CollectionMetaData objects, so if you wanted to find out about the mapping for a particular class you can call getClassMetaData passing either a Class or String(name of the entity) to get it. And have fun with it.

Mark
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The configuration object is a pretty handy component to have, so I always like my HibernateUtil, or HibernateService class provide a method that returns the Configuration object, or with JPA annotations, the AnnotationConfiguration object.



This is not only helpful providing a SessionFactory and subseqently, a Hibernate Session, but can also be useful doing a SchemaExport and recreating your database based on changes to your annotated object model.

Here's a tutorial on creating a simple HibernateUtil class:

How to Create a Hibernate Util Class

-Cameron McKenzie


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic