I'm trying spring data JDBC (1.1.0.M3). I have a Formateur (Teacher) class which contains an email attribute (class Email). Email is an immutable class. When I retrieve all the teachers from the database, I have the following
exception: java.lang.UnsupportedOperationException: Cannot set immutable property fr.yestech.data.jdbc.domain.Email.email
It seems that spring data JDBC doesn't work well with final attributes!
public class Formateur {
private @Id Long id;
private String nom;
@Embedded
private Email email;
...
}
Email class (Immutable class so no setters !)
public final class Email {
private final String email;
private static final Pattern EMAIL_PATTERN = Pattern.compile("^[_A-Za-z]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
private Email(String email) {
Objects.requireNonNull(email, "Adresse email doit être non null!");
if (!EMAIL_PATTERN.matcher(email).matches())
throw new IllegalArgumentException("L'adresse email est
invalide !");
this.email = email;
}
public static Email of(String email) {
return new Email(email);
}
...
}
Application
@SpringBootApplication
public class SpringJdbcApplication {
public static void main(String[] args) {
SpringApplication.run(SpringJdbcApplication.class, args);
}
@Bean
ApplicationRunner applicationRunner(FormateurRepository formateurRepository) {
return args -> {
Formateur teacher = Formateur.of("YesTech", Email.of("yestech@email.com"), Adresse.of("Goux"));
formateurRepository.save(teacher);
formateurRepository.findAll().forEach(System.out::println);
};
}
}
This is indeed a bug. You can work around it by adding a "wither"
private Email withEmail(String email) {
return new withEmail(email);
}
It may even be private.
I created an issue for this, so it gets fixed soonish.