Working and fighting with JBoss AS 7

On September 30, 2011, in JBoss AS 7, by lucasterdev

The Hibernate version included in JBoss AS 7 comes with Hibernate Envers, which, guess what? Is bugged. When using MySQL, even though you set GenerationType.AUTO, Envers tries to use sequences. The problem arises if you import data with an import.sql file at deploy time, as the hibernate_sequence will be set to value 1, disregarding the hardcoded ids.

Solution: use GenerationType.IDENTITY when working with MySQL.

 

(from http://community.jboss.org/wiki/HowToAddRichFaces4xToMavenBasedProject)


<dependency>
	<groupId>org.richfaces.core</groupId>
	<artifactId>richfaces-core-api</artifactId>
	<version>4.0.0.Final</version>
	<scope>compile</scope>
</dependency>
<dependency>
	<groupId>org.richfaces.core</groupId>
	<artifactId>richfaces-core-impl</artifactId>
	<version>4.0.0.Final</version>
	<scope>compile</scope>
</dependency>
<dependency>
	<groupId>org.richfaces.ui</groupId>
	<artifactId>richfaces-components-ui</artifactId>
	<version>4.0.0.Final</version>
	<scope>compile</scope>
</dependency>
 

(from http://thecodecentral.com/2011/01/18/fix-ubuntu-10-10-suspendhibernate-not-working-bug)

sudo gedit /etc/pm/sleep.d/20_custom-ehci_hcd

#!/bin/sh
#inspired by http://art.ubuntuforums.org/showpost.php?p=9744970&postcount=19
#...and http://thecodecentral.com/2011/01/18/fix-ubuntu-10-10-suspendhibernate-not-working-bug
# tidied by tqzzaa :) 

VERSION=1.1
DEV_LIST=/tmp/usb-dev-list
DRIVERS_DIR=/sys/bus/pci/drivers
DRIVERS="ehci xhci" # ehci_hcd, xhci_hcd
HEX="[[:xdigit:]]"
MAX_BIND_ATTEMPTS=2
BIND_WAIT=0.1

unbindDev() {
  echo -n > $DEV_LIST 2>/dev/null
  for driver in $DRIVERS; do
    DDIR=$DRIVERS_DIR/${driver}_hcd
    for dev in `ls $DDIR 2>/dev/null | egrep "^$HEX+:$HEX+:$HEX"`; do
      echo -n "$dev" > $DDIR/unbind
      echo "$driver $dev" >> $DEV_LIST
    done
  done
}

bindDev() {
  if [ -s $DEV_LIST ]; then
    while read driver dev; do
      DDIR=$DRIVERS_DIR/${driver}_hcd
      while [ $((MAX_BIND_ATTEMPTS)) -gt 0 ]; do
          echo -n "$dev" > $DDIR/bind
          if [ ! -L "$DDIR/$dev" ]; then
            sleep $BIND_WAIT
          else
            break
          fi
          MAX_BIND_ATTEMPTS=$((MAX_BIND_ATTEMPTS-1))
      done
    done < $DEV_LIST
  fi
  rm $DEV_LIST 2>/dev/null
}

case "$1" in
  hibernate|suspend) unbindDev;;
  resume|thaw)       bindDev;;
esac

sudo chmod 755 /etc/pm/sleep.d/20_custom-ehci_hcd

 

Environment setup

  1. Download and install Java 1.6 JDK
  2. Download and install Eclipse Indigo 32bit
  3. Install m2eclipse from Eclipse Marketplace
  4. Install TestNG plugin from Eclipse marketplace
  5. Install JBoss Tools for Eclipse through the update site.
    1. First install “All JBoss Tools”
    2. Restart Eclipse
    3. Secondly, install JBoss Maven Integration
  6. Download and unzip Seam 2.2.2.Final
  7. Open Eclipse; go to Window –> Preferences –> JBoss Tools –> Web –> Seam and add the Seam 2 installation.
  8. Download and unzip JBoss AS 7
  9. Open Eclipse, go to Window –> Preferences –> Server –> Runtime enviroments and add the JBoss AS 7 runtime environment

Project creation

  1. Create a new “Seam Web Project”
    • Target Runtime: the JBoss AS 7 runtime you added before
    • Dynamic Web module version: make sure you select 2.5, because Seam 2 doesn’t work with Servlet 3.0
    • Configuration: click Modify to select the Project Facets
      • Ensure CDI is unchecked (because Seam 2 is incompatible with CDI/Weld)
      • Ensure Dynamic Web Module is set to 2.5 (because Seam 2 is incompatible with Servlet 3.0)
      • Ensure Java is 1.6
      • Ensure JavaServer Faces is 1.2 (because Seam 2 is incompatible with JSF 2.0)
      • Ensure Seam is checked and set to 2.2
      • JPA: check if you want to enable Eclipse’s JPA tooling. This has weird side-effects in the project structure that Eclipse creates. I’ll talk in detail later.
  2. Click Next
  3. Source folders: click Next
  4. JPA Facet:
    • Platform: Hibernate (JPA 2.x)
    • JPA implementation: Library Provided by Target Runtime
    • Connection: if you checked JPA before, you must select a connection.
    • Persistent class management: Discover annotated classes automatically (for easier life)
  5. Click Next
  6. Web Module: check “Generate web.xml deployment descriptor”
  7. Click Next
  8. JBoss M2 capabilities: this tutorial assumes you choose “war” as Packaging.
  9. Click Next
  10. JSF capabilities
    • JSF implementation library: Library Provided by Target Runtime
  11. Click Next
  12. Seam Facet
    • Seam Runtime: the seam runtime you installed in “Environment Setup”
    • Deploy as: must be the same as the Packaging selected at (8 – JBoss M2 capabilities)
    • Database Type: must be the type of the database you chose at (4 – JPA Facet)
  13. Click Finish
  14. Wait a lot for Maven to download the artifacts for the first time.

