-->
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.  [ 7 posts ] 
Author Message
 Post subject: no saving objects to MySQL InnoDB using JTA in JBoss
PostPosted: Tue Nov 21, 2006 7:30 am 
Newbie

Joined: Tue May 16, 2006 3:35 pm
Posts: 7
Hi, I have a problem saving objects into InnoDB MySQL database using JTA transactions within my JBoss and Hibernate Entitymanager environment. No exceptions or/and errors were thrown. The object is constructed correct in memory, but no saving to database does happen...

My current workbench looks like this:

JDK v1.5.0_10
MySQL v5.0.27 (InnoDB)
MySQL JConnector v3.1.14
JBoss v4.0.5GA (EJB3.0 RC9 / Hibernate EntityManager v3.2.0GA)
NetBeans v5.5

Here a piece of the console stack trace of project deployment (everything looks fine, I think):

Code:

[EARDeployer] Init J2EE application: file:/D:/Verzeichnis/_JBoss_/server/default_ejb3/deploy/BenchmarkEJB3.ear
[Ejb3Deployment] EJB3 deployment time took: 2073
[JmxKernelAbstraction] installing MBean: persistence.units:ear=BenchmarkEJB3.ear,jar=BenchmarkEJB3-ejb.jar,unitName=BenchmarkEJB3-ejbPU with dependenci
[JmxKernelAbstraction]       jboss.jca:name=BenchmarkDS,service=ManagedConnectionFactory
[Version] Hibernate EntityManager 3.2.0.GA
[Version] Hibernate Annotations 3.2.0.GA
[Environment] Hibernate 3.2.0
[Environment] hibernate.properties not found
[Environment] Bytecode provider name : javassist
[Environment] using JDK 1.4 java.sql.Timestamp handling
[Ejb3Configuration] found EJB3 Entity bean: benchmark.database.entities.Entity0
[Ejb3Configuration] found EJB3 Entity bean: benchmark.database.entities.Entity1
[Ejb3Configuration] found EJB3 Entity bean: benchmark.database.entities.Entity2
[Ejb3Configuration] Persistence provider caller does not implements the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
[Configuration] Reading mappings from resource: META-INF/orm.xml
[Ejb3Configuration] [PersistenceUnit: BenchmarkEJB3-ejbPU] no META-INF/orm.xml found
[AnnotationBinder] Binding entity from annotated class: benchmark.database.entities.Entity0
[EntityBinder] Bind entity benchmark.database.entities.Entity0 on table Entity0
[AnnotationBinder] Binding entity from annotated class: benchmark.database.entities.Entity1
[EntityBinder] Bind entity benchmark.database.entities.Entity1 on table Entity1
[AnnotationBinder] Binding entity from annotated class: benchmark.database.entities.Entity2
[EntityBinder] Bind entity benchmark.database.entities.Entity2 on table Entity2
[CollectionBinder] Mapping collection: benchmark.database.entities.Entity0.entities0_1N -> Entity0
[CollectionBinder] Mapping collection: benchmark.database.entities.Entity1.entities1_1N -> Entity1
[ConnectionProviderFactory] Initializing connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
[InjectedDataSourceConnectionProvider] Using provided datasource
[SettingsFactory] RDBMS: MySQL, version: 5.0.27-community-nt
[SettingsFactory] JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.1.14 ( $Date: 2006-10-18 17:40:15 +0200 (Wed, 18 Oct 2006) $, $Rev
[Dialect] Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
[TransactionFactoryFactory] Transaction strategy: org.hibernate.ejb.transaction.JoinableCMTTransactionFactory
[TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
[TransactionManagerLookupFactory] instantiated TransactionManagerLookup
[SettingsFactory] Automatic flush during beforeCompletion(): disabled
[SettingsFactory] Automatic session close at end of transaction: disabled
[SettingsFactory] JDBC batch size: 15
[SettingsFactory] JDBC batch updates for versioned data: disabled
[SettingsFactory] Scrollable result sets: enabled
[SettingsFactory] JDBC3 getGeneratedKeys(): enabled
[SettingsFactory] Connection release mode: auto
[SettingsFactory] Maximum outer join fetch depth: 2
[SettingsFactory] Default batch fetch size: 1
[SettingsFactory] Generate SQL with comments: disabled
[SettingsFactory] Order SQL updates by primary key: disabled
[SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
[ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
[SettingsFactory] Query language substitutions: {}
[SettingsFactory] JPA-QL strict compliance: enabled
[SettingsFactory] Second-level cache: disabled
[SettingsFactory] Query cache: disabled
[SettingsFactory] Optimize cache for minimal puts: disabled
[SettingsFactory] Structured second-level cache entries: disabled
[SettingsFactory] Statistics: disabled
[SettingsFactory] Deleted entity synthetic identifier rollback: disabled
[SettingsFactory] Default entity-mode: pojo
[SessionFactoryImpl] building session factory
[SessionFactoryObjectFactory] Factory name: java:hibernate/HibernateFactory
[NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jb
[NamingHelper] Creating subcontext: java:hibernate
[SessionFactoryObjectFactory] Bound factory to JNDI name: java:hibernate/HibernateFactory
[SessionFactoryObjectFactory] InitialContext did not implement EventContext
[NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jb
[JmxKernelAbstraction] installing MBean: jboss.j2ee:ear=BenchmarkEJB3.ear,jar=BenchmarkEJB3-ejb.jar,name=BenchmarkSessionBean,service=EJB3 with depende
[EJBContainer] STARTED EJB: ejb.BenchmarkSessionBean ejbName: BenchmarkSessionBean
[EJB3Deployer] Deployed: file:/D:/Verzeichnis/_JBoss_/server/default_ejb3/tmp/deploy/tmp57692BenchmarkEJB3.ear-contents/BenchmarkEJB3-ejb.jar
[TomcatDeployer] deploy, ctxPath=/BenchmarkEJB3-war, warUrl=.../tmp/deploy/tmp57692BenchmarkEJB3.ear-contents/BenchmarkEJB3-war-exp.war/
[EARDeployer] Started J2EE application: file:/D:/Verzeichnis/_JBoss_/server/default_ejb3/deploy/BenchmarkEJB3.ear



Here is my persistence.xml:

Code:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
  <persistence-unit name="BenchmarkEJB3-ejbPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:BenchmarkDS</jta-data-source>
    <properties>
      <property name="hibernate.session_factory_name" value="java:hibernate/HibernateFactory"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
      <property name="transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
      <property name="hibernate.cache.use_second_level_cache" value="false"/>
      <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
    </properties>
  </persistence-unit>
</persistence>



Here my simple code within my JavaEE5 project:

Code:

@PersistenceContext(name="BenchmarkEJB3-ejbPU") EntityManager entityManager;

Entity0 entity0 = new Entity0();
entity0.setAttribute0(123456);
entityManager.persist(entity0);



And here is my hibernate tools generated annotated Entity0 class:

Code:

package benchmark.database.entities;
// Generated 20.11.2006 18:31:39 by Hibernate Tools 3.2.0.beta8


import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
* Entity0 generated by hbm2java
*/
@Entity
@Table(name="Entity0"
)
public class Entity0  implements java.io.Serializable {

    // Fields   

     private int idEntity0;
     private long attribute0;
     private Entity0 entity0_N1;
     private Set<Entity0> entities0_1N = new HashSet<Entity0>(0);
     private Set<Entity1> entities1_0_MN = new HashSet<Entity1>(0);
     private Set<Entity1> entities1_1_MN = new HashSet<Entity1>(0);

     // Constructors

    /** default constructor */
    public Entity0() {
    }

   /** minimal constructor */
    public Entity0(long attribute0) {
        this.attribute0 = attribute0;
    }
    /** full constructor */
    public Entity0(long attribute0, Entity0 entity0_N1, Set<Entity0> entities0_1N, Set<Entity1> entities1_0_MN, Set<Entity1> entities1_1_MN) {
       this.attribute0 = attribute0;
       this.entity0_N1 = entity0_N1;
       this.entities0_1N = entities0_1N;
       this.entities1_0_MN = entities1_0_MN;
       this.entities1_1_MN = entities1_1_MN;
    }
   
    // Property accessors
     @Id @GeneratedValue
   
    @Column(name="idEntity0", unique=false, nullable=false, insertable=true, updatable=true)
    public int getIdEntity0() {
        return this.idEntity0;
    }
   
    public void setIdEntity0(int idEntity0) {
        this.idEntity0 = idEntity0;
    }
   
    @Column(name="attribute0", unique=false, nullable=false, insertable=true, updatable=true)
    public long getAttribute0() {
        return this.attribute0;
    }
   
    public void setAttribute0(long attribute0) {
        this.attribute0 = attribute0;
    }
@ManyToOne(cascade={},
        fetch=FetchType.LAZY)
   
    @JoinColumn(name="fk_idEntity0_N1", unique=false, nullable=true, insertable=true, updatable=true)
    public Entity0 getEntity0_N1() {
        return this.entity0_N1;
    }
   
    public void setEntity0_N1(Entity0 entity0_N1) {
        this.entity0_N1 = entity0_N1;
    }
    @OneToMany(cascade={}, fetch=FetchType.LAZY, mappedBy="entity0_N1")
    public Set<Entity0> getEntities0_1N() {
        return this.entities0_1N;
    }
   
    public void setEntities0_1N(Set<Entity0> entities0_1N) {
        this.entities0_1N = entities0_1N;
    }
    @ManyToMany(cascade={}, fetch=FetchType.LAZY, mappedBy="entities0_0_NM")
    public Set<Entity1> getEntities1_0_MN() {
        return this.entities1_0_MN;
    }
   
    public void setEntities1_0_MN(Set<Entity1> entities1_0_MN) {
        this.entities1_0_MN = entities1_0_MN;
    }
    @ManyToMany(cascade={}, fetch=FetchType.LAZY, mappedBy="entities0_1_NM")
    public Set<Entity1> getEntities1_1_MN() {
        return this.entities1_1_MN;
    }
   
    public void setEntities1_1_MN(Set<Entity1> entities1_1_MN) {
        this.entities1_1_MN = entities1_1_MN;
    }
}



After testing, object "entity0" is not saved to database, does anybody faced the same problem & can help?! Thx!


Last edited by Stygian on Sun Nov 26, 2006 7:10 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 23, 2006 1:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
your session bean probably does not start a transaction

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Line missing
PostPosted: Thu Nov 23, 2006 8:52 pm 
Newbie

Joined: Mon Oct 24, 2005 6:34 pm
Posts: 14
Location: Guadalajara, Jalisco. México.
... there is one important line missing:

Code:
@PersistenceContext(name="BenchmarkEJB3-ejbPU") EntityManager entityManager;

Entity0 entity0 = new Entity0();
entity0.setAttribute0(123456);
entityManager.persist(entity0);

entityManager.flush();  //  ... this was a headache as well :P


Top
 Profile  
 
 Post subject: Re: Line missing
PostPosted: Sun Nov 26, 2006 6:58 am 
Newbie

Joined: Tue May 16, 2006 3:35 pm
Posts: 7
bulloncito wrote:
... there is one important line missing:

Code:
@PersistenceContext(name="BenchmarkEJB3-ejbPU") EntityManager entityManager;

Entity0 entity0 = new Entity0();
entity0.setAttribute0(123456);
entityManager.persist(entity0);

entityManager.flush();  //  ... this was a headache as well :P


...no, "entityManager.flush()" is unfortunately not the solution, everything behave the same, after testing.

The following line seems to be the key line:

Code:
@PersistenceContext(name="BenchmarkEJB3-ejbPU") EntityManager entityManager;


When I implement it into my DAO class (no SessionBean!) everything behaves as I described above. But when I code the same into my SessionBean, "entityManager" stays "null" & I get a NullPointerException when I call "entityManager.persist(entity0);". Its difficult to decide whether the problem lies on JBoss or on Hibernate´s EntityManager side. Does anybody has some experience with this context & can give some hints?!


Top
 Profile  
 
 Post subject: NullPointerException
PostPosted: Fri Dec 01, 2006 7:38 pm 
Newbie

Joined: Mon Oct 24, 2005 6:34 pm
Posts: 14
Location: Guadalajara, Jalisco. México.
... nullPointerException means that jBoss is not injecting PersistenceContext, so entityManager is null, verify the name of the PersistenceContext, or search for it using jndi:
Code:
   new InitialContext().lookup("java:/EntityManagers/YourEntityManager") ;

... not sure about the path from java:/ but it should be easy to find in the docs, you can also try to find if there is an instance of entity manager to be injected, try »EJTools« to browse JNDI, maybe your EntityManager didn't even deployed correctly.


Top
 Profile  
 
 Post subject: JTA is Programmatically
PostPosted: Wed Dec 06, 2006 12:11 pm 
Beginner
Beginner

Joined: Thu Jul 06, 2006 11:09 pm
Posts: 25
Just as what in the EntityManager documentation said about its propagation:

The EntityManager is automatically propagated in the active transaction when using JTA (or something like this).

So, when using JTA as your transaction type, you need to programmatically declare the demarcation of your transaction.

Code:
UserTransaction tx = lookup("UserTransaction");

try {
tx.begin();

//do some work here.

tx.commit(); //this is where the EntityManager flushes or commits the changes.
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}


The EntityManager.flush() forces the EntityManager to commit/flush any changes queued in its memory to the database.

_________________
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 12:01 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you're use a sesison bean, then a declarative transaction boundary is enough, no need for programmatic boundaries
@TransactionType(REQUIRED)

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.