SQL Zone is brought to you in partnership with:

I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

Seam and Hibernate Development with JBoss

  • submit to reddit

JBoss Developer Studio includes brand new tools for Seam, a powerful application framework for building next generation Web 2.0 applications. These Seam tools provide wizards for creating new Seam projects, validation tools, expression language code completion, testing tools, and wizards for generating entity objects. In this demonstration Jim Tyrrell, JBoss Solutions Architect, will show you exactly how easy it is to use JBoss Developer Studio to reverse engineer a database into a complete web application.

The complete transcript of this presentation has been provided below.

My name is Jim Tyrrell. I am a JBoss Solutions Architect. I get to travel around the country talking about the technology with people like yourselves. I will spend about the next 15 or 20 minutes talking about JBoss Developer Studio with you guys. When this is all over, after you get your t-shirt, you go around the corner and get an entitlement to that. We will also show you Seam and Hibernate underneath the covers running inside the Seam application.

If that works for you guys, we are going to get going. Just a few slides and we'll get rocking and rolling to a demo.

A few notable features about Seam. One of those to me or the biggest one that I talk to groups like yourselves about is the notion of a conversation. Anybody here familiar with what a conversation is in Seam? Some people, all right. Well, if you think about Session Scope, right? I'm sure none of us have overloaded the Session with stuff that we shouldn't be putting in there.

For example, if you go to a lot of online commerce websites, they definitely overload the Session to the point where two windows aren't going to work. I know I go to buy airline tickets all the time, and I try to open up two windows to try to figure out what the best flight for me is. At a lot of airlines, they will kill the other session, the other window, and I am going to be stuck not being able to buy that ticket, same thing with trying to buy something at an e-commerce site. Again, you put things in your shopping cart; it may or may not work.

A conversation is a unique atomic thing going on inside of each individual browser window. That is added into Seam applications essentially for free with some simple annotations. There is also extensive RichFaces support.

An hour ago, Max (Katz) and the guys around the corner could definitely help you out with some talk around RichFaces, but this adds in a lot of AJAX functionality into your applications. With the reverse engineering demo that we do we will see some examples of how that works.

I am going to show you a demonstration of a CRUD application, create, read, update and delete. Literally, in just a few minutes you can take a database and have a fully working web application in just a few minutes. Then, we will also talk a little bit about Hibernate and some of the artifacts that are created underneath the covers.

I have been here at Java One. This is my second year in a row. I was here a couple of years ago. A lot of times people don't tell you what it is they are showing you. Well, there, that's what I am showing you.

I am showing you JBoss Developer Studio 11. It's a Beta 1. Actually, there's a CR candidate that just hit the wires internally. I am using a MySQL database and a MySQL driver to make all of this stuff go and a little bit of a SQL script I created a long time ago to do this demo. That's what goes into this.

Again, what is it we are going to reverse engineer? Well, I am sure that with Java One a lot of you are familiar with the blogging application. There is my database schema that I am using for this. At the topmost we have our role which is going to be security. Above that or below that, if you will, with a foreign key relationship we have a user which, of course, has to point to one of those roles.

Then, of course, you are going to need to be able to make blog postings. All right? You are going to be able to write up whatever it is, your thoughts, your comments on the day. Then, lastly, if other users want to make comments on a particular blog posting we have that foreign key relationship in this. This is just a crude drawing of the various foreign keys and where they are pointing to from within the database schema within this.

Without further ado, let's go rock and roll through a demo.

This is JBoss Developer Studio right out of the box. This is the landing page that you will get to if you hadn't already selected the work space. Underneath the covers I have a MySQL database running. We will go look at what is going on there. Underneath there...so we do show tables. We will be able to see that. We don't have any other...

Show databases. One of these years I will remember all the commands. Show databases. You can see I don't have a blogging database just yet. I'm going to go to MySQL script that I created a couple of months ago. Cut and paste this in here. I will have now an SQL script that I can use.

That's what happens when they mess with your resolution before you are ready to talk about this stuff. I'm sure that's never happened to you guys. Control C, back up to that window... all right, so edit, paste... c'mon... edit, paste that in there. Here we go.

So I am creating my database table. If I go back in and show my databases, I have that schema that we just saw up there inside of this. All right. Any questions so far? All right. Let's go back to...

I am going to go through and create a new Seam project. Really, this is just straight out of the box. There are no fix packs to this. This is exactly what is in the download. So, I am going to just call this a blogging application. Literally, just go through and do next, next and next. Really nothing all that challenging in all this.

