Implemento el metodo nuevoViaje() y la nueva estructura de ReservaRepository

parent 2241a322
......@@ -11,7 +11,7 @@ import com.carpooling.carpoolingaoraha.entidades.*;
import jakarta.persistence.*;
@Entity
@Table (name = "Reservas")
@Table (name = "reservas")
public class Reserva {
@Transient
private static int incrementaReserva = 0;
......@@ -47,6 +47,10 @@ public class Reserva {
this.pasajeros = new ArrayList<String>();
}
public void nuevoViaje(Viaje v)
{
this.viaje = v;
}
public void anadirPasajeros(String DNI, int idReserva){
for(int i=0; i<this.sistema.getReservas().size(); i++){
if(idReserva == this.sistema.getReservas().get(i).getIdReserva()){
......
package com.carpooling.carpoolingaoraha.repositorios;
import com.carpooling.carpoolingaoraha.entidades.Reserva;
import com.carpooling.carpoolingaoraha.entidades.Viaje;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
public interface ReservaRepository extends JpaRepository<Reserva, Integer> {
Reserva findByIdReserva(Integer idReserva);
import java.util.Optional;
@Repository
@Transactional(propagation = Propagation.REQUIRED)
public class ReservaRepository {
@PersistenceContext
EntityManager em;
@Cacheable("reservas")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public Optional<Reserva> buscar(String idReserva)
{
return Optional.ofNullable(em.find(Reserva.class,idReserva));
}
@CacheEvict(value="reservas", key = "#reserva.idReserva")
public void guardar(Reserva reserva)
{
em.persist(reserva);
}
@CacheEvict(value="reservas", key = "#reserva.idReserva")
public void nuevoViaje(Reserva reserva, Viaje viaje){
em.persist(viaje);
reserva = em.merge(reserva);
reserva.nuevoViaje(viaje);
}
@CacheEvict(value="reservas", key = "#reserva.idReserva")
public void actualizar(Reserva reserva)
{
em.merge(reserva);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment