Goals
↓
Explore options
↓
Compare
↓
Choose the best
@Entity
class OrganizationOrm {
@Id
private long id;
@OneToMany
private List<TeamOrm> teams;
public void createTeamAndMember() {
val team = new TeamOrm();
val member = new MemberOrm();
team.add(member);
this.teams.add(team);
}
}
ps: I never used an ORM, no guarantees it works
@Entity
class TeamOrm {
@Id
private long id;
@ManyToMany
private List<MemberOrm> members;
}
@Entity
class MemberOrm {
@Id
private long id;
}
class OrganizationRepository {
private OrmRepository orm;
public void save(OrganizationOrm organization) {
this.orm.save(organization);
}
}
class OrganizationController {
@PutMapping("/organization/{orgaId}/team")
public Response endpoint(String orgaId) {
OrganizationOrm organization = repo.find(orgaId);
organization.createTeamAndMember();
repo.save(organization);
return Response.ok();
}
}
@Entity
class OrganizationOrm {
@Id
private long id;
@OneToMany
private List<TeamOrm> teams;
public void createTeamAndMember() {
val team = new TeamOrm();
val member = new MemberOrm();
team.add(member);
this.teams.add(team);
}
}
(or else known as domain)
There can not be more than 10 teams per organization
@Entity
class OrganizationOrm {
// ...
public void createTeamAndMember() {
if (teams.size() > 10) {
return;
}
val team = new TeamOrm();
val member = new MemberOrm();
team.add(member);
this.teams.add(team);
}
}
De l'explicabilité des systèmes : les enjeux de l'explication des décisions automatisées
class OrganizationController {
@PutMapping("/organization/{orgaId}/team")
public Response endpoint(String orgaId) {
OrganizationOrm organization = repo.find(orgaId);
organization.createTeamAndMember();
repo.save(organization);
if (/* some condition */) {
return Response.error();
}
return Response.ok();
}
}
public boolean createTeamAndMember()
public void createTeamAndMember() throws TooManyTeamsException
public Optional<TooManyTeams> createTeamAndMember()
It's the story of 10000 users editing the same organization but not only!
Use SELECT FOR UPDATE
to lock the aggregate's row
The lock is freed on COMMIT
or ROLLBACK
@Entity
class OrganizationOrm {
// ...
private int version;
}
class OrganizationRepository {
private OrmRepository orm;
public void save(OrganizationOrm organization) {
this.orm.save(organization);
this.orm.execute("update organisations " +
" set version = $1 " +
" where organisations.id = $2 " +
" and organisations.version = $3",
organisation.getVersion() + 1,
organisation.getId(),
organisation.getVersion());
}
}
class Organization {
private OrganizationId id;
private List<Team> teams;
public OrganizationEvent createTeamAndMember() {
if (teams.size() > 10) {
return new TooManyTeams();
} else {
return new AddNewTeamAndMember(this.id);
}
}
}
class OrganizationController {
@PutMapping("/organization/{orgaId}/team")
public Response endpoint(String orgaId) {
Organization organization = repo.find(orgaId);
val event = organization.createTeamAndMember();
repo.save(event);
return toResponse(event);
}
}
class OrganizationRepository {
private OrmRepository orm;
public void save(OrganizationEvent event) {
if (event instanceOf AddNewTeamAndMember) {
OrganizationOrm orga = this.orm.find(event.id);
val team = new TeamOrm();
val member = new MemberOrm();
team.add(member);
orga.getTeams.add(team);
this.orm.save(orga);
}
}
}
class OrganizationRepository {
private OrmRepository orm;
public void save(OrganizationEvent event) {
if (event instanceOf AddNewTeamAndMember) {
long teamId = this.orm.get(
"insert into teams (organization_id) values ($1) " +
" returning id",
event.id
);
this.orm.execute(
"insert into members (team_id) values ($1)",
teamId
);
}
class OrganizationRepository {
private OrmRepository orm;
private EventNotifier notifier;
public void save(OrganizationEvent event) {
this.persist(event);
this.notifier.propagate(event);
}