Hi,

Im not sure I got this right but seems to me you still have to call 
"User.findOne(id)" for each user

actually not, findOne() is called for the currently selected user only - that's 
the advantage.

Regards
Sven


On 11.01.2017 13:16, Zbynek Vavros wrote:
Hi,

Im not sure I got this right but seems to me you still have to call
"User.findOne(id)" for each user
that touches DB (big nope) or create a Map<ID, User> that doesn't sound
great either.

Seems to me the best way is to fetch all users from DB and in
ChoiceRenderer just
iterate and get the correct one by ID...

Zbynek

On Wed, Jan 11, 2017 at 1:01 PM, Sven Meier <s...@meiers.net> wrote:

Hi,

as an alternative you can use a custom model instead:

     IModel<Long> target = ...;

     IModel<User> userModel = new IModel<User>() {
         public void setObject(User user) {
             target.set(user.getId());
         }
         public User getObject() {
             return User.findOne(target.get());
         }
     }

Then your choice can just work on a user:

     DropDownChoice<User> usersDropDown = new DropDownChoice<>("userId",
userModel, User.getUsers());

Both approaches have advantages and disadvantages.

Have fun
Sven



On 11.01.2017 11:38, Zbynek Vavros wrote:

Hey,

I have a DropDownChoice for list of users. I want to display user's name
but bind user's ID.
So far I have come to this (ugly) solution that can surely be improved:

// create model with Long userId...
final DropDownChoice<Long> usersDropDown = new DropDownChoice<>(
                      "userId", Lists.transform(User.getUsers(), new
Function<User, Long>() {
                  @Nullable
                  @Override
                  public Long apply(@Nullable final User user) {
                      return user.getId();
                  }
              }),
                      new ChoiceRenderer<Long>() {
                          @Override
                          public Object getDisplayValue(final Long id) {
                              return User.findOne(id);
                          }
                      }
              );

Thanks for any pointers.
Zbynek


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to