【问题标题】:Play! Framework JPA and GroupBy玩!框架 JPA 和 GroupBy
【发布时间】:2013-01-18 13:20:07
【问题描述】:

玩!框架没有按功能分组。这种缺乏功能真的开始让我恼火。我该如何解决这个问题?我希望byRouteId 按trip_headsign 分组。简单的原始查询如下所示:

SELECT *
FROM trips
WHERE route_id = 1070
GROUP BY trip_headsign

这是我的 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
            .fetch("troute") // fetch TRoute properties.
            .where().like("route_id", route_id)
            .findList();
        return trips;
    }

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

}

【问题讨论】:

  • This thread 可能会对您有所帮助。你能测试其中一种方法是否有效吗?

标签: java jpa playframework group-by ebean


【解决方案1】:

您可以通过使用 javax.persistence 的 EntityManager 类中的 createNativeQuery 函数来使用原始 SQL 查询。

EntityManager em = play.db.jpa.JPA.em();
List<Trip> tripList = em.createNativeQuery("insert raw sql query here").getResultList();

【讨论】:

  • [RuntimeException: 没有为名称 [default] 配置 JPA EntityManagerFactory]
猜你喜欢
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
相关资源
最近更新 更多