Rapid CRUD application development with JBoss Seam and MySQL database.

Rapid CRUD application development with JBoss Seam and MySQL database.

Rapid web application development is only “rapid” if you know what you are doing! It took me some time to accomplish this goal with JBoss Seam but I think it was worth it. Seam framework is a bunch of the best technologies in Java world connected together and personally I think it has a big future.
Seam is great framework but like with the others, the problems arise if you want to skip the default and create something on your own, to plug it into existing environment. The framework, when used with defaults, allows you to create the CRUD application so fast that you won’t have a chance, to have a sip of your favourite coffee, on the other hand, if you want to change for example, the database from default Hypersonic bundled together with JBoss application server it can brings you some trouble.

This is a very simple tutorial on how to set up your first Seam project with the MySQL server.

I use Ubuntu 7.04 FF with MySQL and Java 6.0 installed from the standard repos.

Let’s get started, first you have to install MySql database and of course Java on your machine, this is not tutorial on mysql setup, so I won’t go into detail but you have to create the user, new database and some sample tables, add privileges for the users accordingly, create the password and so on.
One more thing before you get started, you have to download appropriate jdbc driver for you database. I’m using mysql-connector-java-5.0.7-bin.jar for MySQL database.
I start my tutorial assuming you have prepared the above.
1. Download and unpack JBoss Seam 1.2.1.GA from:
http://labs.jboss.com/jbossseam/download/index.html
2. Open build.properties and change the path to your JBoss 4.0.5 installation accordingly.
You can easily skip the tomcat path for the purpose of this tutorial
3. When using Linux/Unix add -x flag to seam file in Seam directory by executing:
chmod +x seam
4. Run ./seam setup
The setup application will ask you quite a lot of questions, be patient and make sure you give the correct answers.

Click on the image to enlarge.
Seam will remember your very first setup and when at a later time, you would like to change anything, most of the time you will just press Enter to accept your defaults.

5. After the initial setup, run ./seam new-project to generate skeleton application template with build scripts and configuration files.
6. Type ./seam explode and when everything is ok, start the JBoss server.
7. When the server is ready and your application (ear) is successfully deployed to $JBOSS-HOME/server/default/deploy directory start the browser and enter the URL:
with default settings this would be: http://localhost:8080/YourProjectName
You should see the basic seam application running in your browser, something very similar to the Ruby on Rails functionality.

At this point you are able to log in/out of the application which, maybe it’s not much but just wait a bit for the best piece of cake..
8. Go back to Seam folder and type: ./seam generate-entities
This will reverse-engineer EJB 3.0 entity beans from your database.
9. Type ./seam restart and your application will be hot-redeployed to the application server.
10. Refresh your application in the browser and you are ready to go.


The links at the top of the web page are links for managing in CRUD way your database. Click on one of them (depends how many tables you had in your database, for this tutorial I had created just one with two columns – yes I know, I’m lazy!) and you are able to mange your database data.

Problems:
I hope you won’t have any, but I did, so just to have a peace of mind I will write here what I did.
Very strange thing, well, when you don’t really know what you are doing you always feel strange ;), but to the point, when I have downloaded JBoss Seam for the first time on my system about 3 days ago, I was doing everything I could to make it work and it just didn’t really want to run. I was able to generate entities but not able to run the link responsible for managing the tables. Anyway, I have sorted it in two ways. First, I have removed JBoss AS, and JBoss Seam completely and started from scratch about 2 hours ago and everything works perfectly :), second, if it still doesn’t work in your case, do the following:
1. Make sure than the database password exists in the following files from yourProjectName/resources/:
SeamTest-dev-ds.xml
SeamTest-prod-ds.xml
2. You can experiment with persistence.xml file found in: yourProjectName/exploded-archives/SeamTest.jar/META-INF and change the section, according to M. Yuan’s book:
……………………..
<properties>
<property name=”hibernate.dialect” value=”org.hibernate.dialect.MySQLDialect” />
<property name=”hibernate.hbm2ddl.auto” value=”none” />
</properties>
……………………..
The value of hibernate.hbm2ddl.auto stands for:
none – schema is not automatically created when the application is deployed.
create-drop – tables are created at application deployment and dropped at redeployment.
update – the schema is updated or created but the content is not deleted.

By the way, the Yuan’s book is great, could be a bit bigger and more detailed but still a very good read. You can have a look at Yuan’s blog to find out more @:
http://www.michaelyuan.com/blog/seam-next-gen-web-framework/

