-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Entity Manager Standalone
PostPosted: Sun Oct 22, 2006 2:03 pm 
Newbie

Joined: Sun Oct 22, 2006 1:56 pm
Posts: 5
I am trying to put hibernate-entity-manager to work.
I don't know how to avoid using jndi. Is it necesary?

this is the error:


Code:
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.TransactionException: Could not find UserTransaction in JNDI:
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
   at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:558)
   at org.hibernate.ejb.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:81)
   at org.hibernate.ejb.EntityManagerImpl.<init>(EntityManagerImpl.java:37)
   at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:37)
   at org.hibernate.ejb.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:32)
   at org.josuealcalde.forums.main.Main.main(Main.java:15)
Caused by: org.hibernate.TransactionException: Could not find UserTransaction in JNDI:
   at org.hibernate.transaction.JTATransaction.<init>(JTATransaction.java:64)
   at org.hibernate.transaction.JTATransactionFactory.createTransaction(JTATransactionFactory.java:57)
   at org.hibernate.jdbc.JDBCContext.getTransaction(JDBCContext.java:193)
   at org.hibernate.impl.SessionImpl.getTransaction(SessionImpl.java:1315)
   at org.hibernate.ejb.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:452)
   ... 5 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
   at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
   at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
   at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
   at javax.naming.InitialContext.lookup(InitialContext.java:351)
   at org.hibernate.transaction.JTATransaction.<init>(JTATransaction.java:60)
   ... 9 more



The main code failing is:

Code:
   
public static void main(String[] args) {
      EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("forums");
      EntityManager em = emf.createEntityManager();
      em.close();
   }



And my persistence.xml:


Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
   version="1.0">
   <persistence-unit name="forums" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jar-file>Forums-Persistence.jar</jar-file>
      <class>org.josuealcalde.forums.entity.ForumBean</class>
      <class>org.josuealcalde.forums.entity.MessageBean</class>
      <class>org.josuealcalde.forums.entity.TopicBean</class>
      <class>org.josuealcalde.forums.entity.UserBean</class>
      <properties>
         <property name="hibernate.transaction.factory_class"
            value="org.hibernate.transaction.JTATransactionFactory" />
         <property name="hibernate.dialect"
            value="org.hibernate.dialect.MySQL5InnoDBDialect" />
         <property name="hibernate.hibernate.ejb.cfgfile"
            value="hibernate.cfg.xml" />
         <property name="hibernate.hbm2ddl.auto" value="validate" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.connection.driver_class"
            value="com.mysql.jdbc.Driver" />
         <property name="hibernate.connection.password"
            value="forum" />
         <property name="hibernate.connection.url"
            value="jdbc:mysql://localhost/forum" />
         <property name="hibernate.connection.username"
            value="forum" />
         <property name="hibernate.default_catalog" value="forum" />
         <property name="hibernate.dialect"
            value="org.hibernate.dialect.MySQL5InnoDBDialect" />

      </properties>
   </persistence-unit>
</persistence>



Top
 Profile  
 
 Post subject: JTA is not available "as is" in J2SE
PostPosted: Mon Oct 23, 2006 2:30 pm 
Newbie

Joined: Wed Oct 18, 2006 11:18 am
Posts: 9
Location: Nanterre, France
Hi.

You've configured Hibernate's EM to use a JTA transaction factory, but JTA is not available "as is" in a standalone environment (it's a standard component of J2EE, not J2SE).

Simply remove the "transaction-type" attribute from the "persistence-unit" element and the "hibernate.transaction.factory_class" property from you persistence.xml file and it should be ok:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
   version="1.0">
   <persistence-unit name="forums">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jar-file>Forums-Persistence.jar</jar-file>
      <class>org.josuealcalde.forums.entity.ForumBean</class>
      <class>org.josuealcalde.forums.entity.MessageBean</class>
      <class>org.josuealcalde.forums.entity.TopicBean</class>
      <class>org.josuealcalde.forums.entity.UserBean</class>
      <properties>
         <property name="hibernate.dialect"
            value="org.hibernate.dialect.MySQL5InnoDBDialect" />
         <property name="hibernate.hibernate.ejb.cfgfile"
            value="hibernate.cfg.xml" />
         <property name="hibernate.hbm2ddl.auto" value="validate" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.connection.driver_class"
            value="com.mysql.jdbc.Driver" />
         <property name="hibernate.connection.password"
            value="forum" />
         <property name="hibernate.connection.url"
            value="jdbc:mysql://localhost/forum" />
         <property name="hibernate.connection.username"
            value="forum" />
         <property name="hibernate.default_catalog" value="forum" />
         <property name="hibernate.dialect"
            value="org.hibernate.dialect.MySQL5InnoDBDialect" />

      </properties>
   </persistence-unit>
</persistence>


Note: it is not mandatory to explicitly define the "provider" class, unless you're using several providers in the same application.

Note: you'll also have to handle the transaction boundary programmatically when it will be about creating/updating/deleting entities:
Code:
em.getTransaction().begin();
// ... modify entities
em.getTransaction().commit();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.