The next step is going into the MySQL database connection pooling setup in this. Basically, I am going to use the MySQL NODB server profile in this. This is going to create a blogging DS, underscore DS file in the JBoss deploy directory.

I have to go through and select up a configuration for MySQL database. So, I just go through there. Click through a few of these things. Select the MySQL database and remove out the default JAR driver that is in there. I need to go and select one that I already have. Then I need to update my database to be, in this case, blogging three and my password is "testing." I click OK on that. Put this thing that I had added in here for me now that that's been configured. Click test connection. If I am having a good day and the demo gods are with me, it says "ping succeeded." Better than the Sun guys yesterday. No laughter out there. Man, you guys are a tough audience.

Now, that I am all done with this I am now creating all of the artifacts for essentially a base Seam application. Now, we are going to open this up. Our editor is going to have a tab here, a service tab, and we are going to get this opening in the background. Start on that and we'll double click on that while that's firing up.

If you look on the left hand side what we created here was a pile of artifacts that represents essentially a default Seam application. At this point we just have a landing page which is represented by this home XHTML file. We'll get that opening up, also; we can see that.

Is that server tab done, up and running yet? Scroll that over there. My computer is catching up to me. Man, we've got a bigger crowd now. All right. So, that's there. We have that editor open and we'll move this over to here. Now, that our computer has caught up to us, and our server has almost got that application deployed, it looks like. Somebody go out to this web browser window, hit open. I'm waiting on that. Now I have a Seam application with just the artifacts that have been created for me automatically.

One of the things that is really neat about some of the tools that Exadel donated to this effort of this tool set is this two-paned editor that we have. So as I scroll around in here I can maneuver around and I can click on this. Notice the top box is going to move around and update me to that point of my code. If I go update this internationalization and change it to an 18 to abbreviate it, if I go and save this off - where's my mouse - save that off, count to about five or so, Click the refresh button on this a few times. We'll see that that application was re-deployed for us automatically underneath the covers.

So you get that round F5, round trip development on that. So that update was there for you just right away. All right?

So I promised you a database CRUD generated application in about 10 minutes or so. I need to make this just go up a little bit slower, or a little bit higher. Could you...File->New->Generate Entities, and if any of you have done Seam and used the Seam Gen tools, Generate Entities is something that you can fire up there also.

What this is going to do is it's going to create all the various artifacts, all the Backing Bean inside, represent the database schema and also XHMTL files for all the various database components that we already had that we showed you earlier when we were doing this.

So if I look up here, if you look here in the Action class and the source model, you see that I have things like comment, post, roll and user, right? So if I double-click on role, we can see the source code that was generated out of the Hibernate tools that generates all the Backing Beans, the Entity Beans that we'll be leveraging in this.

This becomes then something that you can leverage from within the Facelets that were generated automatically. Notice that the column names are in here. You can see that this has a primary key ID field that was going to be leveraged or used in this. There is also a length of a field that is going to be injected in this, also in the Facelets that are generated.

So, with all that being said, what do I want to do to move that out of the way slightly? We'll move that out of the way ever so slightly. If we refresh this page, we'll now see all the various artifacts that were created, that represented that database for us, right?

So we go into here, a role, and it's going to inject security automatically out of the box. There isn't any security that's really enforced. We'll show you where that is. You can update that. I create a role name, let's just say, "admin" in this. Save that off and click "done."

I'll create another role. We'll call this "A Blogger." We can save that off and we are done. Kind of neat stuff, right? Some of the questions I usually ask people at this point in the demo is, OK, I have four database tables and normal J2EE land of Strut, Spring, Hibernate, coding all this by hand, how many hours or days might you do to get to even this point, right?

So, literally in five or 10 minutes I at least have the beginnings of the role table, able to be edited and modified. We go to the user table. Notice that I have a star in this role. Star, you see that? If you look to the database schema, I actually have a foreign key relationship there, a 'Not Null' on that.

So I actually have to go in there and pick one of these role IDs. Put in there "Jim" password, save this off, click "done." There you go. Now I have a user that could hit this database, right? Do you have a question, sir?

Audience Member 1: Could you briefly describe the table there?

Jim: Could I briefly describe the tables, because you were a little tardy? That's no big deal.

Audience Member 1: I saw the [inaudible]

Jim: No, these are all database table names. All of the things that I'm talking about, role, comment, post, blogging, those are all key names from the database.

