This is a simple, simple case of a database, a table with a long ID and an Integer ROOMNR,
An Entity bean and a Session bean, all called up by a JSP web app.
Using Netbeans 6.7.1 on Glassfish 2.1.
I've tried it with the Glassfish Derby database, and again
with a MySQL database and in both cases the result is the same :
I can read from the table but not write to the table.
Here in the stateless session bean I create a bean id = 1L and roomnr = 201
and persist it.
List all the rooms with id == 1L and one should show up, shouldn't it?
Then list all the rooms in the DB and two should show up, one I put in by hand--id=10L and the new one--id=1L
It only shows the bean id = 10L.
What is missing? A security matter? Does the persistence.xml need special tweaking
Must a special user be prepared?
i think there's no wrong with your snippet code..
have you solved your problem?..
Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Joined: Dec 26, 2008
Yes I have, I'll put the whole odessey down here in print in a couple hours.
Joined: Dec 26, 2008
a bit more than two hours but....
Bringing Netbeans 6.7, Glassfish 2.x and EJB Entity and Session Beans together
Start Glassfish running, the JavaDB running and Netbeans running.
1. Database : My database is called Danny. I contact it over the JNDI name "jdbc/Herman"
Over http://localhost:4848 get into Glassfish Administrator portal and over Resources / JDBC / Connection Pools
create a new pool name "BigPool", resource type : javax.sql.DataSource, Vendor : "Derby"
After "Next" you have at the bottom of the frame an astounding number of options but you
only need 6 :
ConnectionAttributes, set this to ";create=true"
DatabaseName : Danny
User : APP
Password : APP
PortNumber : 1527
ServerName : localhost
Lose the rest, especially the ssl stuff.
Try the ping button to make sure the configuration works.
With Resources / JDBC / JDBC Resources create and JNDI resource named : "jdbc/Herman"
and select the pool "BigPool" which you just created.
2. In Netbeans in the left hand Project Frame create a New Project / Java EE / EJB Module
Name it Shaila-ejb, the -ejb is a tradition/rule/law that Netbeans requires.
Otherwise keep the default settings.
Under Source Packages in the project tree add a package "com.members".
Right-click on the new package and select New / Session Bean and name it Shaila.
Set it Stateful, and check both Local and Remote interfaces.
The coding for ShailaBean.java is at the end of this article. Shaila is a Stateful Session Bean
and acts as a facade for the Books Entity Bean.
At the start you can see @Stateful(mappedName="ShailaEJB")
"mappedName" means JNDI name and in localhost:4848 you can see it over
Application Server / JNDI Browsing
Next comes the real fun.
When you have the three files for the Session bean, ShailaBean.java, ShailaLocal.java and
ShailaRemote.java, then you can make the Entity Bean.
It has a rather nondescript beginning. Over the package for Shaila right click and
New / Entity Bean. Name it "Books" and click on Create Persistence Unit. Name the unit
"MyPu", keep the provider at the default TopLink and set the data source as jdbc/Herman.
Use Java Transaction APIs is set and Table Generation Strategy is at Create.
The coding for Books.java is at the end of this article.
Note the getter and setter methods for the two fields of the table "Books",
ID, a Long, and TITLE, a string. In SQL these are bigint and varchar(255).
Also note where "MyPu" appears near the top of the class.
Now is a good time to have a beer.
3. Over the EJB Project Shaila-ejb right click and run the "verify" test.
When it passes run the Build and then Deploy.
After deployment see with the admin portal (localhost:4848) whether the
EJB appears und Applications / EJB Modules. Also check Application Server / JNDI Browsing
and locate ShailaEJB in the list.
In the left frame of Netbeans is a third tab called "Services". Here you can connect
with the Derby database with a right click on the Databases node. Remember that it
is named Danny, port : 1527, host : localhost, user/password : APP/APP. Don't forget
to use Refresh and then you can see the Books table which was created during deployment
Have another beer.
4. Now in the Project Frame create a New Project / Java / Java Application.
Name it "Gonzo". This is not a Java EE application so you must add
4 jars to the libraries area of the new project. In %AS_INSTALL%\lib are
javaee.jar, appserv-admin.jar and appserv-rt.jar. Deeper down, in
%AS_INSTALL%\lib\install\applications\jmsra is imqjmsra.jar.
So, right click over the Libraries folder in the project explorer
and select Add JAR/Folder.. and select the four jars.
Also bring in the EJB by doing a right click over Libraries and select
Add Project... to find and select Shaila-ejb.
Under Source Packages find Main.java and fill in the code with the list
at the end of this article. Note that contact with the EJB is made with :
InitialContext ic = new InitialContext(props);
Object o = ic.lookup("ShailaEJB"); // using the JNDI name
ShailaRemote shaila = (ShailaRemote)o;
There is another possibility for contacting ShailaEJB by
making an instance variable for the Main class, static of course :
EJB (mappedName="ShailaEJB") // specify the JNDI name
private static ShailaRemote shaila;
However I haven't tried it yet in this manner.
One thing to remember : you can Clean and Build the project Shaila-ejb and
then Deploy it. Next if you Clean (!!!) and Build the Gonzo application, the *.class files
of Shaila will be deleted, the Shaila-ejb.jar will be deleted and the class files
and jars for Shaila-ejb will be rebuilt, as well as the same with Gonz.
But Shaila will be REMOVED from the server !!!
Not only is Gonzo cleaned and rebuilt. The project Shaila-ejb which Gonzo contains in
Libraries, will be cleaned and rebuilt and UNDEPLOYED.
Don't Clean Gonzo.
After you Build the Gonzo application you can have another beer.
5. Now, from Gonzo you can call into Shaila and create, delete, modify and list
the books in the database. Modifying the code in Gonzo is straightforward.
All this is from the last couple weeks and I hope it can save you some
blood sweat and tears. Have fun.