views:

344

answers:

1

Hello.

It would be nice if anybody could show me how to use setRestrictions method whitch belongs to the EntityQuery object from the org.jboss.seam.framework.EntityQuery package.

I have tried to use it this way:

...
import org.jboss.seam.framework.EntityQuery;
import org.jboss.seam.core.Expressions.ValueExpression;

public class LetterList extends EntityQuery<Letter>
{
    public LetterList()
    {
        setEjbql("select letter from Letter letter");
    }

    public void sampleMethod(){              
        List<ValueExpression> restrictions = new ArrayList<ValueExpression>();
        restrictions.add(createValueExpression("letter.id=7"));
        setRestrictions(restrictions);          
    }

}

However, this piece of code throws an exception.

The following lines lead to error too:

String[] RESTRICTIONS = {"letter.id=7"};
setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));

It is also interesting if it is possible to pass any parameters the nice way while using setEjbql. I mean whether it is possible to avoid concatenation.

I have really tried to find some examples of relevant code, but in vain. So, I would highly appreciate a few lines of code that work.

A: 

Try the following example:

@Name("letterList") // (1)
public class LetterList extends EntityQuery<Letter> {

    private Long letterId;

    public LetterList() {
        setEjbql("select letter from Letter letter");
        setRestrictionExpressionStrings(Arrays.asList(new String[] {
            "letter.id = #{letterList.letterId}" // (2)
        }));
    }

    public Long getLetterId() { // (3)
        return letterId;
    }
    public void setLetterId(Long letterId) {
        this.letterId = letterId;
    }
}

Note the three comment locations:

  1. The @Name annotation declares your class as a seam component.
  2. Each restriction expression must contain exactly one expression to be evaluated, no less and no more. It cannot be used with a constant like in your example.
  3. You need to define a getter for each property that you reference in the restriction expressions.
Hosam Aly