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) {
|
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 {
|
try {
|
||||||
// Get studio for the movie from the database
|
// Get studio for the movie from the database
|
||||||
Studios movieStudio = movie.getStudio();
|
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
|
if (!studioListResult.isEmpty() && studioListResult.size() > 0) {
|
||||||
for (Studios studio : allStudios) {
|
Long actorId = studioListResult.get(0).getId();
|
||||||
if (studio.getName().equals(movieStudio.getName()) & studio.getCountrycode().equals(movieStudio.getCountrycode())
|
movieStudio.setId(actorId);
|
||||||
& studio.getPostcode().equals(movieStudio.getPostcode())) {
|
|
||||||
// Set id
|
|
||||||
movieStudio.setId(studio.getId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (movieStudio.getId() == null) {
|
else
|
||||||
|
{
|
||||||
LOGGER.info("Rollback! Movie studio: " + movieStudio);
|
LOGGER.info("Rollback! Movie studio: " + movieStudio);
|
||||||
context.setRollbackOnly();
|
context.setRollbackOnly();
|
||||||
return;
|
return;
|
||||||
@ -99,27 +97,22 @@ public class MoviesService {
|
|||||||
// Get actor for the movie from the database
|
// Get actor for the movie from the database
|
||||||
for (Actors movieActor : movie.getActors()) {
|
for (Actors movieActor : movie.getActors()) {
|
||||||
LOGGER.info("Get an actor " + movieActor);
|
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("firstname", movieActor.getFirstname())
|
||||||
.setParameter("lastname", movieActor.getLastname())
|
.setParameter("lastname", movieActor.getLastname())
|
||||||
.setParameter("sex", movieActor.getSex())
|
.setParameter("sex", movieActor.getSex())
|
||||||
//.setParameter("birthdate", movieActor.getBirthdate())
|
//.setParameter("birthdate", movieActor.getBirthdate())
|
||||||
.getResultList();
|
.getResultList();
|
||||||
|
|
||||||
// If any actor is missing in the database, roll back only
|
if (!actorListResult.isEmpty() && actorListResult.size() > 0) {
|
||||||
for (Actors actor : allActors) {
|
Long actorId = actorListResult.get(0).getId();
|
||||||
if (actor.getFirstname().equals(movieActor.getFirstname())
|
movieActor.setId(actorId);
|
||||||
& actor.getLastname().equals(movieActor.getLastname())
|
|
||||||
& actor.getSex().equals(movieActor.getSex())) {
|
|
||||||
// & actor.getBirthdate().equals(movieActor.getBirthdate())) {
|
|
||||||
// Set id
|
|
||||||
movieActor.setId(actor.getId());
|
|
||||||
}
|
}
|
||||||
if (movieActor.getId() == null) {
|
else
|
||||||
LOGGER.info("Rollback! Movie actor: " + movieActor);
|
{
|
||||||
context.setRollbackOnly();
|
LOGGER.info("Rollback! Movie actor: " + movieActor);
|
||||||
return;
|
context.setRollbackOnly();
|
||||||
}
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user