Audience Member 1: So I could say...and I can say?

Jim: Exactly. Yep. Yep. Yep. The other thing that's going on is: out of the database we're taking in all those words that were there in the table and they are generating the user interface. We'll see all that if we go back here to this. Things like user name - that's right out of the database definition.

Audience Member 1: [inaudible]

Jim: You just want to look at the columns of that database for a second. We can do that for sure, just a second. So we'll go over to here.

Audience Member 1: All of this is going to generate...[inaudible]?

Jim: Yea. So, we'll use this database, "show tables." Those are the tables that are there. Is that what you are looking for? Then if I do describe, which one? Role?
Audience Member 1: Role.

Jim: We describe role. It looks like a role ID and role name. If I describe a user, you'll see that there's a role ID, which is a foreign key, on that role table.

Audience Member 1: [inaudible]

Jim: Yeah, everything I did was right out of the database schema that was created earlier. So, I have a user ID. I've created that. Let's go in and let's do a post. I don't have any posts yet. It tells me that. If I create a post, notice my user field has a star on it. I need to go down here and select a user.

Select a user. Again, that is going to prompt me for that. It's going to go in there. There is automatically from the date field that was inside of this, I have an AJAX Date Picker that was created for me automatically out of the database schema.

Then I go through and I create a title, whatever that happens to be. Create a body, whatever that happens to be. Then I can save that particular data off. Then I'm done.

I can go through and do the same thing on a comment. And again, no comments exist in this. This is a fresh application. Notice that I have two stars on here. I have to pick a post out of this, one that I just made. Go through and pick a user that is making this post.

So I'll just pick myself in this. Again, the Ajax picker is available for you. Again I'll do another title in this and a body. So I save this off, I can click "done." What you get out of this is now I have a working application that shows me all the various relationships that are going on.

So I can go to my role table, click the "select" on this. I can see that this particular user is in that particular table, is referencing that particular role ID. If I go through and select this particular user, I can go through and look at their posts and look at the comments that they made.

So, literally in 10 minutes or so, haven't really been watching on the clock, we have now reverse engineered this CRUD application and underneath the covers we have created a full set of Entity Beans that back this. Facelets, which you can go edit and update. Now you have got a working web application. Ruby on Rails guys, now you got something that can compete with them at least from the speed point of view.

Audience Member 1: Do I use...[inaudible]

Jim: A little louder, sir.

Audience Member 1: Ruby is also doing the same...?

Jim: Yea.

Audience Member 1: Just telling you.

Jim: Yea. So a very, very powerful stuff here creating J2EE standard components that you can go manage, update, et cetera with usual things that you have been doing for weeks and or months. It's been my experience with Struts, Spring projects is because of all the artifacts you have to create.
Good so far? Are there any questions? Yes, sir.

Audience Member 2: That Ajax Date Picker...

Jim: Ajax DatePicker, yep.

Audience Member 2: Is that [inaudible} guess into a comment...?

Jim: If you go look at both of these tables, they have the same kind of thing. If I look at the comment, one, describe comment, there is a field in there of a Type Date and that's just used to reverse engineer and lay that in there.

Audience Member 2: OK. In that case...[inaudible] so basically a [inaudible] Seam generator project..[inaudible] input rigid type based on column names, column types.

Jim: Column types, yea. Granted most everything is just a text field type box that you are going to put numbers into, but it is pretty easy to drag in some of the other widgets that allow you to do a slider bar. That you might do a zero to a hundred, and have a sliding representation, instead of updating a number, for example.

Audience Member 2: [inaudible]

Jim: A little louder, sir?

Audience Member 2: Session handling for foreign keys, foreign constraints...?

Jim: Like, what kind of things are you looking to be able to do?

Audience Member 2: I want to see, this has to be... [inaudible]

Jim: You can do a error handle and listen for that and show a message to the end user. Is that what you are looking for? Or the other thing that you can do, and that's a good segue into the next part of this, which is, you can do a form field validation on this.

So, I'm going to go into the user table, where the user generated Entity Bean that was generated here. We look down inside of the password column; you can see that user name is "64." Let's say that the password, instead of 64, we'll just call it a max of eight, right? We don't want to increase that.

The other thing that you can do in here is that you can do a pattern on this. And I need to do a RegEx on this, quotes and normal regular expressions. And we'll say, basically, a number or a lower case number or a letter, right?

