2 Replies Latest reply on Nov 3, 2011 5:36 AM by fhmaddi

    Seam Persistence- Can't Use The EntityManager

    fhmaddi
      Hi all,
      i'm getting a nervous breakdown while thinking on this topic ;)

      ==============================================================================
      What I use:
      seam 3, JBOSS AS 7.0.1, Primefaces
      ==============================================================================

      ==============================================================================
      What I did so far:
      1. added hibernate to maven:

      <?xml version='1.0' encoding='UTF-8'?>
      <project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
              xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd'>
              <modelVersion>4.0.0</modelVersion>

              <groupId>de.fhhannover.inform.dataqualitycheckingengine</groupId>
              <artifactId>de.fhhannover.inform.dataqualitycheckingengine</artifactId>
              <version>0.0.1-SNAPSHOT</version>
              <packaging>war</packaging>

              <name>Java EE 6 webapp project</name>
              <description>A starter Java EE 6 webapp project for use on JBoss AS 7, generated from the jboss-javaee6-webapp archetype</description>

              <repositories>
                      <repository>
                              <id>prime-repo</id>
                              <name>PrimeFaces Maven Repository</name>
                              <url>http://repository.primefaces.org</url>
                              <layout>default</layout>
                      </repository>

                      <repository>
                              <id>java-net</id>
                              <name>Java.net Maven Repository</name>
                              <url>http://download.java.net/maven/2</url>
                      </repository>

                      <!-- JBoss - Repository -->
                      <repository>
                              <id>jboss-public-repository-group</id>
                              <name>JBoss Public Maven Repository Group</name>
                              <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
                      </repository>

              </repositories>

              <properties>
                      <!-- Explicitly declaring the source encoding eliminates the following
                              message: -->
                      <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
                              resources, i.e. build is platform dependent! -->
                      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                      <!-- Timestamp format for the maven.build.timestamp property -->
                      <!-- You can reference property in pom.xml or filtered resources (must
                              enable third-party plugin if using Maven < 2.1) -->
                      <maven.build.timestamp.format>yyyyMMdd'T'HHmmss</maven.build.timestamp.format>
                      <!-- Specify the JBoss AS directory to be the JBOSS_HOME environment variable -->
                      <jboss.home>${env.JBOSS_HOME}</jboss.home>
              </properties>

              <dependencyManagement>
                      <dependencies>
                              <!-- Define the version of JBoss' Java EE 6 APIs we want to import. Any
                                      dependencies from org.jboss.spec will have their version defined by this
                                      BOM -->
                              <!-- JBoss distributes a complete set of Java EE 6 APIs including a Bill
                                      of Materials (BOM). A BOM specifies the versions of a 'stack' (or a collection)
                                      of artifacts. We use this here so that we always get the correct versions
                                      of artifacts. Here we use the jboss-javaee-web-6.0 stack (you can read this
                                      as the JBoss stack of the Java EE Web Profile 6 APIs), and we use version
                                      2.0.0.Beta1 which is the latest release of the stack. You can actually use
                                      this stack with any version of JBoss AS that implements Java EE 6, not just
                                      JBoss AS 7! -->
                              <dependency>
                                      <groupId>org.jboss.spec</groupId>
                                      <artifactId>jboss-javaee-web-6.0</artifactId>
                                      <version>2.0.0.Final</version>
                                      <type>pom</type>
                                      <scope>import</scope>
                              </dependency>
                      </dependencies>
              </dependencyManagement>

              <dependencies>
                      <!-- First declare the APIs we depend on and need for compilation. All
                              of them are provided by JBoss AS 7 -->

                      <!-- Import the CDI API, we use provided scope as the API is included in
                              JBoss AS 7 -->
                      <dependency>
                              <groupId>javax.enterprise</groupId>
                              <artifactId>cdi-api</artifactId>
                              <scope>provided</scope>
                      </dependency>

                      <!-- Import the Common Annotations API (JSR-250), we use provided scope
                              as the API is included in JBoss AS 7 -->
                      <dependency>
                              <groupId>org.jboss.spec.javax.annotation</groupId>
                              <artifactId>jboss-annotations-api_1.1_spec</artifactId>
                              <scope>provided</scope>
                      </dependency>

                      <!-- Import the JAX-RS API, we use provided scope as the API is included
                              in JBoss AS 7 -->
                      <dependency>
                              <groupId>org.jboss.spec.javax.ws.rs</groupId>
                              <artifactId>jboss-jaxrs-api_1.1_spec</artifactId>
                              <scope>provided</scope>
                      </dependency>

                      <!-- Import the JPA API, we use provided scope as the API is included in
                              JBoss AS 7 -->
                      <dependency>
                              <groupId>org.hibernate.javax.persistence</groupId>
                              <artifactId>hibernate-jpa-2.0-api</artifactId>
                              <scope>provided</scope>
                      </dependency>

                      <!-- Import the EJB API, we use provided scope as the API is included in
                              JBoss AS 7 -->
                      <dependency>
                              <groupId>org.jboss.spec.javax.ejb</groupId>
                              <artifactId>jboss-ejb-api_3.1_spec</artifactId>
                              <scope>provided</scope>
                      </dependency>

                      <!-- JSR-303 (Bean Validation) Implementation -->
                      <!-- Provides portable constraints such as @Email -->
                      <!-- Hibernate Validator is shipped in JBoss AS 7 -->
                      <dependency>
                              <groupId>org.hibernate</groupId>
                              <artifactId>hibernate-validator</artifactId>
                              <version>4.2.0.Final</version>
                              <scope>provided</scope>
                              <exclusions>
                                      <exclusion>
                                              <groupId>org.slf4j</groupId>
                                              <artifactId>slf4j-api</artifactId>
                                      </exclusion>
                              </exclusions>
                      </dependency>

                      <!-- Now we declare any tools needed -->

                      <!-- Annotation processor to generate the JPA 2.0 metamodel classes for
                              typesafe criteria queries -->
                      <dependency>
                              <groupId>org.hibernate</groupId>
                              <artifactId>hibernate-jpamodelgen</artifactId>
                              <version>1.1.1.Final</version>
                              <scope>provided</scope>
                      </dependency>

                      <!-- Needed for running tests (you may also use TestNG) -->
                      <dependency>
                              <groupId>junit</groupId>
                              <artifactId>junit</artifactId>
                              <version>4.10</version>
                              <scope>test</scope>
                      </dependency>

                      <!-- Optional, but highly recommended -->
                      <!-- Arquillian allows you to test enterprise code such as EJBs and Transactional(JTA)
                              JPA from JUnit/TestNG -->
                      <dependency>
                              <groupId>org.jboss.arquillian.junit</groupId>
                              <artifactId>arquillian-junit-container</artifactId>
                              <version>1.0.0.CR4</version>
                              <scope>test</scope>
                      </dependency>

                      <dependency>
                              <groupId>org.primefaces</groupId>
                              <artifactId>primefaces</artifactId>
                              <version>2.2.1</version>
                      </dependency>

                      <dependency>
                              <groupId>org.primefaces.themes</groupId>
                              <artifactId>south-street</artifactId>
                              <version>1.0.1</version>
                      </dependency>


                      <dependency>
                              <groupId>com.sun.faces</groupId>
                              <artifactId>jsf-api</artifactId>
                              <version>2.0.2</version>
                      </dependency>
                      <dependency>
                              <groupId>com.sun.faces</groupId>
                              <artifactId>jsf-impl</artifactId>
                              <version>2.0.2</version>
                      </dependency>
                      <!-- SEAm Solder 3.0.0 ist vorhanden, doppelter eintrag verursachte konflikte -->
      <!-- <dependency> <groupId>org.jboss.solder</groupId> <artifactId>solder-impl</artifactId>
                              <version>3.1.0.Beta3</version> </dependency> -->

                      <dependency>
                              <groupId>org.jboss.logging</groupId>
                              <artifactId>jboss-logging-tools</artifactId>
                              <version>1.0.0.Beta1</version>
                              <scope>provided</scope>
                      </dependency>


                      <dependency>
                              <groupId>org.jboss.seam.persistence</groupId>
                              <artifactId>seam-persistence-api</artifactId>
                              <version>3.0.0.Final</version>
                      </dependency>

                      <dependency>
                              <groupId>org.jboss.seam.persistence</groupId>
                              <artifactId>seam-persistence-impl</artifactId>
                              <version>3.0.0.Final</version>
                              <scope>runtime</scope>
                      </dependency>

                      <dependency>
                              <groupId>org.hibernate</groupId>
                              <artifactId>hibernate-core</artifactId>
                              <version>3.5.1-Final</version>
                      </dependency>
              </dependencies>

              <build>
                      <!-- Maven will append the version to the finalName (which is the name
                              given to the generated war, and hence the context root) -->
                      <finalName>${project.artifactId}</finalName>
                      <plugins>
                              <!-- Compiler plugin enforces Java 1.6 compatibility and activates annotation
                                      processors -->
                              <plugin>
                                      <artifactId>maven-compiler-plugin</artifactId>
                                      <version>2.3.1</version>
                                      <configuration>
                                              <source>1.6</source>
                                              <target>1.6</target>
                                      </configuration>
                              </plugin>
                              <plugin>
                                      <artifactId>maven-war-plugin</artifactId>
                                      <version>2.1.1</version>
                                      <configuration>
                                              <!-- Java EE 6 doesn't require web.xml, Maven needs to catch up! -->
                                              <failOnMissingWebXml>false</failOnMissingWebXml>
                                      </configuration>
                              </plugin>
                              <!-- The JBoss AS plugin deploys your war to a local JBoss AS container -->
                              <!-- To use, set the JBOSS_HOME environment variable and run: mvn package
                                      jboss-as:deploy -->
                              <plugin>
                                      <groupId>org.jboss.as.plugins</groupId>
                                      <artifactId>jboss-as-maven-plugin</artifactId>
                                      <version>7.0.2.Final</version>
                              </plugin>
                      </plugins>
              </build>

              <profiles>
                      <profile>
                              <!-- The default profile skips all tests, though you can tune it to run
                                      just unit tests based on a custom pattern -->
                              <!-- Seperate profiles are provided for running all tests, including Arquillian
                                      tests that execute in the specified container -->
                              <id>default</id>
                              <activation>
                                      <activeByDefault>true</activeByDefault>
                              </activation>
                              <build>
                                      <plugins>
                                              <plugin>
                                                      <artifactId>maven-surefire-plugin</artifactId>
                                                      <version>2.4.3</version>
                                                      <configuration>
                                                              <skip>true</skip>
                                                      </configuration>
                                              </plugin>

                                              <plugin>
                                                      <groupId>org.codehaus.mojo</groupId>
                                                      <artifactId>jboss-maven-plugin</artifactId>
                                                      <!-- only version 1.4.1 supports hard deploying multiple files -->
                                                      <version>1.4.1</version>

                                                      <configuration>
                                                              <jbossHome>${jboss.home}</jbossHome>
                                                              <serverName>${jboss.domain}</serverName>

                                                              <fileNames>
                                                                      <param>src/main/webapp/resources/DCE-Project_ds.xml</param>
                                                                      <param>${project.build.directory}/${project.build.finalName}.war</param>
                                                              </fileNames>
                                                      </configuration>
                                              </plugin>
                                      </plugins>
                              </build>
                      </profile>

                      <profile>
                              <!-- We add the JBoss repository as we need the JBoss AS connectors for
                                      Arquillian -->
                              <repositories>
                                      <!-- The JBoss Community public repository is a composite repository
                                              of several major repositories -->
                                      <!-- see http://community.jboss.org/wiki/MavenGettingStarted-Users -->
                                      <repository>
                                              <id>jboss-public-repository</id>
                                              <name>JBoss Repository</name>
                                              <url>http://repository.jboss.org/nexus/content/groups/public</url>
                                              <!-- These optional flags are designed to speed up your builds by reducing
                                                      remote server calls -->
                                              <releases>
                                              </releases>
                                              <snapshots>
                                                      <enabled>false</enabled>
                                              </snapshots>
                                      </repository>
                              </repositories>

                              <pluginRepositories>
                                      <pluginRepository>
                                              <id>jboss-public-repository</id>
                                              <name>JBoss Repository</name>
                                              <url>http://repository.jboss.org/nexus/content/groups/public</url>
                                              <releases>
                                              </releases>
                                              <snapshots>
                                                      <enabled>false</enabled>
                                              </snapshots>
                                      </pluginRepository>
                              </pluginRepositories>

                              <!-- An optional Arquillian testing profile that executes tests in your
                                      JBoss AS instance -->
                              <!-- This profile will start a new JBoss AS instance, and execute the
                                      test, shutting it down when done -->
                              <!-- Run with: mvn clean test -Parq-jbossas-managed -->
                              <id>arq-jbossas-managed</id>
                              <dependencies>
                                      <dependency>
                                              <groupId>org.jboss.as</groupId>
                                              <artifactId>jboss-as-arquillian-container-managed</artifactId>
                                              <version>7.0.2.Final</version>
                                              <scope>test</scope>
                                      </dependency>
                              </dependencies>
                      </profile>

                      <profile>
                              <!-- We add the JBoss repository as we need the JBoss AS connectors for
                                      Arquillian -->
                              <repositories>
                                      <!-- The JBoss Community public repository is a composite repository
                                              of several major repositories -->
                                      <!-- see http://community.jboss.org/wiki/MavenGettingStarted-Users -->
                                      <repository>
                                              <id>jboss-public-repository</id>
                                              <name>JBoss Repository</name>
                                              <url>http://repository.jboss.org/nexus/content/groups/public</url>
                                              <!-- These optional flags are designed to speed up your builds by reducing
                                                      remote server calls -->
                                              <releases>
                                              </releases>
                                              <snapshots>
                                                      <enabled>false</enabled>
                                              </snapshots>
                                      </repository>
                              </repositories>

                              <pluginRepositories>
                                      <pluginRepository>
                                              <id>jboss-public-repository</id>
                                              <name>JBoss Repository</name>
                                              <url>http://repository.jboss.org/nexus/content/groups/public</url>
                                              <releases>
                                              </releases>
                                              <snapshots>
                                                      <enabled>false</enabled>
                                              </snapshots>
                                      </pluginRepository>
                              </pluginRepositories>

                              <!-- An optional Arquillian testing profile that executes tests in a remote
                                      JBoss AS instance -->
                              <!-- Run with: mvn clean test -Parq-jbossas-remote -->
                              <id>arq-jbossas-remote</id>
                              <dependencies>
                                      <dependency>
                                              <groupId>org.jboss.as</groupId>
                                              <artifactId>jboss-as-arquillian-container-remote</artifactId>
                                              <version>7.0.2.Final</version>
                                              <scope>test</scope>
                                      </dependency>
                              </dependencies>
                      </profile>

              </profiles>
      </project>
      ==============================================================================

      ==============================================================================
      2. have a hibernat.properties - File:

      hibernate.connection.password=XXXXXXXX
      hibernate.connection.username=userName
      hibernate.connection.driver_class=oracle.jdbc.OracleDriver
      hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
      hibernate.connection.url=jdbc\:oracle\:thin\:@localhost\:1521\:ora11g

      hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
      hibernate.datasource=
      hibernate.transaction.manager_lookup_class=

      ==============================================================================
      3. created a persistence.xml

      <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_2_0.xsd'
          version='2.0'>
          <persistence-unit name='database'>
              <jta-data-source>java:/jdbc/dceDS</jta-data-source>
              <properties>
                  <property name='hibernate.dialect' value='org.hibernate.dialect.Oracle10gDialect' />
                  <property name='hibernate.hbm2ddl.auto' value='create-drop' />
                  <property name='hibernate.show_sql' value='true' />
                  <property name='hibernate.format_sql' value='false' />
                  <property name='hibernate.use_sql_comments' value='true' />
              </properties>
          </persistence-unit>
      </persistence>
      ==============================================================================

      ==============================================================================
      4. Created a DataSource in standalone.xml
      <datasource jndi-name='java:/jdbc/dceDS' pool-name='OracleDS' enabled='true' jta='true' use-java-context='true' use-ccm='true'>
                          <connection-url>
                              jdbc:oracle:thin:@localhost:1521:ora11g
                          </connection-url>
                          <driver>
                              ojdbc6.jar
                          </driver>
                          <pool>
                              <prefill>
                                  false
                              </prefill>
                              <use-strict-min>
                                  false
                              </use-strict-min>
                              <flush-strategy>
                                  FailingConnectionOnly
                              </flush-strategy>
                          </pool>
                          <security>
                              <user-name>
                                  userName
                              </user-name>
                              <password>
                                  XXXXXXXX
                              </password>
                          </security>
                      </datasource>
      ==============================================================================

      ==============================================================================
      5. Created a Module for oracle and add the drivers (module.xml):
      <module xmlns='urn:jboss:module:1.0' name='com.oracle.db'>
          <resources>
              <resource-root path='ojdbc6.jar'/>
          </resources>
          <dependencies>
              <module name='javax.api'/>
              <module name='javax.transaction.api'/>
          </dependencies>
      </module>
      ==============================================================================

      ==============================================================================
      6. Add some Tags in my beans.xml
      <beans xmlns='http://java.sun.com/xml/ns/javaee' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
              xmlns:s='urn:java:ee' xmlns:t='urn:java:org.jboss.seam.transaction'
              xsi:schemaLocation='
            http://java.sun.com/xml/ns/javaee
            http://docs.jboss.org/cdi/beans_1_0.xsd'>

              <t:SeSynchronizations>
                      <s:modifies />
              </t:SeSynchronizations>

              <t:EntityTransaction>
                      <s:modifies />
              </t:EntityTransaction>
      </beans>
      ==============================================================================

      ==============================================================================
      7. Tried to use the EntityManager in my controller-class:

      public class RuleController {
             
              private Rule rule;

              private Collection<Rule> rules;
             
              @ExtensionManaged
          @PersistenceUnit
          @ConversationScoped
          EntityManagerFactory producerField;
             
              @Inject
              private EntityManager em;
             
             
              private static RuleController instance = new RuleController();


              private RuleController() {
              }

              public static RuleController getInstance() {
                      return instance;

              }
             
              /** Anlegen einer neuen Regel.
               *
               */
              public void createRule(Rule rule){
                     
                      rule.setId(new RuleDefinitionsId(new BigDecimal(1),new BigDecimal(0)));
                      rule.setSortNo(new BigDecimal(1));
                      rule.setChangedBy('Matthias');
                      rule.setRulePriority(new BigDecimal(1));
                      rule.setVersionEndDate(new Date());
                      rule.setVersionEndDate(new Date());
                      rule.setVersionOperation('i');
                      System.out.println(rule);
                      System.out.println('is em Open?'+em.isOpen());
                      em.persist(rule);
              }
      ==============================================================================

      ==============================================================================
      What works?
      I definetly have a connection to the database. The entity classes have been created as tables in my database
      ==============================================================================

      ==============================================================================
      Where is the Problem?
      My application works fine as long as I hit the button to persist the Entry. The first system.out. shows that everything is filled right. The next System.out throws a NullPointerException. So the fault should be there. I think my EntityManager isn't defined. But I have no clue what to do and google or the documentation couldn't help. Do you have any ideas to move on?

      (I changed my " to ' due to formatting errors)