Chapter 6. Flamingo Dynamic Persistent Methods

Flamingo provides a very powerful capability for operations on entities directly from the Flex user interface. General thinking is that access from user interface must be architected with the layer of facades (DAOs), but in some cases these facades introduce unnecessary complexity, and in other cases, having a framework providing these facades in a uniform matter is just more convenient. Flamingo provides dynamic methods to operate on entities directly without facades. Most of queries performed on projects are not that complex. These simple queries can be completely replaced by Flamingo dynamic persistent methods, as almost no effort is required to use them. These methods are split into two main categories:

6.1. Dynamic Finders

Dynamic finders are a group of methods which have been designed in order to create queries on the fly without having to implement any additional logic on server or client side. These queries have the following syntax:

findBy[property]Operator(parameter(s))

where

  • Property - capitalized name of a property

  • Operator – operator supported by Flamingo

Example 6.1. Code Example

If there is the following entity:

@Entity
public class Person implements Serializable {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   	
} 

then the following usage of dynamic finders is possible:

... 
<mx:RemoteObject id="person" destination="Person" result="resultHandler(event)"/>
...
private function resultHandler(event:ResultEvent):void
{
    entities = event.result as ArrayCollection;
}
...
person.findAll();
person.findByNameLike("John%");
person.findByNameNotEqual("John Smith");

where Name is a property of bean Person, and Like and NotEqual are operators used in this examples.


Currently the following operators are supported:

Table 6.1. 

Supported Operators
Equal
NotEqual
Like
And
Or
Not
IsNull
IsNotNull
LessThan
LessThanEquals
GreaterThan
GreaterThanEquals
Between

6.1.1. Query by Relationship

Flamingo also supports dynamic querying by relationship. You can easily search for an entity by its relationship to another entities. Just include relationship into the search operator, for example: suppose you have 2 entities with "one-to-many" relationship:

Example 6.2. Code Example


@Entity
public class Person implements Serializable {
    ....

    private Set<Book> books;
    
    @OneToMany(mappedBy = "person", fetch = FetchType.EAGER)
    public Set<Book> getBooks() {
        return books;
    }
}


@Entity
public class Book implements Serializable {
             private Person person;

    @ManyToOne(fetch = FetchType.EAGER)
    public Person getPerson() {
        return person;
    }
}

Now you can call objects by relationship. Declare this remote object:


<mx:RemoteObject id="book" destination="Book"/>

To get a list of Books owned by currentPerson just request method book.findByPerson(currentPerson).


6.1.2. Sorting

You can also use sorting when sending a query to the database. In order to do that specify additional parameters in the find... method.

Example 6.3. Code Example

Here is an example of sorting for one column:


person.findAll({sortBy:'name', order:'desc'});

It shows all objects person sorted by their names in the descending order.

 

Here is an example of sorting for several columns at once:


person.findAll({sortBy:'id, name', order:'desc'});

It shows fields sorted by id in the descending order and then by name in the ascending order. This happens as ascending order is set by default and will be used if not specified separately.


6.2. Dynamic Updaters

Flamingo provides two dynamic updaters:

  • save

  • remove

The save method will persist all properties of a remote object mapped to the database, while the remove method will delete this entity from the database. Calling these methods is equally easy:


<mx:RemoteObject id="person" destination="Person" result="resultHandler(event)"/>

...

var modifiedPerson:Person;

modifiedPerson = ObjectUtil.copy(originalPerson) as Person;

modifiedPerson.name = "New Name";

person.save(modifiedPerson);

...
person.remove(modifiedPerson);