• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Annotations

 
Selvakumar Kumarasamy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use Hibernate with Annotation without any EJBs. That means, my application contains only Web Project. Where to place the persistence.xml file in the web project.

Please clarify.
 
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
My scja.com website, uses Hibernate with JPA annotations running on a Tomcat server. No EJBs are used at all. It's not a complex site, with most of the data being pushed and pulled to provide some mock SCJA exam questions, but it's Hibernate and JPA doing the work. That's one of the promises of Hibernate and to a lesser extent, JPA - that you can do persistence with POJOs and not the traditional overhead of J2EE entity beans.

As far as setup goes, I like to develop the Hibernate code in a separate "Java Project" in Eclipse, and link to it from the web application. That creates a few extra steps. But a simple solution would be to just put all the required Hibernate and JPA related files in the WEB-INF\lib directory. Then you can reference the Hibernate and JPA classes in any of your web components or Java code. It all gets deployed together, and runs self-contained on a Tomcat or any other standard servlet engine.

-Cameron McKenzie
[ May 06, 2008: Message edited by: Cameron Wallace McKenzie ]
 
Selvakumar Kumarasamy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. My constraint in I need to use Annotations. that means all the table-java object mappings should be configured with annotaions and not through configuration files. Only persistence.xml file should be present. In this case where to store the persistence.xml file. If I use EJBs I can place the same in META-INF folder.

Kindly clarify.
 
Ray Johnson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Persistence.createEntityManagerFactory() will always search for persistence.xml under META-INF/. So place your persistence.xml under web-inf/classes/META-INF and try. I assume you are not packaging your entities in a jar file, but simply having them under classes folder. Please post your findings. It may be useful to others too.
 
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 Selvakumar Kumarasamy:
Is it possible to use Hibernate with Annotation without any EJBs. That means, my application contains only Web Project. Where to place the persistence.xml file in the web project.

Please clarify.


Of course you can, as was stated. Just make sure that the persistence.xml is in the root of your classpath.

Mark
 
Selvakumar Kumarasamy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As adviced I have placed persistence.xml under WEB-INF\Classes\META_INF folder. When I tried to create EntityManagerFactory using Persistence.createEntityManagerFactory(<Persistence Unit Name> I am getting the following error.

I have included the following jar files in the project.

antlr-2.7.6.jar
cglib.jar
commons-beanutils-bean-collections.jar
commons-beanutils-core-1.7.0.jar
commons-collections-2.1.1.jar
commons-digester-1.7.jar
commons-logging-1.0.4.jar
commons-validator-1.3.1.jar
dom4j.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
ejb3-persistence.jar
EnvironmentProperties-1.0.2.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
j2ee.jar
javassist.jar
jboss-archive-browsing.jar
jboss-common.jar
jdom-1.0.jar
log4j-1.2.11.jar


Error as follows:
=================

[5/11/08 12:01:05:280 IST] 00000039 SystemErr R javax.persistence.PersistenceException: No Persistence provider for EntityManager named SCBP_PU
[5/11/08 12:01:05:280 IST] 00000039 SystemErr R at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89)
[5/11/08 12:01:05:296 IST] 00000039 SystemErr R at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)
[5/11/08 12:01:05:296 IST] 00000039 SystemErr R at com.travelers.cd.dao.CDDAO.getEntityManager(CDDAO.java:35)
[5/11/08 12:01:05:296 IST] 00000039 SystemErr R at com.travelers.cd.dao.CDDAO.getIndicator(CDDAO.java:19)


Please help me in resolving the issue.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WEB-INF\Classes\META_INF

Should be

WEB-INF\classes\META-INF
[ May 11, 2008: Message edited by: Gregg Bolinger ]
 
Selvakumar Kumarasamy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it was META-INF only. It was a spelling mistake.
 
Selvakumar Kumarasamy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am able to query the data base with the above setup. Now I get a new problem. Whenever I start the server freshly and run the application it's working fine. But if I am updating anything in the application and publish it to the server I am not able to query the database and I am getting the following error. If I again restart the server it is working fine. Can anyonle help.

[5/14/08 17:34:49:482 IST] 00000029 SystemErr R WARNING: Found unrecognized persistence provider "org.hibernate.ejb.HibernatePersistence" in place of OpenJPA provider. This provider's properties will not be used.
[5/14/08 17:34:49:779 IST] 00000029 SystemErr R javax.persistence.PersistenceException: org.hibernate.HibernateException: Could not instantiate dialect class
[5/14/08 17:34:49:779 IST] 00000029 SystemErr R at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:737)
[5/14/08 17:34:49:779 IST] 00000029 SystemErr R at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
[5/14/08 17:34:49:779 IST] 00000029 SystemErr R at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)


[5/14/08 17:34:49:779 IST] 00000029 SystemErr R Caused by: org.hibernate.HibernateException: Could not instantiate dialect class
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:84)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:42)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:128)
===========================================================================

my persistence.xml content is as follows:
============================================

<property name="dialect" value="org.hibernate.dialect.DB2Dialect"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup"/>
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why two different dialect properties?

<property name="dialect" value="org.hibernate.dialect.DB2Dialect"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>

Also, if you do get a new different issue, it might be better to just create a new thread instead.

Thanks

Mark
 
Selvakumar Kumarasamy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if I use one property I am getting the same error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic