Spring, Hibernate, Data Modeling, REST and TDD:Agile Java Design and Development by Amritendu De

Spring, Hibernate, Data Modeling, REST and TDD:Agile Java Design and Development by Amritendu De

Author:Amritendu De [De, Amritendu]
Language: eng
Format: epub, pdf
Published: 2015-01-10T22:00:00+00:00


var username = $(“#userSelectBox”).find(‘option:selected’).text();

var groupname = $(“#groupSelectBox”).find(‘option:selected’).text();

if(null != userid && “” != userid && null!= groupid && “” != groupid) {

var formData={“userDto”:{“id”:userid},”groupDto”:{“id”:groupid}};

$.ajax({

url : “/Spring-OODD/manytomanyunidirectional/usergroup/mock/isPresent”,

type: “POST”,

data : JSON.stringify(formData),

beforeSend: function(xhr) {

xhr.setRequestHeader(“Accept”, “application/json”);

xhr.setRequestHeader(“Content-Type”, “application/json”);

},

success: function(data, textStatus, jqXHR)

{

if(!data) {

createUserGroup(userid, groupid, username, groupname);

} else {

alert(“User already assigned to this group”);

}

},

error: function (jqXHR, textStatus, errorThrown)

{

alert(“Error Status Create:”+errorThrown);

}

});

}

return false;

}

We will also look at the createUserGroup Javascript method which creates the join between the selected User and Group.

function createUserGroup(userid,groupid, username, groupname){

var formData={“userDto”:{“id”:userid, “name”:username},”groupDto”:{“id”:groupid, “name”:groupname}};

$.ajax({

url : “/Spring-OODD/manytomanyunidirectional/usergroup/mock/create”,

type: “POST”,

data : JSON.stringify(formData),

beforeSend: function(xhr) {

xhr.setRequestHeader(“Accept”, “application/json”);

xhr.setRequestHeader(“Content-Type”, “application/json”);

},

success: function(data, textStatus, jqXHR)

{

loadObjects();

},

error: function (jqXHR, textStatus, errorThrown)

{

alert(“Error Status Create:”+textStatus);

}

});

return false;

}

Next we will scrutinize the deleteUserGroupObject Javascript method which takes the User identifier and Group identifier as an input and removes the reference from the join.

function deleteUserGroupObject(userid,groupid){

var formData={“userDto”:{“id”:userid},”groupDto”:{“id”:groupid}};

delurl=”/Spring-OODD/manytomanyunidirectional/usergroup/mock/remove”;

$.ajax({

url : delurl,

type: “POST”,

data : JSON.stringify(formData),

dataType: “json”,

beforeSend: function(xhr) {

xhr.setRequestHeader(“Accept”, “application/json”);

xhr.setRequestHeader(“Content-Type”, “application/json”);

},

success: function(data, textStatus, jqXHR)

{

loadObjects();

},

error: function (jqXHR, textStatus, errorThrown)

{

alert(“Error Status Delete:”+textStatus);

}

});

}

Develop the Service

The ensuing step is the actual service development which involves four main steps – creating the entity, creating the data access tier, creating the business service tier and creating the JSON based REST capabilities. We will begin with the entity and go up to the REST service.

Develop the resource (entity) tier

First, we create two instances of User and then eventually check for two instances of the user. Later, we do an update and test whether the modifications are in place. Lastly, we remove one existence and test whether the count is solitary. Also, we add two variations of Group and associate them with the remaining instances of the User.

@RunWith( SpringJUnit4ClassRunner.class )

@ContextConfiguration( locations = { “classpath:context.xml” } )

@TransactionConfiguration( defaultRollback = true )

@Transactional

public class UserTest {

@Autowired

private SessionFactory sessionFactory;



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.