A life committed to learning.

Look for the obvious first…

As I like to experiment cool web development frameworks, I decided to make a quick tour on Play! Framework. It was queued 🙂

So, here I was trying the persistence from the framework, that is basically JPA 2.0 with Hibernate as the persistence provider. I was also very interested in knowing what the framework could provide me for data validation, you know, that basic stuff that a framework should give us for free before it can be called wed framework.

I started with a very simple JPA entity like this one:

@Entity
public class Partner extends Model {

 @Required
 @MaxSize(60)
 @MinSize(4)
 private String name;

 @Required
 @Enumerated(EnumType.STRING)
 private BusinessType businessType;

 @Required
 @Embedded
 @Valid
 private Address address;
 //The rest of the code
}

And the embedded entity Address was defined like this:

@Embeddable
public class Address{

	@Required
	@MaxSize(100)
	@MinSize(5)
	private String addressLineOne;

	@MaxSize(100)
	@MinSize(4)
	private String addressLineTwo;

	@Required
	@MaxSize(60)
	@MinSize(8)
	private String postalCode;

	@Required
	@MaxSize(50)
	@MinSize(4)
	private String city;

	@Required
	@MaxSize(50)
	@MinSize(2)
	private String country;
        //the rest of the code, getters, setters, constructors, etc
}

The annotations @Required, @MaxSize and @MinSize are used by the validation plugin and is easy to understand what they do.

The @Valid annotation in the address field from Partner entity just tell Play!: “go into that object and validate it according to the annotations you find in the fields.” So everything should validates fine.

At my first unit test to create and retrieve a Partner entity I stuck in a strange behavior that looked like the validation of embedded entities was not working as expected.

Any values I provided in an Address instance always failed against the validation. I was getting validation error messages for all fields in the Address entity:

models.Address.addressLineOne cannot have less than 5 elements
models.Address.addressLineTwo cannot have less than 4 elements
models.Address.postalCode cannot have less than 8 elements
models.Address.city cannot have less than 4 elements
models.Address.country cannot have less than 2 elements

yes, I doubled checked that the strings I provided to the fields were according to validation…

Given that, I found myself  debugging the framework and looking for the problem… debugging is cool 🙂 After three iterations of debug, I could not found why the execution was not going through the code that should validate the minimum size of a field…. WTF

So, I restarted myself and looked at the Address entity again and found :

import net.sf.oval.constraint.MinSize;

where should be:

import play.data.validation.MinSize;

Play! Framework is using OVal in the validation plugin…

Wasn’t that cool?

jpereira

http://jpereira.eu

View more posts from this author

Leave a Reply

Your email address will not be published. Required fields are marked *