JPA @ManyToOne字段为空

时间:2013-01-17 16:36:00

标签: sql jpa playframework

我花了几个小时研究如何解决这个问题。当我试图从孩子那里得到父母时,除了它的id字段是空的。这没有任何意义。我正在使用PlayFramework 2.0.4,如果这可能表明任何事情(除了一个糟糕的框架选择)。

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
    );

}

1 个答案:

答案 0 :(得分:1)

finder有一个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;
}