JPA @ManyToOne字段为空

问题描述:

我花了数小时寻找如何解决这个问题。当我试图从小孩那里得到父母的所有,但它的id字段是空的。这没有意义。我使用PlayFramework 2.0.4,如果这可能表明任何东西(除了可怕的框架选择)。JPA @ManyToOne字段为空

TRoute.java(父)

@Entity 
@Table(name="routes") 
public class TRoute extends Model { 

    @Id 
    public String route_id; 
    public String agency_id; 
    @Constraints.Required 
    public String route_short_name; 
    @Constraints.Required 
    public String route_long_name; 
    public String route_desc; 
    @Constraints.Required 
    public String route_type; 
    public String route_url; 
    public String route_color; 
    public String route_text_color; 

    @OneToMany(mappedBy="troute") 
    public List<Trip> trips; 

    public static Finder<String, TRoute> find = new Finder(
      String.class, TRoute.class 
    ); 

} 

Trip.java(子)

@Entity 
@Table(name="trips") 
public class Trip extends Model { 

    @Constraints.Required 
    public String route_id; 
    @Constraints.Required 
    public String service_id; 
    @Id 
    public String trip_id; 
    public String trip_headsign; 
    public String direction_id; 
    public String block_id; 
    public String shape_id; 

    @ManyToOne 
    @JoinColumn(name="route_id") 
    public TRoute troute; 

    public static List<Trip> byRouteId(String route_id) { 
     List<Trip> trips = 
      Trip.find 
      .where().like("route_id", route_id) 
      .findList(); 
     return trips; 
    } 

    public static Finder<String, Trip> find = new Finder(
      String.class, Trip.class 
    ); 

} 
+0

你的日志显示了什么?你看到父级的SQL提取? –

+0

我不是那种在Java中经历过的。有没有办法查看由Play框架执行的SQL查询? – heroix

取景器具有可用于加载其他表中的特性的fetch()方法。例如:

public static List<Trip> byRouteId(String route_id) { 
    List<Trip> trips = List<Trip> trips = Trip.find 
     .fetch("troute") // fetch the other table's properties here 
     .where() 
     .like("route_id", route_id) 
     .findList(); 
    return trips; 
} 
+0

所以这可能意味着如果我使用finder来获取我的Trips,它会包含正常的TRoute对象吗?失败的原因是我使用了我描述自己并且显然不完全正确的RouteId()方法? – heroix

+1

是的,每个“Trip”将包含一个“TRoute”对象。默认情况下,相邻表的属性不会被加载,因为你可能真的只想要Trip属性而不关心将数据发送到Web浏览器时的'TRoute'属性。在上面添加'.fetch'行指定,每次调用'byRouteId'方法时都要加载这些数据 –