Permalink
Browse files

Database seeder

the line 'spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true' have to be added to application.properties to ensure accessing lazy fetch data after hibernate session done
  • Loading branch information...
dgkncelik committed May 21, 2018
1 parent 7e74024 commit 0961ca8fc09c1cd361fc72c5603088703ce0142f
@@ -69,12 +69,12 @@ public ErrorResponse handleIllegalArgument(IllegalArgumentException e){
* @param e caught exception
* @return ErrorResponse with exception message with related HTTP status
*/
@ExceptionHandler(Exception.class)
/* @ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleException(Exception e){
System.out.printf(e.getMessage());
return new ErrorResponse(1,e.getMessage(), null);
}
}*/
}
@@ -13,6 +13,7 @@
import edu.itu.cavabunga.core.service.IcalService;
import edu.itu.cavabunga.core.service.ParticipantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -34,9 +35,10 @@
@GetMapping
@Transactional
public String seedDataBase(){
calendarManagerService.addParticipant(participantService.createParticipant("testuser", ParticipantType.User));
calendarManagerService.addParticipant(participantService.createParticipant("testgroup", ParticipantType.Group));
participantService.saveParticipant(participantService.createParticipant("testuser", ParticipantType.User));
participantService.saveParticipant(participantService.createParticipant("testgroup", ParticipantType.Group));
Component calendar = icalService.createComponent(ComponentType.Calendar);
@@ -52,6 +54,7 @@ public String seedDataBase(){
calendar.addProperty(version);
calendar.addProperty(calscale);
calendar.addProperty(method);
calendar.setOwner(calendarManagerService.getParticipantByUserName("testuser"));
@@ -49,7 +49,7 @@
private String value;
@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "property_id")
@JsonBackReference
private Property property;
@@ -1,3 +1,4 @@
package edu.itu.cavabunga.core.entity;
import com.fasterxml.jackson.annotation.*;
@@ -50,4 +51,4 @@ public Participant(){
public void validate(){
}
}
}
@@ -78,12 +78,12 @@
private String value;
@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "component_id")
@JsonBackReference
private Component component;
@OneToMany(mappedBy = "property", cascade = CascadeType.ALL)
@OneToMany(mappedBy = "property", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference
private List<Parameter> parameters = new ArrayList<>();
@@ -16,6 +16,7 @@
import edu.itu.cavabunga.exception.Validation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.validation.Valid;
@@ -93,6 +94,7 @@ public Component createComponentForParticipant(ComponentType componentType, Part
* {@inheritDoc}
*/
@Override
@Transactional
public void saveComponent(Component component){
try {
component.validate();
@@ -6,6 +6,7 @@
import edu.itu.cavabunga.core.repository.ParticipantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@@ -50,6 +51,7 @@ public Participant createParticipant(String userName, ParticipantType participan
* {@inheritDoc}
*/
@Override
@Transactional
public void saveParticipant(Participant participant){
participantRepository.save(participant);
}
@@ -2,3 +2,4 @@ spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://127.0.0.1/test
spring.datasource.username=cavabunga
spring.datasource.password=password
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

0 comments on commit 0961ca8

Please sign in to comment.