Friday, April 29th, 2011
Background
For years I’ve bemoaned Oracle’s lack of an auto-number/identity type column (think MySQL AUTO_INCREMENT). Obviously you can create sequences and triggers to produce the same effect but it always seemed like a bit of a palaver to me.
So, I had a smile on my when I discovered the delight that is SYS_GUID().
Taken from the Oracle documentation:
SYS_GUID generates and returns a globally unique identifier (RAW value) made up of 16 bytes. On most platforms, the generated identifier consists of a host identifier, a process or thread identifier of the process or thread invoking the function, and a nonrepeating value (sequence of bytes) for that process or thread.
Example
select SYS_GUID() from dual
SYS_GUID()
= ================================
1 A20D42BA1F165DB5E04400144FB99F0A
SYS_GUID() as a default
Because SYS_GUID() “returns a globally unique identifier” we can use it as a default value in a Primary Key column of a table.
create table products(
product_id raw(32) default sys_guid() not null primary key
............
);
Tags: oracle, sql
Posted in Development | No Comments »
Friday, April 22nd, 2011
Motivation
I added validation, via annotations, to a Spring “Model” bean. I needed someway to Unit Test this validation, without running the container and without initialising the Spring context.
The bean (simplified)
package foo.bar;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
public final class ProductModel {
@NotNull
@Size(max=100)
@Pattern(regexp="[^\n^\t^\r]+", message="Long Name must not contain New Lines, Carriage Returns or Tabs")
private String longName;
@Size(max=20)
private String shortName;
// rest snipped for brevity
}
The Unit Test
package foo.bar;
import java.util.Set;
import javax.validation.ConstraintViolation;
import junit.framework.Assert;
import org.hibernate.validator.HibernateValidator;
import org.junit.Before;
import org.junit.Test;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
public class Temp {
private LocalValidatorFactoryBean localValidatorFactory;
@Before
public void setup() {
localValidatorFactory = new LocalValidatorFactoryBean();
localValidatorFactory.setProviderClass(HibernateValidator.class);
localValidatorFactory.afterPropertiesSet();
}
@Test
public void testLongNameWithInvalidCharCausesValidationError() {
final ProductModel productModel = new ProductModel();
productModel.setLongName("A long name with\t a Tab character");
Set<ConstraintViolation<ProductModel>> constraintViolations = localValidatorFactory.validate(productModel);
Assert.assertTrue("Expected validation error not found", constraintViolations.size() == 1);
}
}
More
It actually took me a few hours to work the above test out (simple as it is). If I hadn’t stumbled upon these Spring Unit Tests, I might never have got it.
Tags: annotations, java, junit, spring, testing
Posted in Spring | No Comments »
Monday, April 18th, 2011
Background
So, I’m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration. As I was trawling through the source code I noticed “some wierd” syntax: “/{xxxx}” (which of course I had to look up!).
Most of the following is based upon the excellent Spring Documentation. I’ve lifted and rewritten the parts I require to serve as a condensed “aide-mémoire”.
Description
A URI Template is a URI-like string, containing one or more variable names. When you substitute values for these variables, the template becomes a URI.
Read More »
Tags: annotations, java, spring
Posted in Spring | No Comments »
Monday, April 18th, 2011
Background
So, I’m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration files. As I was trawling through the source code I noticed the “@ModelAttribute” annotation (which of course I had to look up!).
If the above background seems familiar that’s because it’s the same as for a previous Spring Annotation post I made (ed: this is in danger of turning into a mini-series)
Description
Most of the following is based upon the excellent Spring Documentation. I’ve simply lifted and rewritten the parts I require to serve as an “aide-mémoire”.
The @ModelAttribute annotation serves two purposes depending on how it is used:
Read More »
Tags: annotations, java, spring
Posted in Spring | 3 Comments »
Friday, April 15th, 2011
Background
So, I’m working on an existing Spring 3.x web project. This project heavily favours the use of annotations over XML configuration files. As I was trawling through the source code I noticed the “@PathVariable” annotation (which of course I had to look up!).
Example
Imagine you’d like to create a controller that would allow users to see view products by using a URL such as http://foo.bar.com/productDetails/someProduct
e.g.
http://foo.bar.com/productDetails/widgetA - display details for widgetA
http://foo.bar.com/productDetails/widgetB - display details for widgetB
The @PathVariable annotation makes this easy…
package foo.bar;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@Controller
public class ShowProductController {
@RequestMapping("/productDetails/{productName}") // Handle any request of the form "/productDetails/XXXXX"
public String showProduct(Model model, @PathVariable("productName") String productName) {
model.addAttribute("productName", productName);
return "showProduct"; // viewname
}
}
What just happened?
Note the {productName} in the @RequestMapping – this is the path variable. The @PathVariable annotation on the next line captures the value and binds it to the method argument
Tags: annotations, java, spring
Posted in Development, Examples | No Comments »