Last Time we learned how to configure a hibernate+ spring+ GWT application.
Now we will continue explaining how I built this application.
First, we'll start with the Beans.
For this example we will define one bean called Regions and an interface called Bean.
Region will be the pojo class that contains the mapping for HR schema regions table
Now we have a fully functional hibernate+spring+gwt application that you can use as a template for any web app you want.
Now we will continue explaining how I built this application.
First, we'll start with the Beans.
For this example we will define one bean called Regions and an interface called Bean.
Region will be the pojo class that contains the mapping for HR schema regions table
The @Entity annotation is used to mark this class as an Entity bean. To use this annotation the class must have at least a package scope no-argument constructor.
The @Table annotation is used to specify the table to persist the data. The name attribute refers to the table name. If @Table annotation is not specified then Hibernate will by default use the class name as the table name.
The @Id annotation is used to specify the identifier property of the entity bean. The placement of the@Id annotation determines the default access strategy that Hibernate will use for the mapping. If the @Id annotation is placed over the field, then field access will be used. Instead if it placed over the getter method of that field, then property access will be used. Here we use property access.
The @Column annotation is used to specify the details of the column to which a field or property will be mapped. Here the regionName property is mapped to REGION_NAME column in the REGIONS table. If we don't use the property name of this annotation then hibernate will take the property name as column name.
The Bean interface extends Serializable and Comparable and will be implemented by our Pojo's
The comparable interface is for sorting purposes but is out of the scope of this example.
Now well proceed to write the DAO.
First will tell spring that this class will by a data accesor by using @Repository annotation. The name param is optional, if you don define it then spring will put a default one.
The @Transactional annotation is used to make all the methods to be within a transaction. (the default is read-only)
Next step, the method to retrieve regions:
I used criteria instead of HQL and also implemented the pagination on "DB side".
I also use a "Set" implementation because it doesn't allow repeated values. (this is necessary in case of @OneToMany relationships).
I also use a "Set" implementation because it doesn't allow repeated values. (this is necessary in case of @OneToMany relationships).
There are some interfaces that I've created but I think you could infer them.
Now, with the DAO complete, I'll proceed to explain the Spring Service layer
The business service layer for this example is too simple I will just mention it because of a couple of annotations
The business service layer for this example is too simple I will just mention it because of a couple of annotations
To define a business service in spring It is needed to add the @Service annotation to the Class definition as you can see on the image above. The name attribute is optional.
The @Autowired annotations tells spring to automatically make an instance of the annotated Field. This instantation is made when the spring context loads.
In this service class we call the DAO to read/write data, you could also put some business rules if you need to.
Here, I've defined the following method to retrieve the regions:
The @Autowired annotations tells spring to automatically make an instance of the annotated Field. This instantation is made when the spring context loads.
In this service class we call the DAO to read/write data, you could also put some business rules if you need to.
Here, I've defined the following method to retrieve the regions:
Finally I will explain what you need to write to show the regions list on screen through GWT
First, you need to have an Entrypoint class as the one below:
After we have this class we proceed to develop onModuleLoad to start the process of showing the regions list
First, you need to have an Entrypoint class as the one below:
After we have this class we proceed to develop onModuleLoad to start the process of showing the regions list
In this method we define new object and map objects that already exists on our mapped html.
*Note: You have to tell GWT which module to bind by declaring it on your html as follows:
The findRegions method is in charge of calling an AsynchService to get the Regions List.
*Note: You have to tell GWT which module to bind by declaring it on your html as follows:
The findRegions method is in charge of calling an AsynchService to get the Regions List.
In the method above the declare a new Table that will be inserted within the table container that is a Paragraph object (
)
Once you finished writing this you will get a lot of error and it because we need to define the method en regionService. Will will do that now.
We need to define 2 interfaces one for Synchronous communication and the other one for asynchronous communication (Ajax).
We will start with synch interface: RegionsClientService
The @RemoteServiceRelativePath enables the calling of this service by using a url string or as Javadoc says: "Associates a
Once this interface you need to proceed defining the asynch interface RegionClientServiceAsync as follows:
Notice that the method signature is different from the other interface this is because GWT needs to wrap the method argument inside an AsyncCallBack to work with the result in an asynchronous way.
Finally comes the implementation RegionClientServiceImpl:
As you can notice, this class is a Servlet. That's why you hate to define it on web.xml:
*Note: The trailing url pattern must match the string defined on @RemoteServiceRelativePath
)
Once you finished writing this you will get a lot of error and it because we need to define the method en regionService. Will will do that now.
We need to define 2 interfaces one for Synchronous communication and the other one for asynchronous communication (Ajax).
We will start with synch interface: RegionsClientService
The @RemoteServiceRelativePath enables the calling of this service by using a url string or as Javadoc says: "Associates a
RemoteService
with a relative path. This annotation will cause the client-side proxy to automatically invoke the ServiceDefTarget.setServiceEntryPoint(String)
method withGWT.getModuleBaseURL()
+ value()
as its argument. Subsequent calls to ServiceDefTarget.setServiceEntryPoint(String)
will override this default path."Once this interface you need to proceed defining the asynch interface RegionClientServiceAsync as follows:
Notice that the method signature is different from the other interface this is because GWT needs to wrap the method argument inside an AsyncCallBack to work with the result in an asynchronous way.
Finally comes the implementation RegionClientServiceImpl:
As you can notice, this class is a Servlet. That's why you hate to define it on web.xml:
*Note: The trailing url pattern must match the string defined on @RemoteServiceRelativePath
Right click on the desired webpage and then select run as - Web Application option from the context menu.
This will trigger the starting of Jetty embedded server and will show a link to start the page:
This will trigger the starting of Jetty embedded server and will show a link to start the page:
Now we have a fully functional hibernate+spring+gwt application that you can use as a template for any web app you want.
Comments