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?
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);