Monday, July 11, 2011
The project is finished
Ok, the project is done and presented to the professor. That is good, I learned a lot while working on this application.
Saturday, July 2, 2011
Transactions
Today I found a really good way to deal with transaction management in EJB 3.0 and Hibernate.
I found it on a Java Developer's Forum called "Coderanch", here: http://www.coderanch.com/t/216252/ORM/java/hibernate-Container-Managed-Transactions-CMT
It proposes the usage of container managed transactions (CMT) on the level of EJB beans together with the transactions in Hibernate session.
Like this: When a CMT transaction in an EJB bean is running, and the Hibernate transaction is started, it joins the CMT one.
I was worrying there might be some conflict about it, but looks like everything is fine. It is just necessary to explicitly set the transaction annotation for each bean:
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import static javax.ejb.TransactionAttributeType.*;
@Stateless
@TransactionManagement(value=TransactionManagementType.CONTAINER) //CMT type transaction
@TransactionAttribute(value=REQUIRED) //Required as its attribute
public class MyEJB implements MyEJBRemote {
.... }
******************* Update: *******************
By some reason the NetBeans 6.9 doesn't resolve the input for this code correctly, so in case you copy it please make sure you copy the imports too.
I found it on a Java Developer's Forum called "Coderanch", here: http://www.coderanch.com/t/216252/ORM/java/hibernate-Container-Managed-Transactions-CMT
It proposes the usage of container managed transactions (CMT) on the level of EJB beans together with the transactions in Hibernate session.
Like this: When a CMT transaction in an EJB bean is running, and the Hibernate transaction is started, it joins the CMT one.
I was worrying there might be some conflict about it, but looks like everything is fine. It is just necessary to explicitly set the transaction annotation for each bean:
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import static javax.ejb.TransactionAttributeType.*;
@Stateless
@TransactionManagement(value=TransactionManagementType.CONTAINER) //CMT type transaction
@TransactionAttribute(value=REQUIRED) //Required as its attribute
public class MyEJB implements MyEJBRemote {
.... }
******************* Update: *******************
By some reason the NetBeans 6.9 doesn't resolve the input for this code correctly, so in case you copy it please make sure you copy the imports too.
Achievements: user session
The project is almost done. Tonight I managed to transfer the user session handling from previously used Http session into session, stored into the EJB 3.0 Stateful Bean.
In general, some testing was done and the UI slightly changed for better usability.
In general, some testing was done and the UI slightly changed for better usability.
Thursday, June 30, 2011
To Do List
Currently most of the presentation layer is transferred to JSP with Servlets instead of just plain servlets as it was done previously, in the first version of the project. That gives a clear separation of business logic and interface elements.
Also a registration possibility was added, in the previous version the users could use only the those user names, that were already in the DB. (That was due to Hibernate DB writing problem)
As the project is coming to it's final stage, here is a list of things that still need to be done:
The priority things to be implemented are the following:
- Proper handling of transactions - only Hibernate transactions are to be used and I should switch off the other types in order to prevent conflicts
- User session needs to be transferred from plain http session to a session, supported by a stateful bean
- Documentation of this new version
- General improvements and testing
Also a registration possibility was added, in the previous version the users could use only the those user names, that were already in the DB. (That was due to Hibernate DB writing problem)
As the project is coming to it's final stage, here is a list of things that still need to be done:
The priority things to be implemented are the following:
- Proper handling of transactions - only Hibernate transactions are to be used and I should switch off the other types in order to prevent conflicts
- User session needs to be transferred from plain http session to a session, supported by a stateful bean
- Documentation of this new version
- General improvements and testing
Wednesday, June 29, 2011
Writing to a DB with Hibernate
Finally I managed to solve that really old problem of writing into database with the Hibernate approach. (see post from 12.02.2011)
Previously I was thinking, that just as reading from a DB it should be working with HQL queries. They didn't work.
Now I know how to do it correctly: there is a safer method in the Hibernate session - saveOrUpdate. It simply writes an instance of a Hibernate-generated database entity class in to the base. And of course it is possible to use transaction for that.
Previously I was thinking, that just as reading from a DB it should be working with HQL queries. They didn't work.
Now I know how to do it correctly: there is a safer method in the Hibernate session - saveOrUpdate. It simply writes an instance of a Hibernate-generated database entity class in to the base. And of course it is possible to use transaction for that.
Monday, June 27, 2011
JNDI and Service Locator pattern
Right now I am in the process of developing a portable and server independent way of data exchange between the layers of architecture and the architectural elements. So far I found the following methods being proposed in literature:
- accessing through plain JNDI
- using Service Locator design pattern (not recommended by for example: http://www.javalobby.org/articles/service-locator/)
Looks like the question of the design patterns and their usage is changing as the newer versions of EJB appear. I will have to spend more time on this issue than was planned earlier.
Updated:
A little addition about design patterns by Paul Wheaton (2006):
"I think that any pattern being used in an application could/should(/must!) be trumped by the simplest thing that could possibly work."
Looks like I should keep that in mind.
- accessing through plain JNDI
- using Service Locator design pattern (not recommended by for example: http://www.javalobby.org/articles/service-locator/)
Looks like the question of the design patterns and their usage is changing as the newer versions of EJB appear. I will have to spend more time on this issue than was planned earlier.
Updated:
A little addition about design patterns by Paul Wheaton (2006):
"I think that any pattern being used in an application could/should(/must!) be trumped by the simplest thing that could possibly work."
Looks like I should keep that in mind.
Saturday, June 25, 2011
Achievements
Today I managed to organize the 3 tiers - presentation, business-logic and persistence in jar and war accordingly. Previously I had some issues with Hibernate concerning that and had to store everything, related to Hibernate in war, now this one is solved.
The next issue of the first version I have to take care of is the proper division of the presentation layer between JSP and Servlets.
So far I have studied a several tutorials, but they didn't cover those questions, so I look further.
The next issue of the first version I have to take care of is the proper division of the presentation layer between JSP and Servlets.
So far I have studied a several tutorials, but they didn't cover those questions, so I look further.
Sunday, June 19, 2011
EJB 3.1
Currently I am working on the possibility to switch to EJB 3.1 instead of EJB 3.0. It follows the tendency for further simplification, declared by version 3.0. For example it provides the "interfaceless" mode. Now I am studying the issues that might appear for cooperation with Hibernate and other technologies.
What is clear so far: EJB 3.1 naturally cooperates with NetBeans 6.9-7.0 and GlassFish 3.x, which is already good.
Any further discoveries on this question will follow.
What is clear so far: EJB 3.1 naturally cooperates with NetBeans 6.9-7.0 and GlassFish 3.x, which is already good.
Any further discoveries on this question will follow.
Saturday, June 11, 2011
Time to continue
Now I can see the gap in my schedule and will use it to start working on the project again. A lot needs to be done and there is time only till the 3rd of July.
The To-do list for the project is in the previous post (from February, that is)
The To-do list for the project is in the previous post (from February, that is)
Saturday, February 26, 2011
The project continues
I have to work on the project more and change certain things in my architecture and technologies. Here is the list of things that has to be done:
- Separate presentation from contents (can be done with JSP + servlets instead of just servlets that I have now)
- Remove business logic from servlets and put it into proper EJBs
- Separate presentation (servlets + JSP) and business logic putting them into WAR and JAR respectively.
- Implement proper design patterns (for example DTO and DAO, Façade, business delegation and others)
- Take care of transactions - they are done automatically by database, that should not overlap with other types of transactions.
- JNDI - everything should be accessed through it
I will continue posting relevant information on the project in this blog.
Monday, February 14, 2011
Valentine's Day post
It is already 4 a.m. of the Valentine's Day and I have finally finished the project. Of course it has a number of things, that was possible to do better. I had to exclude much of what I originally planned from the project scope because of limited time resources.
What I finally got:
The Java dynamic web application implements 3-layered architecture with
What I finally got:
The Java dynamic web application implements 3-layered architecture with
- Web tier in Java EE Servlets
- Business-logic tier in EJB 3.0 Stateful and Stateless beans
- Persistence tier in Hibernate 3.2.5
The main issue is that web tier is too "thick" in this application, because of that the separation between content and presentation was not properly reached everywhere. The issue occurred on the initial phase of the project, in the beginning of work with servlets. As all problems of the early stages, this one was hard to solve later.
Sunday, February 13, 2011
Final Report and what remains to be done.
In parallel with continued improvements of the project, today I began to compose a final report on it. Schemes, diagrams as well as plain-text words take time, and there is not much of it left.
For now I want to make a conclusion of what is yet to be done:
For now I want to make a conclusion of what is yet to be done:
- Final report
- Login function: stateful beans + servlet for it (session)
- Cart function: stateful bean + servlet (that should be the same session)
- Further work on separating the content and presentation.
Because of the recent issue with the Hibernate I decided to remove the "Registration" option from the application. It was supposed to insert data into the database (new user info), but it was impossible to do so - more information in the previous post.
Saturday, February 12, 2011
Hibernate HQL issue
Previously I was only retrieving the information from database using HQL queries, it worked fine. This time I had to insert some information their. And I suddenly found out that it is not that trivial.
HQL doesn't support INSERT... VALUES... at all. (Security reasons, I suppose). The documentation says it supports INSERT INTO...SELECT... and gives some examples about that. But when I try to run such queries they show an exception: "query must begin with SELECT or FROM".
I wonder, how could that be, if in their own documentation (http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html) they state that this:
insert into DelinquentAccount (id, name) select c.id, c.name from Customer c where ...
should work.
I am puzzled about how to do it properly. Their is absolutely not enough time to change the architecture now. If anybody has any suggestions, feel free to express them.
Update on the 13.02.2011: I am sure it is possible to insert info into the database using Hibernate, but I couldn't find any trivial way. Due to the time limitations I will have to exclude that from the project scope.
Update on the 29.06.2011: At last found the solution for this issue! See post from 29.06.2011
HQL doesn't support INSERT... VALUES... at all. (Security reasons, I suppose). The documentation says it supports INSERT INTO...SELECT... and gives some examples about that. But when I try to run such queries they show an exception: "query must begin with SELECT or FROM".
I wonder, how could that be, if in their own documentation (http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html) they state that this:
insert into DelinquentAccount (id, name) select c.id, c.name from Customer c where ...
should work.
I am puzzled about how to do it properly. Their is absolutely not enough time to change the architecture now. If anybody has any suggestions, feel free to express them.
Update on the 13.02.2011: I am sure it is possible to insert info into the database using Hibernate, but I couldn't find any trivial way. Due to the time limitations I will have to exclude that from the project scope.
Update on the 29.06.2011: At last found the solution for this issue! See post from 29.06.2011
Thursday, February 10, 2011
New achievements: interface
Yesterday I finished working on the presentation part of the project. I developed all the servlets to represent the basic structure of the web application. Also I attached some of the previously developed Enterprise Java Beans to process data from the database.
By now I can say that my project will be an online bookstore - classical example.
By now I can say that my project will be an online bookstore - classical example.
Tuesday, February 8, 2011
Exams...
Unfortunately I had to make a forced break in the project work process because of 3 exams. Tomorrow is the last one and I will continue the work after it is over.
By the way: Happy Birthday to me! :-)
By the way: Happy Birthday to me! :-)
Thursday, February 3, 2011
Achievements again
Finally I managed to combine all the required technologies. Now a Servlet represents a piece of business-logic which gets data from database through Hibernate session. The problem was with the last part. In the class, that interacts with Hibernate objects, instead of using the current Hibernate session, it was necessary to create a new one (unlike what was written in a tutorial):
this.session = HibernateBookUtil.getSessionFactory().openSession();
because otherwise the class couldn't "see" the current session. Maybe this issue is really basic but for me it was very confusing.
this.session = HibernateBookUtil.getSessionFactory().openSession();
because otherwise the class couldn't "see" the current session. Maybe this issue is really basic but for me it was very confusing.
Wednesday, February 2, 2011
Problems of technology integration
I am having a lot of issues on the integration of Hibernate with EjB and Servlets, it is just frustrating. When they don't work, they show exceptions, of course. It seems to me that Captain Obvious himself is writing those exceptions for me :)
Seriously: "A system exception occurred during an invocation on EJB BookController method". He told me that an exception happened, that's very informative, thank you, Captain Obvious! Of course they seem truly un-googlable.
Sorry, this post was full of frustration.
Seriously: "A system exception occurred during an invocation on EJB BookController method". He told me that an exception happened, that's very informative, thank you, Captain Obvious! Of course they seem truly un-googlable.
Sorry, this post was full of frustration.
Tuesday, February 1, 2011
Short notice
The Hibernate reverse engineering method is very convenient, but lacks a very important functionality. In case you changed the database it is impossible to add POJO by this method incrementally. So the best solution is to begin with the base and don't change it during the project. That is not always possible.
Important: Maybe I was just unable to find and use the right functionality. So I am open for recommendations on this issue.
Important: Maybe I was just unable to find and use the right functionality. So I am open for recommendations on this issue.
Monday, January 31, 2011
Issues
During the work on the project today I met an issue that I couldn't solve yet: integration of Hibernate and EJB3. It is giving exceptions (Like, "Exception while invoking class org.glassfish.ejb.startup.EjbDeployer load method") and others, that are barely Googlable - they address to forum topics, where people raise the same questions but don't get any valid answers.
- Could anybody recommend a tutorial on Glassfish + EjB3 + Hibernate combination? I would be grateful for any help.
I managed to find material on Hibernate with EjB and JBoss - there it is different and could be easier, but switching technologies doesn't seem like a good idea to me, since there is so little time left.
I found a good tutorial on Hibernate: http://netbeans.org/kb/docs/web/hibernate-webapp.html
Instead of required EjB3 and Servlets it uses JSF and xhtml, but the overall architecture is correct and persistence tier is as it is supposed to be.
Saturday, January 29, 2011
Daily report
It took me a long time to get into the new set of technologies, to understand and solve all the minor issues. Finally I managed to bring the environment back into working condition - there was an issue with Hibernate mapping between classes and database entries. Now I can finally start gathering all my previous attempts into the actual final project.
To Do:
To Do:
- Design (Template?)
- EJB interconnection with Hibernate
- Group all the business logic in EJB
- Organize all presentation-related code in servlets
So, what is left is just to start and to finish, more or less. (That's a Russian proverb paraphrased, I don't really know whether it means something in English)
Thursday, January 27, 2011
New set of technologies
Because of the constant problems with the set of technologies described in the previous post, today I finally decided to try the new one:
Netbeans 6.9.1 + Glassfish + Java DB (SQL Express in perspective) + Hibernate
After a long time setting them all up and understanding their differences from what I tried before, I managed to achieve some results. I reached proper division of layers: Hibernate maps DB structure into objects (persistency), JSP accesses Hibernate (business logic), Java servlet accesses JSP and represents the data (presentation).
To Do:
Netbeans 6.9.1 + Glassfish + Java DB (SQL Express in perspective) + Hibernate
After a long time setting them all up and understanding their differences from what I tried before, I managed to achieve some results. I reached proper division of layers: Hibernate maps DB structure into objects (persistency), JSP accesses Hibernate (business logic), Java servlet accesses JSP and represents the data (presentation).
To Do:
- Further attempts to substitute JavaDB with SQL Express, since Derby is very hard to debug
- Implement business logic on EJB
- Solve a problem with default package (objects from other packages don't interact with DB properly)
Wednesday, January 26, 2011
Today's progress
Today there were more problems, rather than achievements:
- Hibernate + Eclipse + EJB3 + Oracle Express + Derby combination, described in one of the tutorials didn't work. I still cannot see why not. Something is wrong with its connection to database. Probably to Derby.
- Failed attempt to install MyEclipse, the "New software" mechanism showed me some errors about dependencies.
Still, achievements:
- Hibernate + Eclipse + EJB3 + Oracle Express + Derby combination, described in one of the tutorials didn't work. I still cannot see why not. Something is wrong with its connection to database. Probably to Derby.
- Failed attempt to install MyEclipse, the "New software" mechanism showed me some errors about dependencies.
Still, achievements:
- Created a spreadsheet on all the related technologies and abbreviations. Attempt to "Put an octopus in the bottle", as was mentioned on the lecture
- Got theoretical knowledge about Hibernate, trying to implement it practically.
- Studied and tried Spring
- Studied JavaScript
- Started alternative approaches to Hibernate
- Tried Ivy for Eclipse
Any help would be appreciated, for example good links to fresh tutorials and info on Hibernate + Eclipse + EJB3 working together.
Tuesday, January 25, 2011
Further work
Over the past few weeks I worked extensively on the following technologies for the project:
- JSP and Servlets on top of Eclipse Helios and Tomcat 7.0
- EJB3, which required a lot about the following technologies:
- JBoss AS 6.0
- Glassfish 3.0.1
- Beans, JNDI, Java EE 5
4. Started with Hibernate and DB related issues
Note: got more theoretical, rather than practical knowledge so far. None of the open-source technologies worked as they had been described in tutorials. Different versions of the same technologies show totally different (and usually non-documented) behavior. Eclipse mechanism for adding new software is really not reliable.
So far all the difficulties were solved!
Subscribe to:
Comments (Atom)