Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] EL-novice question

Hello, I am testnig EclipseLink and have one problem: I have two classes, Route and RouteStop (tables are named same, everything is ddl auto-generated). Each Route is collection of RouteStops which is defined by field "route" in RouteStop table. So here is what I did:

Route.java:

@Entity
public class Route implements Serializable {
   @Id
   @GeneratedValue
   private int route_id = 0;
...
   @OneToMany(targetEntity=RouteStop.class, mappedBy="route")
   @JoinColumn(name="ROUTE_ID", referencedColumnName="ROUTE")   
   private List<RouteStop> stops;
...
}

RouteStop.java:

@NamedQuery (
   name = "All Routes", 
   query = "select r from Route r"
)

@Entity
public class RouteStop implements Serializable {
   @Id
   @GeneratedValue
   private int route_stop_id = 0;
...
   @ManyToOne(optional=true, fetch=FetchType.LAZY)
   private Route route;
...
}

Now I inserted one route with two stops to empty (MySQL) DB:
em.getTransaction().begin();
Route r = new Route();
em.persist(r);

RouteStop rs1 = new RouteStop();
rs1.setRoute(r);
em.persist(rs1);

RouteStop rs2 = new RouteStop();
rs2.setRoute(r);
em.persist(rs2);
em.getTransaction().commit();

EclipseLink created both tables nicely and inserted these 3 records. In ROUTESTOP table there is "ROUTE_ROUTE_ID" field, which properly contains ID of related route.
Problem is when readnig Route, it does contains empty RouteStop list:

Query q = em.createNamedQuery("All Routes");
List<Route> res = q.getResultList(); // This list is OK, contains one route
for (Route route : res) {
   List<RouteStop> stops = route.getStops();
   for (RouteStop rs : stops) {
      // never gets here, stops list is empty!
   }
}

Any idea?


Back to the top