I'm trying out Spring Data JDBC, and I think that immutable entities are the key feature here.
I want to model a one-to-many relationship between Team and Player and have these entities immutable. What I have working right now is:
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Team {
@Id
@Wither
private Integer id;
private String name;
private List<Player> players;
}
@Value
public class Player {
@Id
@Wither
Integer id;
String name;
public static Player of(String name) {
return new Player(null, name);
}
}
I managed to get a OneToMany relationship working properly for mutable classes. I managed to make it work for immutable Player. However, I can't get Team mapping to work without @NoArgsConstructor
, or with @Value
instead of what I have right now. I get the weird message as if Spring Data JDBC started seeing different mapping when this constructor is missing:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "players_id" not found [42122-199]
If anybody tries to recreate that scenario, here's my h2 schema:
create table team (id serial primary key, name varchar);
create table player (id serial primary key, name varchar, team int references team(id), team_key int);
And my Spock test:
@SpringBootTest
class TeamRepositoryTest extends Specification {
@Autowired
TeamRepository teamRepository
def "Context loads"() {
given:
def rick = Player.of("Rick")
def morty = Player.of("Morty")
def team = new Team(null, "Rick and Morty", Arrays.asList(rick, morty))
when:
def savedTeam = teamRepository.save(team)
then:
def savedTeamOpt = teamRepository.findById(savedTeam.id)
Team retrievedTeam = savedTeamOpt.get()
retrievedTeam.id == savedTeam.id
retrievedTeam.name == "Rick and Morty"
retrievedTeam.players.size() == 2
}
}
You hit a bug in Spring Data JDBC: https://jira.spring.io/browse/DATAJDBC-127
Currently entities or collections of entities don't work as constructor arguments. Create a constructor without these arguments and add setters or if you want to stay immutable withers for them.