spring jpa denormalize one to many
up vote
0
down vote
favorite
I am doing a project and i need to implement some denormalizations. Specifically i need to query Projects by taskCount and/or projectTotalCost.
This is what i have so far.
Seems to me like it works, but i'm not sure anymore. I'm afraid it is prone to race conditions and things like that. Specifically the parts where i update the counters.
Even though i'm using transactions inside my service, i'm not sure about transaction IsolationLevels.
Is this ok ? Do i need to add something else ? Should i use @Version as well (beside transactions) ?
I really need to maintain this denormalization (taskCount, projectTotalCost) and keep it consistent.
@Entity
@Data
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "project_id", referencedColumnName = "id")
private List<Task> tasks;
private int taskCount;
private double projectTotalCost;
public Project() {
this.tasks = new ArrayList<>();
this.taskCount = 0;
this.projectTotalCost = 0;
}
public void addTask(Task t) {
this.tasks.add(t);
updateTaskCount();
updateProjectCost();
}
public void removeTask(Task t) {
this.tasks.remove(t);
updateTaskCount();
updateProjectCost();
}
public void removeWhere(Predicate<Task> spec) {
this.tasks.removeIf(spec);
updateTaskCount();
updateProjectCost();
}
private void updateTaskCount() {
this.taskCount = this.tasks.size();
}
private void updateProjectCost() {
this.projectTotalCost = this.tasks.stream().mapToDouble((Task t) -> t.getCost()).sum();
}
}
@Entity
@Data
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private double cost;
public Task(String name, String description, double cost) {
this.name = name;
this.description = description;
this.cost = cost;
}
public double getCost() {
return this.cost;
}
}
@Service
public class ProjectService {
@Autowired
private ProjectRepository repo;
@Transactional
public Project create(Project p) {
return this.repo.save(p);
}
@Transactional
public Project addTask(long projectId, Task t) {
Project p = this.repo.findById(projectId);
p.addTask(t);
return repo.save(p);
}
@Transactional
public Project removeTaskFromProject(long projectId, long taskId) {
Project p = this.repo.findById(projectId);
p.removeWhere((Task t) -> t.hasId(taskId));
return repo.save(p);
}
}
java sql spring jpa denormalization
add a comment |
up vote
0
down vote
favorite
I am doing a project and i need to implement some denormalizations. Specifically i need to query Projects by taskCount and/or projectTotalCost.
This is what i have so far.
Seems to me like it works, but i'm not sure anymore. I'm afraid it is prone to race conditions and things like that. Specifically the parts where i update the counters.
Even though i'm using transactions inside my service, i'm not sure about transaction IsolationLevels.
Is this ok ? Do i need to add something else ? Should i use @Version as well (beside transactions) ?
I really need to maintain this denormalization (taskCount, projectTotalCost) and keep it consistent.
@Entity
@Data
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "project_id", referencedColumnName = "id")
private List<Task> tasks;
private int taskCount;
private double projectTotalCost;
public Project() {
this.tasks = new ArrayList<>();
this.taskCount = 0;
this.projectTotalCost = 0;
}
public void addTask(Task t) {
this.tasks.add(t);
updateTaskCount();
updateProjectCost();
}
public void removeTask(Task t) {
this.tasks.remove(t);
updateTaskCount();
updateProjectCost();
}
public void removeWhere(Predicate<Task> spec) {
this.tasks.removeIf(spec);
updateTaskCount();
updateProjectCost();
}
private void updateTaskCount() {
this.taskCount = this.tasks.size();
}
private void updateProjectCost() {
this.projectTotalCost = this.tasks.stream().mapToDouble((Task t) -> t.getCost()).sum();
}
}
@Entity
@Data
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private double cost;
public Task(String name, String description, double cost) {
this.name = name;
this.description = description;
this.cost = cost;
}
public double getCost() {
return this.cost;
}
}
@Service
public class ProjectService {
@Autowired
private ProjectRepository repo;
@Transactional
public Project create(Project p) {
return this.repo.save(p);
}
@Transactional
public Project addTask(long projectId, Task t) {
Project p = this.repo.findById(projectId);
p.addTask(t);
return repo.save(p);
}
@Transactional
public Project removeTaskFromProject(long projectId, long taskId) {
Project p = this.repo.findById(projectId);
p.removeWhere((Task t) -> t.hasId(taskId));
return repo.save(p);
}
}
java sql spring jpa denormalization
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am doing a project and i need to implement some denormalizations. Specifically i need to query Projects by taskCount and/or projectTotalCost.
This is what i have so far.
Seems to me like it works, but i'm not sure anymore. I'm afraid it is prone to race conditions and things like that. Specifically the parts where i update the counters.
Even though i'm using transactions inside my service, i'm not sure about transaction IsolationLevels.
Is this ok ? Do i need to add something else ? Should i use @Version as well (beside transactions) ?
I really need to maintain this denormalization (taskCount, projectTotalCost) and keep it consistent.
@Entity
@Data
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "project_id", referencedColumnName = "id")
private List<Task> tasks;
private int taskCount;
private double projectTotalCost;
public Project() {
this.tasks = new ArrayList<>();
this.taskCount = 0;
this.projectTotalCost = 0;
}
public void addTask(Task t) {
this.tasks.add(t);
updateTaskCount();
updateProjectCost();
}
public void removeTask(Task t) {
this.tasks.remove(t);
updateTaskCount();
updateProjectCost();
}
public void removeWhere(Predicate<Task> spec) {
this.tasks.removeIf(spec);
updateTaskCount();
updateProjectCost();
}
private void updateTaskCount() {
this.taskCount = this.tasks.size();
}
private void updateProjectCost() {
this.projectTotalCost = this.tasks.stream().mapToDouble((Task t) -> t.getCost()).sum();
}
}
@Entity
@Data
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private double cost;
public Task(String name, String description, double cost) {
this.name = name;
this.description = description;
this.cost = cost;
}
public double getCost() {
return this.cost;
}
}
@Service
public class ProjectService {
@Autowired
private ProjectRepository repo;
@Transactional
public Project create(Project p) {
return this.repo.save(p);
}
@Transactional
public Project addTask(long projectId, Task t) {
Project p = this.repo.findById(projectId);
p.addTask(t);
return repo.save(p);
}
@Transactional
public Project removeTaskFromProject(long projectId, long taskId) {
Project p = this.repo.findById(projectId);
p.removeWhere((Task t) -> t.hasId(taskId));
return repo.save(p);
}
}
java sql spring jpa denormalization
I am doing a project and i need to implement some denormalizations. Specifically i need to query Projects by taskCount and/or projectTotalCost.
This is what i have so far.
Seems to me like it works, but i'm not sure anymore. I'm afraid it is prone to race conditions and things like that. Specifically the parts where i update the counters.
Even though i'm using transactions inside my service, i'm not sure about transaction IsolationLevels.
Is this ok ? Do i need to add something else ? Should i use @Version as well (beside transactions) ?
I really need to maintain this denormalization (taskCount, projectTotalCost) and keep it consistent.
@Entity
@Data
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "project_id", referencedColumnName = "id")
private List<Task> tasks;
private int taskCount;
private double projectTotalCost;
public Project() {
this.tasks = new ArrayList<>();
this.taskCount = 0;
this.projectTotalCost = 0;
}
public void addTask(Task t) {
this.tasks.add(t);
updateTaskCount();
updateProjectCost();
}
public void removeTask(Task t) {
this.tasks.remove(t);
updateTaskCount();
updateProjectCost();
}
public void removeWhere(Predicate<Task> spec) {
this.tasks.removeIf(spec);
updateTaskCount();
updateProjectCost();
}
private void updateTaskCount() {
this.taskCount = this.tasks.size();
}
private void updateProjectCost() {
this.projectTotalCost = this.tasks.stream().mapToDouble((Task t) -> t.getCost()).sum();
}
}
@Entity
@Data
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private double cost;
public Task(String name, String description, double cost) {
this.name = name;
this.description = description;
this.cost = cost;
}
public double getCost() {
return this.cost;
}
}
@Service
public class ProjectService {
@Autowired
private ProjectRepository repo;
@Transactional
public Project create(Project p) {
return this.repo.save(p);
}
@Transactional
public Project addTask(long projectId, Task t) {
Project p = this.repo.findById(projectId);
p.addTask(t);
return repo.save(p);
}
@Transactional
public Project removeTaskFromProject(long projectId, long taskId) {
Project p = this.repo.findById(projectId);
p.removeWhere((Task t) -> t.hasId(taskId));
return repo.save(p);
}
}
java sql spring jpa denormalization
java sql spring jpa denormalization
asked Nov 5 at 1:59
justatester
11618
11618
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147370%2fspring-jpa-denormalize-one-to-many%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password