SeamFramework.orgCommunity Documentation

Chapter 10. Properties

10.1. Working with properties
10.2. Querying for properties
10.3. Property Criteria
10.3.1. AnnotatedPropertyCriteria
10.3.2. NamedPropertyCriteria
10.3.3. TypedPropertyCriteria
10.3.4. Creating a custom property criteria
10.4. Fetching the results

Properties are a convenient way of locating and working with JavaBean properties. They can be used with properties exposed via a getter/setter method, or directly via the field of a bean, providing a uniform interface that allows you all properties in the same way.

Property queries allow you to interrogate a class for properties which match certain criteria.

The Property<V> interface declares a number of methods for interacting with bean properties. You can use these methods to read or set the property value, and read the property type information. Properties may be readonly.

Given a class with two properties, personName and postcode:'

class Person {

   PersonName personName;
   Address address;
   void setPostcode(String postcode) {
   String getPostcode() {
      return address.getPostcode();

You can create two properties:

   Property<PersonName> personNameProperty = Properties.createProperty(Person.class.getField("personName");

   Property<String> postcodeProperty = Properties.createProperty(Person.class.getMethod("getPostcode"));

To create a property query, use the PropertyQueries class to create a new PropertyQuery instance:

   PropertyQuery<?> query = PropertyQueries.createQuery(Foo.class);

If you know the type of the property that you are querying for, you can specify it via a type parameter:

   PropertyQuery<String> query = PropertyQueries.<String>createQuery(identityClass);

Once you have created the PropertyQuery instance, you can add search criteria. Weld Extensions provides three built-in criteria types, and it is very easy to add your own. A criteria is added to a query via the addCriteria() method. This method returns an instance of the PropertyQuery, so multiple addCriteria() invocations can be stacked.

After creating the PropertyQuery and setting the criteria, the query can be executed by invoking either the getResultList() or getFirstResult() methods. The getResultList() method returns a List of Property objects, one for each matching property found that matches all the specified criteria:

   List<Property<String>> results = PropertyQueries.<String>createQuery(Foo.class)

If no matching properties are found, getResultList() will return an empty List. If you know that the query will return exactly one result, you can use the getFirstResult() method instead:

   Property<String> result = PropertyQueries.<String>createQuery(Foo.class)

If no properties are found, then getFirstResult() will return null. Alternatively, if more than one result is found, then getFirstResult() will return the first property found.