我正在开发一个使用Java EE,EJB& S的航空管理Web应用程序。 JPA(EclipseLink)
当我从servlet调用服务层中的方法时,我遇到了麻烦:该方法在没有任何错误的情况下没有达到预期的效果。
方法是AddPassengerToFlight,我试图将乘客添加到我在飞行实体中具有ManyToMany关系的乘客列表。
这是该项目的结构。
这是飞行员和乘客实体,不包括在getter和setter中:
飞行:
public class Flight implements Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private Integer id;
@Enumerated( EnumType.STRING )
private Cities depart;
@Enumerated( EnumType.STRING )
private Cities destination;
private String prix;
@Temporal( TemporalType.TIMESTAMP )
private Date date;
@ManyToMany( cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH } )
@JoinTable( name = "f_p_join", joinColumns = @JoinColumn( name = "flight_fk" ), inverseJoinColumns = @JoinColumn( name = "passenger_fk" ) )
private List<Passenger> passengers;
@OneToOne( )
@JoinColumn( name = "airplane_fk" )
private Plane airplane;
@OneToOne( )
@JoinColumn( name = "pilot_fk" )
private Pilot pilot;
}
乘客:
public class Passenger implements Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private Integer id;
private String nom;
private String prenom;
private String adresse;
private String telephone;
private String email;
@Enumerated( EnumType.STRING )
private FlightClass flightClass;
@ManyToMany( mappedBy = "passengers" )
private List<Flight> flights;
}
PassenerService,我有方法:
public class PassengerService {
private static final String CHAMP_FLIGHT_ID = "flightId";
private static final String CHAMP_PASSENGER_ID = "passengerId";
private String resultat;
private Map<String, String> erreurs = new HashMap<String, String>();
private PassengerDao passengerDao;
private FlightDao flightDao;
private PilotDao pilotDao;
private PlaneDao planeDao;
public Map<String, String> getErreurs() {
return erreurs;
}
public String getResultat() {
return resultat;
}
public PassengerService( PassengerDao passengerDao, FlightDao flightDao, PilotDao pilotDao, PlaneDao planeDao ) {
this.passengerDao = passengerDao;
this.flightDao = flightDao;
this.pilotDao = pilotDao;
this.planeDao = planeDao;
}
public Passenger createPassenger( HttpServletRequest request ) {
//The createPassenger code
}
public List<Passenger> getAllPassengers() {
//The getAllPassengers Code
}
public void AddPassengerToFlight( HttpServletRequest request ) {
FlightService flightService = new FlightService( flightDao, planeDao, pilotDao );
String passengerId = request.getParameter( CHAMP_PASSENGER_ID );
String flightId = request.getParameter( CHAMP_FLIGHT_ID );
Passenger passenger = getPassengerById( passengerId );
Flight flight = flightService.getFlightById( flightId );
List<Passenger> pList = flight.getPassengers();
pList.add( passenger );
flight.setPassengers( pList );
passenger.getFlights().add( flight );
}
public Passenger getPassengerById( String passengerId ) {
Passenger passenger = passengerDao.getPassengerById( passengerId );
return passenger;
}
public void removePassenger( Passenger passenger ) {
//The removePassenger code
}
}
这是servlet AddPassengerToFlight
@WebServlet( "/AddPassengerToFlight" )
public class AddPassengerToFlight extends HttpServlet {
private static final String VUE_REDIRECT = "/airline/PassengerList";
private static final long serialVersionUID = 1L;
@EJB
FlightDao flightDao;
@EJB
PilotDao pilotDao;
@EJB
PlaneDao planeDao;
@EJB
PassengerDao passengerDao;
protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
IOException {
FlightService flightService = new FlightService( flightDao, planeDao, pilotDao );
PassengerService passengerService = new PassengerService( passengerDao, flightDao, pilotDao, planeDao );
List<Flight> flights = flightService.getAllFlights();
List<Passenger> passengers = passengerService.getAllPassengers();
request.setAttribute( "flights", flights );
request.setAttribute( "passengers", passengers );
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
}
protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
IOException {
PassengerService passengerService = new PassengerService( passengerDao, flightDao, pilotDao, planeDao );
passengerService.AddPassengerToFlight( request );
response.sendRedirect( VUE_REDIRECT );
}
}
这是AddPassengerToFlight(JSP文件)
的形式<div class="container">
<h2>Adding Passenger To Flight</h2>
<br/><br/>
<form method="POST" action="AddPassengerToFlight">
<div class="form-group">
<h4>Flight :</h4>
<select name="flightId" class="selectpicker" data-live-search="true" data-width="100%">
<c:forEach items="${flights}" var="flight">
<option value="${flight.id}">
FROM <c:out value="${flight.depart}" /> TO <c:out value="${flight.destination}" /> AT <c:out value="${flight.date}" />
</option>
</c:forEach>
</select>
</div>
<div class="form-group">
<h4>Passenger :</h4>
<select name="passengerId" class="selectpicker" data-live-search="true" data-width="100%">
<c:forEach items="${passengers}" var="passenger">
<option value="${passenger.id}"><c:out value="${passenger.nom}" /></option>
</c:forEach>
</select>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
当我点击重定向到PassengerList的AddPassengerToFlight的按钮提交但两个实体(Flight和Passenger)之间没有任何反应,我没有错误。
有人能弄清楚我在这里做错了什么吗?
答案 0 :(得分:1)
我相信你的问题是你的PassengerService类是一个普通的JAVA类(我解释说,不是EJB服务)...因此,当你在该类中建立Passanger和Flight之间的关联时,没有JTA事务正在运行,您的实体处于分离模式;因此,对它们的任何更改都不会在数据库中保留。
您可以快速尝试以查看我是否正确...使您的服务成为EJB类并通过@EJB
注释注入所需的DAO,从Servlet注入新的EJB然后调用该方法: AddPassengerToFlight( HttpServletRequest request )
...