Would highly appreciate any suggestions. Am a newbie to Java/Spring and especially JdbcTemplate.
(also my first question on stackoverflow, hooray!)
I have a simple User.class:
@Data
@Slf4j
@Builder
public class User {
protected long id;
@NotBlank
@Email(message = "email does not correspond to format: xxxxx@yyyy.hh.")
protected String email;
@NotBlank(message = "Login may not be made of blanks.")
protected String login;
protected String name;
@Past(message = "Birthday may not be in the future.")
protected LocalDate birthday;
protected final Set<Long> friendIds = new HashSet<>();
This class is represented by two tables in my H2 database:
CREATE TABLE UserTable (
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
email varchar (100) NOT NULL,
login varchar (70) NOT NULL,
name varchar (50) NOT NULL,
birthday date
);
CREATE TABLE Friendship (
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
userId integer references UserTable (id),
userFriendId integer references UserTable (id),
status boolean default false
);
I have also implemented a UserRowMapper:
private class UserRowMapper implements RowMapper<User> {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
return new User(
rs.getInt("id"),
rs.getString("email"),
rs.getString("login"),
rs.getString("name"),
rs.getDate("birthday").toLocalDate(),
);
}
}
However I am not sure what would the best way to map the Set parameter of User.class be?
Is the only way to create another implementation of RowMapper just for the Set?
Would highly appreciate your suggestions.
Have tried to make a separate class e.g. Friend, and a RowMapper for this class, but I think there should be some simpler way.