LDSTechForumProjects

Database Development 2

This training covers More Spring JDBC and Transaction Management.

Prerequisites

Slide Deck

Section 1: Review, More Spring JDBC

Presentation

Lab 1

  • Download the db development 2 project template
  • Unzip the artifact where you typically keep training or other projects
  • Open the db-training2 folder as a workspace in your LDS Tech IDE
  • Import the db2 project (and sub-projects)
    • File -> Import
    • Expand Maven
    • Choose "Existing Maven Projects"
    • point it to ...db-training2/db2 as the root directory
    • click finish and wait for it to complete
  • Modify ExampleServiceImpl.findExampleByName(..) to use a BeanPropertySqlParameterSource
    • Open ExampleServiceImpl in the db2-web project
    • In the findExampleByName method change the call to queryForObject(..) replacing the parameterMap parameter with the creation of a new BeanPropertySqlParameterSource passing the example object instance as the constructor parameter
    • the new call could look like the following
	return namedParameterJdbcTemplate.queryForObject(sql, 
		new BeanPropertySqlParameterSource(example),
		ParameterizedBeanPropertyRowMapper.newInstance(Example.class));
  • Test your solution by running the ExampleIT integration test
    • Navigate to ExampleIT.java found in /src/test/java/org/lds/stack/training (shortcut: control-shift-T then type EIT)
    • Right-click on the file and choose "run as" -> testNG test
    • verify that one test ran with zero failures
  • Modify the ExampleServiceImpl.getAllExamples(..) method to use a ParameterizedBeanPropertyRowMapper
    • In the getAllExamples method change the call to query(..) replacing the ExampleRowMapper with the creation of a ParameterizedBeanPropertyRowMapper with a static call to newInstance(..) passing Example.class as the parameter
    • alias the example_name column in the sql query so it matches the property name in Example.java (i.e. "name")
    • the new call including both changes could look like the following
	return jdbcTemplate.query(
		"SELECT id, example_name as name, data FROM example",
		ParameterizedBeanPropertyRowMapper.newInstance(Example.class));
  • Test your solution by running the application
    • start your db2-1.0 (Tomcat...) server and navigate to http://localhost/db2/example
    • add some examples using the form and verify they show up in the list on the right
    • stop your server

Lab 1 Solution


Section 2: More OO: MappingSqlQuery & SQLUpdate

Presentation

Section 3: Transactions

Presentation

Lab 2

  • Add functionality to ExampleServiceImpl.createExample(..) that saves a second Example object. Mark the method as transactional.
    • Annotate the createExample(..) method with @Transactional
    • after the call to execute(..) add code to insert another Example object different from the first one
    • between the now 2 calls to execute(..) add code to throw a RuntimeException
    • the new method could look like the following
	@Override
	@Transactional
	public void createExample(Example example) throws ConstraintViolationException {
		Validate.notNull(example, "Example instance must not be null");
		Set<ConstraintViolation<Example>> violations = validator.validate(example);

		if (!violations.isEmpty()) {
			throw new ConstraintViolationException(new LinkedHashSet<ConstraintViolation<?>>(violations));
		}
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("EXAMPLE_NAME", example.getName());
		parameters.put("DATA", example.getData());
		exampleModelJdbcInsert.execute(parameters);

		if (example.getName().equalsIgnoreCase("Joe")) {
			throw new RuntimeException("Something went wrong");
		}

		parameters.put("EXAMPLE_NAME", example.getName() + "'s Brother");
		exampleModelJdbcInsert.execute(parameters);
	}
  • Test your solution by running the application
    • start your db2-1.0 (Tomcat...) server and navigate to http://localhost/db2/example
    • add a new example using the form with "Joe" as the name - YOU SHOULD GET AN EXCEPTION if you used the provided code
    • verify that neither example objects are created by navigating to the original url
    • add a new example using the form with something other than "Joe" as the name
    • verify that two example objects were created
    • stop your server

Lab 2 Solution



This page was last modified on 8 July 2013, at 09:50.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.