Unknown entity problem in Hibernate with AnnotationConfiguration - java

I've been googling for this problem from last 2 days but havent found any proper solution. I am developing a desktop application with Hibernate. I am using AnnotationConfiguration for registering entities in hibernate. I am not creating xml mapping files as I am using persistence type entity beans. I've tried it with configuration xml file, properties file, programmatic registration of entity beans, but every time i am getting the same exception.
I've checked the entity class. Its importing javax.persistence.Entity; I've also tried adding #org.hibernate.annotations.Entity(mutable=true, dynamicInsert=true, dynamicUpdate=true) but no success.
When I pass incorrect path of one of the entity bean, hibernate initialization fails, which means before changing path, hibernate was picking up a correct entity file.
Is there any way to get a list of registered entities in hibernate configuration?

Have you declared the entities inside Your hibernate.cfg.xml file?
example:
<mapping class="test.User"/>

Related

Hibernate over JPA configurtation xml file

I know it's possible to configure Hibernate in the 2 following ways:
hibernate.cfg.xml
persistence.xml - JPA - with specific hibenate configuration
when you add the provider like this:
org.hibernate.ejb.HibernatePersistence
But , I don't understand when should I use which ?
What should be the correct behavior?
Thanks!
You can use hibernate.cfg.xml when you want to use Hibernate in your project. When you create different queries (SELECT, INSERT, etc) you open session.
Session session = sessions.openSession();
where session is instance of org.hibernate.Session.
But as you can see we need sessions for creating session. Sessions is instance of org.hibernate.SessionSessionFactory. SessionFactory is global factory for concrete DB.
Session can be got by this action:
SessionFactory sessions = new Configuration().configure().buildSessionFactory();
where new Configuration().configure().buildSessionFactory() - parses hibernate.cfg.xml
Persistence.xml contains settings for using JPA in your project. Persistence units are defined in a persistence.xml file, which has to be located in the META-INF directory in the classpath. One persistence.xml file can include definitions for one or more persistence units. The portable way to instantiate an EntityManagerFactory in JPA (as explained in the JPA Overview section) requires a persistence unit.
See also: http://www.objectdb.com/java/jpa/entity/persistence-unit

connect to only one schema in hibernate

I am usning jBoss tools to generate POJO's from tables in my database but my database has many schemas so after configuration it took half of the day to show all the schemas.
Kindly let me know if there is any way to connect to the particular schema in database or any other property need to be defined in cfg file.
Thanks
You need to remove the schema information present in your POJO classes which you have generated through the reverse Hibernate mechanism of your JBOSS tools.Just create different hibernate configuration files(cfg) and supply the schema information.Hibernate will configure it accordingly.
So all you need to do is create a separate cfg file for each schema with your same POJO files included in both config files

How to use Spring Data JPA in a simple Servlet?

I'm very new to Spring, but I am working on a project which is using Spring Data JPA to generate repositories for JPA entities.
I'm currently adding a simple module to be able to show some data on a webpage. I have added a Servlet, but I am having trouble accessing the repositories from there.
I have added a ContextLoaderListener in web.xml, I'm referencing the jpa:repositories and persistence.xml in the applicationContext.xml, but I'm currently stuck with this exception:
No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0.
But when I add an EntityManagerFactory in persistence.xml I get the following cryptic message:
java.lang.IllegalAccessError: tried to access field
org.hibernate.engine.spi.CascadeStyle.STYLES from class
org.hibernate.engine.spi.EJB3CascadeStyle
My question is: is what I am trying to do even possible? And if so, how?
Or should I just bite the bullet and use Spring MVC or something else entirely?
Note: this is just for a one-page web site and I'm trying to keep it as simple as possible.
In order to use Spring Data JPA you need to configure the underlying JPA implementation as you would typically do in Spring, see for example, infrastructure.xml and META-INF/persistence.xml in spring-data-jpa-showcase (since Spring 3.1 you can get rid of persistence.xml if you use packagesToScan property of LocalContainerEntityManagerFactoryBean).
Your second problem with IllegalAccessError looks like a classloading problem caused by presence of different versions of Hibernate jars in classpath.

spring web MVC and hibernate setup - how to get a hold of your hibernate bean?