Fixing the project

JBoss Tools is very messy, bugged and outdated; we’ll have to manually fix some mistakes made by our IDE.

  1. The first thing to notice is that our project will have a different directory structure, depending on whether or not you checked the “JPA” Project Facet.
  2. If we added the JPA Project Facet, the generated persistence.xml file will be empty!! WTF??!
  3. Depending on whether we added the JPA Project Facet, the pom.xml file will be different as well.
  4. If you didn’t add the JPA Facet, your pom.xml will give the following error: Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:build-helper-maven-plugin:1.5:add-source (execution: add-source, phase: generate-sources).  Read my other post to fix the error.

Here are some other issues that the project will have anyway, and you have to fix them manually:

  1. The Seam version in the parent project’s pom.xml will not match the version of Seam you installed. Fix it by manually changing the seam.version property.
  2. Your project will contain a WEB-INF/jboss-web.xml that is incompatible with JBoss AS 7. To make it compatible, comment out the <class-loading> element.
  3. The project is not set to export the Maven dependencies into WEB-INF/lib once deployed. To solve that, quick-fix the following warning: Classpath entry org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER will not be exported or published. Runtime ClassNotFoundExceptions may result. Select “Mark the associated raw classpath entry as a publish/export dependency” and click Finish.
  4. Seam 2 works only with hibernate-validator version 3.1.0.GA, which is included in JBoss AS 6. However, JBoss AS 7 has a newer version of hibernate-validator, that is incompativle with Seam 2. To solve this issue, open the project parent’s pom.xml and change the scope of hibernate-validator from “provided” to “compile”.
  5. JBoss Tools forget to add a couple of dependencies that Seam 2 needs. Add the following dependencies to the project:
  6. <dependency>
        <groupId>com.google.gwt</groupId>
        <artifactId>gwt-servlet</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    
  7. JBoss Tools creates a datasource file (projectname/resources/projectname-ds.xml) that is meant to work with JBoss AS 6. However, in JBoss AS 7 datasources are defined in a very different way. Right-click the file and click “Unmark as deployable”
  8. Even though you selected JPA 2.0, the generated persistence.xml will be set to JPA 1.0. Fix that by setting the version attribute to 2.0 and change the .xsd to *2_0.xsd
  9. Here is a persistence.xml that will work out-of-the-box. It is set to use the ExampleDS that is already defined in JBoss AS 7. Remember to change the “myproject” string with your real project name.

    <!--?xml version="1.0" encoding="UTF-8"?--><?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_2_0.xsd"
        version="2.0">
    
        <persistence-unit name="myprojectPU" transaction-type="JTA">
    
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
            <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
    
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
                <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                <property name="hibernate.show_sql" value="true"/>
                <property name="hibernate.format_sql" value="true"/>
                <property name="jboss.entity.manager.factory.jndi.name" value="java:/myprojectEntityManagerFactory"/>
            </properties>
    
        </persistence-unit>
    
    </persistence>
    

That’s it! Now you have setup a fully working Seam 2 project that can be deployed in JBoss AS 7.

 

How to create a H2 connection profile in Eclipse

On September 6, 2011, in Eclipse, H2, by lucasterdev

(from http://www.manning-sandbox.com/message.jspa?messageID=85357)

Define new connection type named “H2″ from “Generic JDBC”
On next screen, click the button to define a “New Driver Definition”
On the screen that pops up, click on “Generic JDBC Driver” and rename to “H2″
Click on the “Jar List” tab and add the H2 JAR to the list
Optionally, on the “Properties” tab you can set the following defaults:
Connection URL: jdbc:h2:mem
Database Name: PUBLIC
Driver Class: org.h2.Driver
User ID: sa
Click “OK”
Now you should be able to select H2 from the list of Drivers
Enter the proper JDBC URL for H2 (somethink like jdbc:h2:file:///home/twoputt/databases/open18-db/h2)
Click Test Connection

Remember, when using H2 in embedded mode, you can only have a single connection open. I encourage you to research how to use H2 in TCP mode (like a regular database) because it’s hard to control when Eclipse opens/closes connections to the database.

 

To solve the problem, add the following in the <build> element:

<pluginManagement>
	<plugins>
		<!--This plugin's configuration is used to store Eclipse m2e settings
			only. It has no influence on the Maven build itself. -->
		<plugin>
			<groupId>org.eclipse.m2e</groupId>
			<artifactId>lifecycle-mapping</artifactId>
			<version>1.0.0</version>
			<configuration>
				<lifecycleMappingMetadata>
					<pluginExecutions>
						<pluginExecution>
							<pluginExecutionFilter>
								<groupId>org.codehaus.mojo</groupId>
								<artifactId>build-helper-maven-plugin</artifactId>
								<versionRange>1.5</versionRange>
								<goals>
									<goal>add-source</goal>
								</goals>
							</pluginExecutionFilter>
							<action>
								<execute/>
							</action>
						</pluginExecution>
					</pluginExecutions>
				</lifecycleMappingMetadata>
			</configuration>
		</plugin>
	</plugins>
</pluginManagement>