Hallo,
ich habe ein Problem mit einer Liste in einer Entity. Und zwar hält diese einfach keine Objekte, wenn ich mit add() welche hinzufüge.
Ich habe also eine Entity Airline die so aussieht:
Ein Session Interface:
Und das Session EJB:
Im Client mache ich dann folgendes:
Ich verstehe einfach nicht warum in der Liste kein Objekt ist. In der EJB-Klasse wird das FLugzeug das ich hinzufüge abgelegt und ist auch in der Liste, nur im CLient ist es nicht mehr da...
ich hoffe man versteht meine Frage und jemand hat lust es sich mal anzuschauen. Falls mehr code benötigt wird kann ich diesen natrlich noch posten.
ich habe ein Problem mit einer Liste in einer Entity. Und zwar hält diese einfach keine Objekte, wenn ich mit add() welche hinzufüge.
Ich habe also eine Entity Airline die so aussieht:
Java:
@Entity(name = "airline")
public class Airline implements Serializable
{
private static final long serialVersionUID = 1L;
private List<Plane> planeList;
private String name;
private int id;
public Airline() {}
/**
* Constructor
*
* @param name Name of the airline
*/
public Airline(String name)
{
this.name = name;
planeList = new ArrayList<Plane>();
}
//...
@OneToMany(mappedBy="airline", fetch=FetchType.EAGER)
public List<Plane> getPlaneList()
{
return planeList;
}
public void setPlaneList(List<Plane> planeList)
{
this.planeList = planeList;
}
//....
}
Ein Session Interface:
Java:
@Remote
public interface AirlineSession
{
public int createEntity (String name);
public Airline getEntity (int id);
public void updateEntity (int id, String name);
public void addPlane(Plane plane, int id);
public void removeEntity (int id);
}
Und das Session EJB:
Java:
@Stateless
public class AirlineSessionEJB implements AirlineSession
{
@javax.persistence.PersistenceContext (unitName = "airport")
private EntityManager em;
//...
@Override
public int createEntity(String name)
{
Airline airline = new Airline(name);
em.persist (airline);
System.out.println("Entity Airline.id=" + airline.getId() +
" " + name + " created.");
return airline.getId();
}
@Override
public void addPlane(Plane plane, int id)
{
Airline airline = em.find(Airline.class, id);
if (airline != null)
{
List<Plane> tmp = airline.getPlaneList();
tmp.add(plane);
airline.setPlaneList(tmp);
em.persist(airline);
System.out.println("Plane.id " + plane.getId() + " to Airline.id="
+ id + " added." + airline.getPlaneList());
}
}
//...
}
Im Client mache ich dann folgendes:
Java:
public class PlaneClient
{
public PlaneClient()
{
try
{
InitialContext ctx = new InitialContext ();
PlaneSession smp = (PlaneSession)ctx.lookup ("Airport/PlaneSessionEJB/remote");
PlaneTypeSession pts = (PlaneTypeSession) ctx.lookup("Airport/PlaneTypeSessionEJB/remote");
AirlineSession airSession = (AirlineSession) ctx.lookup("Airport/AirlineSessionEJB/remote");
int x = pts.createEntity("Airbus");
int i = smp.createEntity("Plane1", x);
int airlineId = airSession.createEntity("Lufthansa2");
System.out.println("ID="+i+" "+smp.getEntity(i).getDescription());
System.out.println (smp.getEntity(i).getPlaneType().getName());
// Hier wird das FLugzeug hinzugefügt
airSession.addPlane(smp.getEntity(i), airlineId);
// Hier kriege ich eine IndexOutOfBounds-Exception wenn ich das FLugzeug aus der liste holen will
System.out.println("Fluggesellschaft "
+ airSession.getEntity(airlineId).getName()
+ " mit Flugzeug " + airSession.getEntity(airlineId).getPlaneList().get(0));
airSession.removeEntity(airlineId);
smp.removeEntity(i);
pts.removeEntity(x);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
new PlaneClient();
}
}
Ich verstehe einfach nicht warum in der Liste kein Objekt ist. In der EJB-Klasse wird das FLugzeug das ich hinzufüge abgelegt und ist auch in der Liste, nur im CLient ist es nicht mehr da...