0

The entire first line is red underlined with the message: Incompatible types. Required: Java.lang.Long. Found: void.

Long userId = request.ifPresent(x -> x.getUsers().getId());
Optional<Users> employee = usersRepository.findOne(userId);

My findOne method:

// fetch an individual user by ID
Optional<Users> user = usersRepository.findOne(1L);
log.info("User found with findOne(1L):");
log.info("--------------------------------");
log.info(user.toString());
log.info("");

Users POJO:

    @Entity
    public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="user_id")
    private Long id;
    ...
        public Long getId() {
                return id;
            }
    ...
    }

What's going on here?

Martin Erlic
  • 5,467
  • 22
  • 81
  • 153

2 Answers2

4

As the other posters point out, you're trying to use ifPresent the wrong way.

Assuming that request is an Optional<Request>, you should probably be doing something like:

Optional<Long> userIdMaybe = request.map(r -> r.getUsers().getId());
Optional<Users> employee = userIdMaybe.flatMap(usersRepository::findOne);
Iulian Dogariu
  • 510
  • 4
  • 10
2

Assuming request is an Optional ifPresent consumes a value and returns void.

The compiling is correctly telling you that you cannot assign void to a Long.

henry
  • 5,923
  • 29
  • 46