So I can save this particular thing off. What I need to go do is update the web XML file, just touch that file essentially to trigger a redeployment. What we'll see, when this is all done, is that this will have the ability to inject automatically in the Facelet page, without really you doing any work, the validation of that particular password.

Of course, that's a pretty simple regular expression I just put in there,really not that exciting, but I still get a red box. What? Oh, you know, you need to have... what am I missing? Oh, pattern, what am I missing? See, it's one of those days. That, no, that is the column name. Oh, pattern was what worked earlier, but that's not it. What is the keyword?

Oh, of course, all my hecklers aren't out there to help me out with this. Pattern, pattern, right there and then I need the... OK, that was odd. I picked it from the list and then it works. I typed it in and it didn't. Hey. That's a bug. I'll follow that with Max and the guys. All right?

Oops. I got to go touch that file so that redeployment happens. So, touch that. If you look down in the lower right hand corner of this, you can't quite see it, there's a redeployment going on in this. So, if we go back out to this, we go back to the user list.

Go out and create a new user, we re-deploy the application again. There's no security in this right now out of the box. Go click that particular role. Put 'Jim' on there.

And if I do, a capital A on that, must match, is prompting me the real expression that I defined in there. I also can create something that would translations for me, if that's important in this application, or a localized message, if you will, that updated that type of validation.

Yes, sir?

Audience Member 3: Do all those wired... stuff, looks like coming from [inaudible]?

Jim: Yeah. Right. If you look here at the top of this, if I delete out my name Jim, tab off of that, says value is required. Notice that wasn't page update, it was the AJAX call that was injected automatically with the Generate Entities that we did earlier.

Audience Member 3: It's straight out of the schema?

Jim: Straight out of the schema. Yes, sir.

Audience Member 3: [inaudible]

Jim: Yea.

Audience Member 3: Is there any type of locking [inaudible]...?

Jim: Well, the normal database locking things that you would be doing underneath the covers, well Hibernate would apply. Or are you talking about writing at the code level?

Audience Member 3: No, no, I am talking about one person...[inaudible]

Jim: It depends on your database and what kind of aesthetic rules you are using. As to what kind of errors you are going to get on that. That's a database specific thing, along with an error that would come up on that. Most of the time, the last one is going to win. In a lot of applications that's OK.

If it's not, you know that and you are going to make the database throw up on that. Then you can catch that error and deal with it. Yea.

Audience Member 3: One of the questions...[inaudible]

Jim: Seam makes it very easy to do language packs. There's examples out in the examples and samples directory that shows you how to switch out languages and also leverages....

Audience Member 3: All this generating...[inaudible]

Jim: You'll have to do a little bit of work to make that go, obviously.

Jim: You would start to create Staple Session Beans or things like that. Call those within the Facelets and basically it's the same stuff you have probably have been doing in Struts, same kind of ideas they have done all along.

It doesn't really matter where that goes. The other thing that we have is there is also a notion inside the tool to do a new action. So, I do a File->New->Seam Action. That would create a bean that then you could leverage to use. It also created an automatic test case out of this that you can fire up from within the tool and validate that your particular action and bean are available for you to... they're going to work as you would expect them to work.

Jim: Not in this particular demo. There was ESPSOA demo that talked about JBoss JBPM the other day. If you like to do a small talk though after this, we can do that with you for sure.

Audience Member 3: [inaudible] is there a way to create your own templates to have more...[inaudible]?

Jim: No, nothing yet. If you look out at the blog postings, there are some notions around doing partial rebuilds. So, you're going to update the schema of this particular database table and then you just rebuild that particular one. That isn't there. It's something that they are working on and thinking about.

There is also the idea of generating all the Facelets out of the Entity Beans, after you have altered them, and then giving you a UI out of that, after you have made changes to this. They are working on that right now.
The RichFaces library underneath the covers, that is being leveraged to generate all of this. You can certainly create the code to do that. Out of the box it doesn't do pagination right out of it, but it's very easy for you to update that and make those changes. If you were here before this, one of the guys who are on the corner, Max and company can show you how that would work also.

Well, guys, in 20 minutes or so, we reverse engineered a database. I have a working CRUD application. That's all I have to talk about now.

The ladies I think I have T-shirts for you round the corner. You can get an entitlement to JBoss Developer Studio with your badge and they will hook you up with something that would allow you to do exactly what I just showed you here.

Thank you guys very much. We are going to do this in probably like 10 or 15 minutes. Everybody standing round in the background that would want to sit down and watch the whole thing. Thank you.