12 responses on “Rapid CRUD application development with JBoss Seam and MySQL database.

  1. Anonymous September 3, 2007 at 9:20 pm

    tak tak grajczyku ofcorse wszystko tutaj to prawda :)))
    chyba sobie to przepisze 😛

  2. cm November 14, 2007 at 11:31 pm

    Thanks for the steps! I followed the instructions and it worked perfectly.

  3. Alex December 10, 2007 at 11:24 am

    Better to generate by Seam-gen than by adapting example files. Finally something that does work. Thanks

  4. New January 31, 2008 at 12:56 pm

    Neato. I will try this now and report back how long it takes. I have used this sourceforge project )http://sourceforge.net/projects/db2app/) as it generates JSP and Flex code but Seam looks perfect for EJB 3.0 code.

  5. xourge February 19, 2008 at 12:05 am

    thank you very much for these, i had to erase the whole dir and do everything again and it worked! i hope you publish more tutorials in the future, for example using combo lists to work tables with foreign keys

  6. Asker Ali M March 24, 2008 at 1:45 pm

    Hi,
    I have a database in MSSQL2005.when i am trying to run seam generate-entities command i am not getting any Bean or UI created for me.is it because of the Database or any configuration i have forgotten.

    Thanx in Advance
    Asker Ali Mardan

  7. Asker Ali M March 24, 2008 at 2:05 pm

    Hi I got the following out put after running

    D:\>seam generate-entities
    Buildfile: D:\dbmx\jboss-seam-2.1.0.A1\seam-gen\build.xml

    init:

    init-properties:
    [echo] D:/dbmx/jboss-4.2.2.GA

    validate-workspace:

    validate-project:

    init-generate:

    generate-model:
    [echo] Reverse engineering database using JDBC driver D:\dbmx\Drivers\sqljd
    bc.jar
    [echo] project=d:/workspace/TestDBMX
    [echo] model=org.jboss.seam.testdbmx
    [hibernate] Executing Hibernate Tool with a JDBC Configuration (for reverse engi
    neering)
    [hibernate] 1. task: hbm2java (Generates a set of .java files)
    [hibernate] Mar 24, 2008 5:26:31 PM org.hibernate.cfg.Environment
    [hibernate] INFO: Hibernate 3.2.4.sp1
    [hibernate] Mar 24, 2008 5:26:31 PM org.hibernate.cfg.Environment
    [hibernate] INFO: hibernate.properties not found
    [hibernate] Mar 24, 2008 5:26:31 PM org.hibernate.cfg.Environment buildBytecodeP
    rovider
    [hibernate] INFO: Bytecode provider name : cglib
    [hibernate] Mar 24, 2008 5:26:31 PM org.hibernate.cfg.Environment
    [hibernate] INFO: using JDK 1.4 java.sql.Timestamp handling
    [hibernate] Mar 24, 2008 5:26:32 PM org.hibernate.cfg.reveng.OverrideRepository
    addFile
    [hibernate] INFO: Override file: d:\workspace\TestDBMX\resources\seam-gen.reveng
    .xml
    [hibernate] Mar 24, 2008 5:26:32 PM org.hibernate.connection.DriverManagerConnec
    tionProvider configure
    [hibernate] INFO: Using Hibernate built-in connection pool (not for production u
    se!)
    [hibernate] Mar 24, 2008 5:26:32 PM org.hibernate.connection.DriverManagerConnec
    tionProvider configure
    [hibernate] INFO: Hibernate connection pool size: 20
    [hibernate] Mar 24, 2008 5:26:32 PM org.hibernate.connection.DriverManagerConnec
    tionProvider configure
    [hibernate] INFO: autocommit mode: false
    [hibernate] Mar 24, 2008 5:26:32 PM org.hibernate.connection.DriverManagerConnec
    tionProvider configure
    [hibernate] INFO: using driver: com.microsoft.sqlserver.jdbc.SQLServerDriver at
    URL: jdbc:sqlserver://localhost:2006;DatabaseName=TestDBMX
    [hibernate] Mar 24, 2008 5:26:32 PM org.hibernate.connection.DriverManagerConnec
    tionProvider configure
    [hibernate] INFO: connection properties: {user=sa, password=****}
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: RDBMS: Microsoft SQL Server, version: 9.00.1399
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: JDBC driver: Microsoft SQL Server 2005 JDBC Driver, version: 1
    .0.809.102
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.dialect.Dialect
    [hibernate] INFO: Using dialect: org.hibernate.dialect.SQLServerDialect
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.transaction.TransactionFactory
    Factory buildTransactionFactory
    [hibernate] INFO: Using default transaction strategy (direct JDBC transactions)
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.transaction.TransactionManager
    LookupFactory getTransactionManagerLookup
    [hibernate] INFO: No TransactionManagerLookup configured (in JTA environment, us
    e of read-write or transactional second-level cache is not recommended)
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Automatic flush during beforeCompletion(): disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Automatic session close at end of transaction: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Scrollable result sets: enabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: JDBC3 getGeneratedKeys(): enabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Connection release mode: auto
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Default schema: ROOT
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Default batch fetch size: 1
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Generate SQL with comments: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Order SQL updates by primary key: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Order SQL inserts for batching: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory createQuer
    yTranslatorFactory
    [hibernate] INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFact
    ory
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.hql.ast.ASTQueryTranslatorFact
    ory
    [hibernate] INFO: Using ASTQueryTranslatorFactory
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Query language substitutions: {}
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: JPA-QL strict compliance: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Second-level cache: enabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Query cache: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory createCach
    eProvider
    [hibernate] INFO: Cache provider: org.hibernate.cache.HashtableCacheProvider
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Optimize cache for minimal puts: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Structured second-level cache entries: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Statistics: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Deleted entity synthetic identifier rollback: disabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Default entity-mode: pojo
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.cfg.SettingsFactory buildSetti
    ngs
    [hibernate] INFO: Named query checking : enabled
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.connection.DriverManagerConnec
    tionProvider close
    [hibernate] INFO: cleaning up connection pool: jdbc:sqlserver://localhost:2006;D
    atabaseName=TestDBMX
    [hibernate] Mar 24, 2008 5:26:33 PM org.hibernate.tool.Version
    [hibernate] INFO: Hibernate Tools 3.2.0.CR1
    [javaformatter] Java formatting of 0 files completed. Skipped 0 file(s).

    generate-ui:
    [echo] Building project ‘TestDBMX’ to generate views and controllers

    init:
    [mkdir] Created dir: d:\workspace\TestDBMX\exploded-archives\TestDBMX.ear
    [mkdir] Created dir: d:\workspace\TestDBMX\dist

    compile:

    copyclasses:

    jar:
    [copy] Copying 1 file to d:\workspace\TestDBMX\exploded-archives\TestDBMX.j
    ar\META-INF
    [copy] Copying 1 file to d:\workspace\TestDBMX\exploded-archives\TestDBMX.j
    ar
    [hibernate] Executing Hibernate Tool with a JPA Configuration
    [hibernate] 1. task: generic exportertemplate: view/list.xhtml.ftl
    [hibernate] Mar 24, 2008 5:26:34 PM org.hibernate.cfg.annotations.Version
    [hibernate] INFO: Hibernate Annotations 3.3.0.GA
    [hibernate] Mar 24, 2008 5:26:34 PM org.hibernate.ejb.Version
    [hibernate] INFO: Hibernate EntityManager 3.3.1.GA
    [hibernate] Mar 24, 2008 5:26:35 PM org.hibernate.cfg.AnnotationConfiguration se
    condPassCompile
    [hibernate] INFO: Hibernate Validator not found: ignoring
    [hibernate] Mar 24, 2008 5:26:35 PM org.hibernate.cfg.AnnotationConfiguration se
    condPassCompile
    [hibernate] INFO: Hibernate Validator not found: ignoring
    [hibernate] 2. task: generic exportertemplate: view/view.xhtml.ftl
    [hibernate] 3. task: generic exportertemplate: view/view.page.xml.ftl
    [hibernate] 4. task: generic exportertemplate: view/edit.xhtml.ftl
    [hibernate] 5. task: generic exportertemplate: view/edit.page.xml.ftl
    [hibernate] 6. task: generic exportertemplate: src/EntityList.java.ftl
    [hibernate] 7. task: generic exportertemplate: view/list.page.xml.ftl
    [hibernate] 8. task: generic exportertemplate: src/EntityHome.java.ftl
    [hibernate] 9. task: generic exportertemplate: view/layout/menu.xhtml.ftl
    [javaformatter] Java formatting of 1 files completed. Skipped 0 file(s).
    [echo] Type ‘seam restart’ and go to http://localhost:8080/TestDBMX

    generate:

    generate-entities:

    Thanx
    Asker Ali Mardan

  8. Erick Magnus August 20, 2008 at 5:23 pm

    It works just great for me ;D
    Thanks, it helped a lot on my way studying JBoss Seam!

  9. Manu November 12, 2008 at 11:10 am

    Thanks a lot!

    Now I want to expose these CRUD operations as webservices.

    Any suggestions on this one please.

    Thanks
    Manu.

  10. ooblogger November 12, 2008 at 11:23 am

    I don’t work with seam any more but I think that it should be quite straightforward, you can easily expose EJB session beans as a web service in Java EE 5, if you cannot just add the web service methods to the existing beans, create your own EJB as a web service and inject other beans you need

  11. Anonymous December 17, 2008 at 5:56 pm

    Thank you! You made my day! I had to use Jrockit Java instead of Sun Java because of PermGen OutOfMemoryError.

  12. SeamSurfer September 1, 2009 at 11:13 am

    This has been most useful.
    I went with the option of WAR (no EJB) and it works perfect.
    One thing I noticed though. If I open the project in Eclipse (that has been configured to auto build and deploy) , the app doesn’t work. I had to undeploy using “./seam unexplode” followed by redeployment using “./seam explode” to make it work again.

Leave a Reply

Related Posts

    No Releated Posts