I am new to Spring and the bean concept, so sorry if asking the obviously.
I have set up a Java project and used the Hibernate framework to make my connections to the DB (using hibernate tool on eclipse, really recommended btw).
My basic setup was as follows:
some hibernate pojos (generated by Hibernate tools),
hibernate mapping files (hbm.xml files).
The configuration was set in hibernate.cfg.xml file placed at my root of the packages.
I initially setup a HibernateUtils class that gets a session from my sessionFactory.
some classes that act as Dao - creates the queries using the pojos and the hibernateUtils to get the session.
This was working fine with hibernate's powerful framework. Then I migrated to project to Spring MVC. From the various tutorials I read I understood that some changes need to be done in order to make things play nicely again. The main change is that the Spring FW now loads the beans by itself, therefore the following:
the pojos did not change, nor did the hbm.xml files
The configuration is no longer set at hibernate.cfg.xml - the Spring MVC FW loads it's own LocalSessionFactoryBean once I declared it in the XML files (Spring search for xmls as part of the initialization process). So I created a separate xml file called spring-hibernate.xml, that has the DB definition, the session factory bean and a hibernate template. To complete it, the xml file also declare the location of the Dao and the hbm files.
with hibernate template configured in the xml, there is a bean injection, therefore I changed the Dao files to add getter/setter to a hibernate template property. Then using this - the Dao class may create the db query.
All of this configuration works fine and I can get the Spring FW initialize the Dao classes (I put a sysout just to confirm that the Dao classes are injected with the hibernate template during MVC startup). Now my problem is - how to get access to the Dao bean instance created?
In my logic part, I get a hold of the bean as follows:
ClassPathXmlApplicationContext appContex = new ClassPathXmlApplicationContext(new String[] {"spring-hibernate.xml"});
UserUIDDao userUIDDao= (UserUIDDao)appContext.getBean("UserUIDDao");
Problem is - this is a NEW reference to a newly created bean and not the same one created during the spring MVC startup.
So my questions are: how to get a hold of the original bean created by the Spring init process? and - is my setup correct?
the pojos did not change, nor did the hbm.xml files
That's correct.
The configuration is no longer set at hibernate.cfg.xml - the Spring MVC FW loads it's own LocalSessionFactoryBean once I declared it in the XML files (Spring search for xmls as part of the initialization process).
You can choose to use Spring to configure Hibernate (but you could keep your hibernate.cfg.xml).
So I created a separate xml file called spring-hibernate.xml, that has the DB definition, the session factory bean and a hibernate template. To complete it, the xml file also declare the location of the Dao and the hbm files. with hibernate template configured in the xml, there is a bean injection, therefore I changed the Dao files to add getter/setter to a hibernate template property. Then using this - the Dao class may create the db query.
Using Spring's HibernateTemplate is one option, but you could also go template-less and just inject Hibernate's SessionFactory and use sessionFactory.getCurrentSession().
This is actually the officially recommended approach for new projects, check out So should you still use Spring's HibernateTemplate and/or JpaTemplate?? and the javadoc of HibernateTemplate.
Now my problem is - how to get access to the Dao bean instance created?
You inject it where needed (typically, in your services).
In my logic part, I get a hold of the bean as follows (...)
No, don't do this. This "service locator approach" actually defeats the point of a DI container like Spring. Configure Spring to inject your DAOs where needed.

Spring, db and property file configuration

I have some JDO objects that I want to spring to configure with info from a property file.
But since spring isn't used to create (i.e these objects are not listed as beans in the xml. Should they, how would it look?) these objects how can I get it to configure those objects?
Only solution I can come up with is to have the property file info configured into the dao and then have the dao insert that data into the object before it returns it. Or I guess I can do some AOP magic, but that seems heavy handed and I don't think it will work in Google App Engine where this service will be deployed.
Any other advice.
You can put any bean in applicationContext.xml, and configure all of its properties there.
The properties file can be loaded via:
<context:property-placeholder location="classpath:application.properties" />
and then, on your bean definition:
<property name="propertyName" value="${valueFromPropertiesFile}" />
Then, in order to have the properly configured bean, you will have to inject it - either in the applicationContext.xml, or via #Resource / #Autowired
But if you can't let spring create, and configure your beans, then simply populate them with your properties manually - load a properties file with java.util.Properties, and fill the data needed.
I have some JDO objects that I want to spring to configure with info from a property file.
I don't get the whole idea. Are these objects persistent or not? If they are, just load them from the datastore. If not, they aren't really JDO objects as pointed out in comments. And in that case, I don't understand the point of the DAO and of the property file. Why don't you just declare them as Spring beans?

Categories

Resources