Used NamedQueries in the save method
This commit is contained in:
parent
5cbece42bb
commit
73c0945c4a
@ -72,24 +72,22 @@ public class MoviesService {
|
||||
|
||||
for (Movies movie : movies) {
|
||||
|
||||
// TODO maybe get just the id and improve checking in for loop?
|
||||
// Get all actors and studios from the database
|
||||
List<Actors> allActors = findAllActors();
|
||||
List<Studios> allStudios = findAllStudios();
|
||||
|
||||
try {
|
||||
// Get studio for the movie from the database
|
||||
Studios movieStudio = movie.getStudio();
|
||||
LOGGER.info("Get a studio" + movieStudio);
|
||||
List<Studios> studioListResult = em.createNamedQuery("Studios.getIdByProperties", Studios.class)
|
||||
.setParameter("name", movieStudio.getName())
|
||||
.setParameter("countrycode", movieStudio.getCountrycode())
|
||||
.setParameter("postcode", movieStudio.getPostcode())
|
||||
.getResultList();
|
||||
|
||||
// If the studio is missing in the database, roll back only
|
||||
for (Studios studio : allStudios) {
|
||||
if (studio.getName().equals(movieStudio.getName()) & studio.getCountrycode().equals(movieStudio.getCountrycode())
|
||||
& studio.getPostcode().equals(movieStudio.getPostcode())) {
|
||||
// Set id
|
||||
movieStudio.setId(studio.getId());
|
||||
}
|
||||
if (!studioListResult.isEmpty() && studioListResult.size() > 0) {
|
||||
Long actorId = studioListResult.get(0).getId();
|
||||
movieStudio.setId(actorId);
|
||||
}
|
||||
if (movieStudio.getId() == null) {
|
||||
else
|
||||
{
|
||||
LOGGER.info("Rollback! Movie studio: " + movieStudio);
|
||||
context.setRollbackOnly();
|
||||
return;
|
||||
@ -99,27 +97,22 @@ public class MoviesService {
|
||||
// Get actor for the movie from the database
|
||||
for (Actors movieActor : movie.getActors()) {
|
||||
LOGGER.info("Get an actor " + movieActor);
|
||||
List actorListResult = em.createNamedQuery("Actors.getIdByProperties", Actors.class)
|
||||
List<Actors> actorListResult = em.createNamedQuery("Actors.getIdByProperties", Actors.class)
|
||||
.setParameter("firstname", movieActor.getFirstname())
|
||||
.setParameter("lastname", movieActor.getLastname())
|
||||
.setParameter("sex", movieActor.getSex())
|
||||
//.setParameter("birthdate", movieActor.getBirthdate())
|
||||
.getResultList();
|
||||
|
||||
// If any actor is missing in the database, roll back only
|
||||
for (Actors actor : allActors) {
|
||||
if (actor.getFirstname().equals(movieActor.getFirstname())
|
||||
& actor.getLastname().equals(movieActor.getLastname())
|
||||
& actor.getSex().equals(movieActor.getSex())) {
|
||||
// & actor.getBirthdate().equals(movieActor.getBirthdate())) {
|
||||
// Set id
|
||||
movieActor.setId(actor.getId());
|
||||
if (!actorListResult.isEmpty() && actorListResult.size() > 0) {
|
||||
Long actorId = actorListResult.get(0).getId();
|
||||
movieActor.setId(actorId);
|
||||
}
|
||||
if (movieActor.getId() == null) {
|
||||
LOGGER.info("Rollback! Movie actor: " + movieActor);
|
||||
context.setRollbackOnly();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.info("Rollback! Movie actor: " + movieActor);
|
||||
context.setRollbackOnly();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user