text
cyber space for sharing the day to day technology related learning and working experiences with interested parties. http://www.chathurangaonline.com
Thursday, October 10, 2013
SOAP Vs RESTFul Web Services
Saturday, October 5, 2013
One To Many Mapping example (Hibernate/JPA) annotations
Look at the following ER diagram.
by looking at the ER diagram, we can find the following relationship between two entities.
1. Lecturer can teach many courses
2. A course can be taught by only one lecturer
there are two possible ways to map the relationship between these two entities.
1. Mapping as One - To - Many Bidirectional Relationship
In this way, we maintain the mapping in both entities. since the relationship is maintained in both side, it is possible to navigate to both directions.
Lecturer.java
In this way, we do not maintain the mapping in both entities, we only maintain the mapping relationship in the owner entity.
Here the owner entity of the relationship is Course. therefore we maintain only the relationship from Course to Lecturer. on the other hand, Lecturer to Course relationship is not maintained.
Lecturer.java
Hope this will be helpful for you!
Cheers
Chathuranga Tennakoon
chathuranga.t@gmail.com
by looking at the ER diagram, we can find the following relationship between two entities.
1. Lecturer can teach many courses
2. A course can be taught by only one lecturer
there are two possible ways to map the relationship between these two entities.
1. Mapping as One - To - Many Bidirectional Relationship
In this way, we maintain the mapping in both entities. since the relationship is maintained in both side, it is possible to navigate to both directions.
Lecturer.java
package com.chathurangaonline.examples.model;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
@Entity
@Table(name = "Lecturer")
public class Lecturer implements Serializable{
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "name")
private String lecName;
@OneToMany(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST,mappedBy = "lecturer")
private List<Course> courseList;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Course> getCourseList() {
return courseList;
}
public void setCourseList(List<Course> courseList) {
this.courseList = courseList;
}
public String getLecName() {
return lecName;
}
public void setLecName(String lecName) {
this.lecName = lecName;
}
}
Course.javapackage com.chathurangaonline.examples.model;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "Course")
public class Course implements Serializable{
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "course_name")
private String courseName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "lecturer_id")
private Lecturer lecturer;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Lecturer getLecturer() {
return lecturer;
}
public void setLecturer(Lecturer lecturer) {
this.lecturer = lecturer;
}
}
2 . Mapping as One - To - Many Unidirectional relationshipIn this way, we do not maintain the mapping in both entities, we only maintain the mapping relationship in the owner entity.
Here the owner entity of the relationship is Course. therefore we maintain only the relationship from Course to Lecturer. on the other hand, Lecturer to Course relationship is not maintained.
Lecturer.java
package com.chathurangaonline.examples.model;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "Lecturer")
public class Lecturer implements Serializable{
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "name")
private String lecName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLecName() {
return lecName;
}
public void setLecName(String lecName) {
this.lecName = lecName;
}
}
Course.javapackage com.chathurangaonline.examples.model;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "Course")
public class Course implements Serializable{
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "course_name")
private String courseName;
@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "lecturer_id")
private Lecturer lecturer;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public Lecturer getLecturer() {
return lecturer;
}
public void setLecturer(Lecturer lecturer) {
this.lecturer = lecturer;
}
}
Hope this will be helpful for you!
Cheers
Chathuranga Tennakoon
chathuranga.t@gmail.com
Thursday, October 3, 2013
JAX-WS Web Service Unit testing with TestNg / JUnit
text
Monday, September 30, 2013
How to generate JAX-WS client and integrate with client application
my previous post explained about how to deploy the web service in the tomcat web server. So, our web service is up and running now. it is time to create/generate the web service client and integrate it with our application.
we can generate the Web Service client from the WSDL file. wsimport utility will help to generate java based web service client from the given WSDL.So in our scenario, we can generate the web service client with following linux command.
wsimport -keep -verbose -d <directory_to_create_web_service_client> <wsdl_url>
-verborse (print the status of the operation being executed to the terminal)
-d (path to the directory where the web service client should be created)
wsimport -keep -verbose -d /home/chathuranga/java/jax-ws/client http://localhost:8080/calculator-service/calcServiceUrl?wsdl
now we have successfully completed the process of generating the web service client. it can be integrated with your application now.
The source code for the complete example can be found at following github url.
Hope this will helpful for you!
Cheers
Chathuranga Tennakoon
chathuranga.t@gmail.com
we can generate the Web Service client from the WSDL file. wsimport utility will help to generate java based web service client from the given WSDL.So in our scenario, we can generate the web service client with following linux command.
wsimport -keep -verbose -d <directory_to_create_web_service_client> <wsdl_url>
-verborse (print the status of the operation being executed to the terminal)
-d (path to the directory where the web service client should be created)
wsimport -keep -verbose -d /home/chathuranga/java/jax-ws/client http://localhost:8080/calculator-service/calcServiceUrl?wsdl
now we have successfully completed the process of generating the web service client. it can be integrated with your application now.
The source code for the complete example can be found at following github url.
Hope this will helpful for you!
Cheers
Chathuranga Tennakoon
chathuranga.t@gmail.com
Thursday, September 26, 2013
Deploy JAX-WS web service in Tomcat Server
if the web service is going to be deployed in the tomcat server,it
has to be developed as a web application project with some added
configurations.
in order to start the web application project, i prefer to use maven archetype generate command as follows.
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
then it will give me a basic web application project that can be built with maven.
now this is the time to integrate my predefined web service with the web application project.
1. first create the relevant package structure in the web-app project and copy the web service src files there.
2. create the web service deployment descriptor ( sun-jaxws.xml ) file under the webapp/WEB-INF directory. (same directory where the web.xml resides).
the endpoints should be defined as follows.
3. in web.xml file (standard deployment descriptor) defines the WSServletContextListener and WSServlet class as follows.
3. if you do all the above steps correctly, you project structure should be as follows.
now it is the time to build the web service. since the web service is published in the tomcat server, it should be built as a WAR file similar to simple web app.
4. once the web service is compiled and package, copy the WAR file to the webapps directory in the tomcat server.
5. sometimes the web service not be loaded because the tomcat does not have the required libraries to server for web services (By default, Tomcat does not comes with any JAX-WS dependencies). In that case you need to download those dependencies and copy to the “${Tomcat}/lib” directory.
once the above depdencies are copied, restart the tomcat server and try to access the web serice with following URL.
http://localhost:8080/calculator-service/calcServiceUrl
if the web service is working, it should display the following page in the browser.
then we are done.
Summary
the all steps taken for publishing a web service in tomcat can be summarized as below.
SOURCE CODE : - https://github.com/chathurangat/jaxws-calculator-service-webapp
Hope this might be helpful for you!
Cheers
Chathuranga Tennakoon
in order to start the web application project, i prefer to use maven archetype generate command as follows.
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
then it will give me a basic web application project that can be built with maven.
now this is the time to integrate my predefined web service with the web application project.
1. first create the relevant package structure in the web-app project and copy the web service src files there.
2. create the web service deployment descriptor ( sun-jaxws.xml ) file under the webapp/WEB-INF directory. (same directory where the web.xml resides).
the endpoints should be defined as follows.
<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="calcService"
implementation="com.chathurangaonline.jaxws.samples.impl.CalculatorServiceImpl"
url-pattern="/calcServiceUrl"/>
</endpoints>
3. in web.xml file (standard deployment descriptor) defines the WSServletContextListener and WSServlet class as follows.
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>calcService</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>calcService</servlet-name> <url-pattern>/calcServiceUrl</url-pattern> </servlet-mapping> </web-app>
3. if you do all the above steps correctly, you project structure should be as follows.
now it is the time to build the web service. since the web service is published in the tomcat server, it should be built as a WAR file similar to simple web app.
4. once the web service is compiled and package, copy the WAR file to the webapps directory in the tomcat server.
5. sometimes the web service not be loaded because the tomcat does not have the required libraries to server for web services (By default, Tomcat does not comes with any JAX-WS dependencies). In that case you need to download those dependencies and copy to the “${Tomcat}/lib” directory.
- Go here http://jax-ws.java.net/
- Download JAX-WS RI distribution.
- Unzip it and copy following JAX-WS dependencies to Tomcat library folder “{$TOMCAT}/lib“.
- jaxb-impl.jar
- jaxws-api.jar
- jaxws-rt.jar
- gmbal-api-only.jar
- management-api.jar
- stax-ex.jar
- streambuffer.jar
- policy.jar
once the above depdencies are copied, restart the tomcat server and try to access the web serice with following URL.
http://localhost:8080/calculator-service/calcServiceUrl
if the web service is working, it should display the following page in the browser.
then we are done.
Summary
the all steps taken for publishing a web service in tomcat can be summarized as below.
- Create a web service
- Create a sun-jaxws.xml, defines web service implementation class.
- Create a standard web.xml, defines
WSServletContextListener,WSServletand structure of a web project - Copy JAX-WS dependencies to “${Tomcat}/lib” directory.
- Generate WAR file and copy to “${Tomcat}/webapp” directory.
- access the web service from browser and check whether it is up and running.
SOURCE CODE : - https://github.com/chathurangat/jaxws-calculator-service-webapp
Hope this might be helpful for you!
Cheers
Chathuranga Tennakoon
Labels:
Apache Tomcat,
JAX-WS,
SOAP,
WebService
Thursday, September 19, 2013
Simple JAX-WS Webservice - Calculator Implementation
First we will create a Simple JAX-WS Web Service. if you do not know how to create such web service, you can refer my previous article that describes how to develop simple JAX-WS Service.
Click Here to See How To Create Simple JAX-WS Service
i assumed that you have created a simple web service and it is up and running. we will modify that web service to provide the calculator based web service operations. in this calculator web service, we support for three operations.
1. Addition
2. Multiplication
3. Subtraction
you can see the SEI (Service Endpoint Interface) and SIB (Service Implementation Bean) as follows.
SEI - CalculatorService.java
SIB - CalculatorServiceImpl.java
Service Publisher (Only for Development Environment)
once everything is added correctly, our project will looks like as follows.
now we have a calculator web service that is ready for the deployment. now we will publish our web service using the Endpoint.publish() method and check whether it is working. since we are uisng this in the test environment, we do not need to deploy this is a J2EE application server at the moment. but in our next article we will look at how to deploy the web service in tomcat server.
once the web service is published you can access the web service in the following url.
http://localhost:6655/jaxWsCalcService
once the web service is published, we need to write/generate a web service client to comsume the web service. it will be discussed in following blog post.
How to generate JAX-WS service client and integrate with Java Application
you can download the source code from following gitHub repo.
https://github.com/chathurangat/jaxws-calculator-service
Hope this will be helpful for you!
Cheers
Chathuranga Tennakoon
www.chathurangaonline.com
Click Here to See How To Create Simple JAX-WS Service
i assumed that you have created a simple web service and it is up and running. we will modify that web service to provide the calculator based web service operations. in this calculator web service, we support for three operations.
1. Addition
2. Multiplication
3. Subtraction
you can see the SEI (Service Endpoint Interface) and SIB (Service Implementation Bean) as follows.
SEI - CalculatorService.java
package com.chathurangaonline.sample.jaxws;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* <p>
* Service Endpoint Interface for the calculator service.
* </p>
*/
@WebService
public interface CalculatorService {
@WebMethod
long add(long number1, long number2);
@WebMethod
long subtract(long number1, long number2);
@WebMethod
long multiply(long number1, long number2);
}
SIB - CalculatorServiceImpl.java
package com.chathurangaonline.sample.jaxws;
import javax.jws.WebService;
/**
* <p>
* Service Implementation Bean (SIB) for CalculatorService endpoint interface
* </p>
*/
@WebService
public class CalculatorServiceImpl implements CalculatorService{
@Override
public long add(long number1, long number2) {
return number1+number2;
}
@Override
public long subtract(long number1, long number2) {
return number1+number2;
}
@Override
public long multiply(long number1, long number2) {
return number1*number2;
}
}
Service Publisher (Only for Development Environment)
package com.chathurangaonline.sample.jaxws;
import javax.xml.ws.Endpoint;
/**
* <p>
* simple web service publisher for testing the web service in the development environment.
* remove this publisher class in the production environment and deploy the web service with
* j2ee compliant web server.
* </p>
*/
public class CalculatorServicePublisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:6655/jaxWsCalcService",new CalculatorServiceImpl());
}
}
once everything is added correctly, our project will looks like as follows.
now we have a calculator web service that is ready for the deployment. now we will publish our web service using the Endpoint.publish() method and check whether it is working. since we are uisng this in the test environment, we do not need to deploy this is a J2EE application server at the moment. but in our next article we will look at how to deploy the web service in tomcat server.
once the web service is published you can access the web service in the following url.
http://localhost:6655/jaxWsCalcService
once the web service is published, we need to write/generate a web service client to comsume the web service. it will be discussed in following blog post.
How to generate JAX-WS service client and integrate with Java Application
you can download the source code from following gitHub repo.
https://github.com/chathurangat/jaxws-calculator-service
Hope this will be helpful for you!
Cheers
Chathuranga Tennakoon
www.chathurangaonline.com
Tuesday, September 17, 2013
how to exclude web service operation (web method) from the web service in jax-ws
The annotation @WebService tells the
server runtime environment to expose all public methods on that bean as a
Web service. In below example, all the public methods in the web service will be exposed as web methods.
suppose that you do not need to expose the addNewHotel method as a web service operation method. but the default implementation of the @WebService method will expose all public methods as web service operations. but there is a special annotation that will prevent the method by exposing/publishing as a web service operation.
Refer the below class and try to publish the web service.
here the addNewHotel method will not be exposed or published as a web service operation/method.
i hope that this will be helpful for you!
Cheers
Chathuranga Tennakoon
chathuranga.t@gmail.com
www.chathurangaonline.com
package com.chathurangaonline.sample.jax.ws;
import javax.jws.WebService; import com.chathurangaonline.sample.model.Hotel;
@WebService
public class HotelWebService{
public Hotel retriveHolelByCity(String cityName) {
//todo implementation should goes here
return new Hotel();
}
public void addNewHotel(Hotel hotel) {
//todo implementation should goes here
}
}
suppose that you do not need to expose the addNewHotel method as a web service operation method. but the default implementation of the @WebService method will expose all public methods as web service operations. but there is a special annotation that will prevent the method by exposing/publishing as a web service operation.
@WebMethod(exclude = true)
Refer the below class and try to publish the web service.
package com.chathurangaonline.sample.jax.ws;
import javax.jws.WebService; import com.chathurangaonline.sample.model.Hotel;
@WebService
public class HotelWebService{
public Hotel retriveHolelByCity(String cityName)
//todo implementation should goes here
return new Hotel();
}
@WebMethod(exclude = true)
public void addNewHotel(Hotel hotel) {
//todo implementation should goes here
}
}
here the addNewHotel method will not be exposed or published as a web service operation/method.
i hope that this will be helpful for you!
Cheers
Chathuranga Tennakoon
chathuranga.t@gmail.com
www.chathurangaonline.com
Saturday, September 7, 2013
Hibernate Relationship Mapping with Compound Primary Key
text
Wednesday, September 4, 2013
How to create compound primary key in hibernate with @IdClass Annotation
text
Tuesday, September 3, 2013
How to create compound primary key in hibernate with @EmbeddedId Annotation
text
Sunday, September 1, 2013
How to create compound primary key in hibernate with @Embeddable Annotation
text
Friday, August 30, 2013
Compound Primary Keys in Hibernate using Annotations
most of the cases, identifier (primary key) of the entity will be a single attribute. but there will be some cases where the primary key of the entity will be comprised by several attributes. In other words, primary key will consist of more than one column. if the primary key consist of more than one attribute/column, we called it as composite primary key or compound primary key.
In hibernate, there are three ways that can be used to create compound primary key in a entity class using annotations. In all of these three scenarios, it will be required to create a separate class for the compound primary key.
1. Compound primary key with @Embeddable annotation
mark the compound primary key (PK) class with @Embeddable annotation and add the instance of the PK class to the entity class as a normal property. then mark it with @Id annotation as usual.
2. Compound Primary key with @EmbeddableId annotation
create the compound PK class as POJO (Plain Old Java Object) and add it as a normal property for the Entity class. then mark that instance with @EmbeddableId annotation.
3. Compound Primary key with @IdClass annotation
declare the compound primary key class with @IdClass annotation and add the all primary key properties of that class to the entity class as normal properties. then mark all the added primary key properties with @Id annotation.
Hope this will be helpful for you!
Cheers
Chathuranga Tennakoon
www.chathurangaonline.com
In hibernate, there are three ways that can be used to create compound primary key in a entity class using annotations. In all of these three scenarios, it will be required to create a separate class for the compound primary key.
1. Compound primary key with @Embeddable annotation
mark the compound primary key (PK) class with @Embeddable annotation and add the instance of the PK class to the entity class as a normal property. then mark it with @Id annotation as usual.
2. Compound Primary key with @EmbeddableId annotation
create the compound PK class as POJO (Plain Old Java Object) and add it as a normal property for the Entity class. then mark that instance with @EmbeddableId annotation.
3. Compound Primary key with @IdClass annotation
declare the compound primary key class with @IdClass annotation and add the all primary key properties of that class to the entity class as normal properties. then mark all the added primary key properties with @Id annotation.
Hope this will be helpful for you!
Cheers
Chathuranga Tennakoon
www.chathurangaonline.com
Subscribe to:
Posts (Atom)


