Spring数据JPA存储库按条件排序

时间:2017-03-13 20:39:06

标签: mysql spring hibernate spring-data-jpa

我有一个包含一些属性的产品实体,我需要根据某些标准获得与给定产品最接近的产品,标准是类别,完成和生产公司。 我写了这个查询

String title = "Upcoming site:";


    Intent notificationIntent = null;

    if (siteId > 0) {
        pref.setNewVar("notificationSiteId", String.valueOf(siteId));
        notificationIntent = new Intent(appContext, DetailActivity.class);
    } else {
        notificationIntent = new Intent(appContext, MainActivityWithMenu.class);
    }


    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    notificationIntent.putExtra("siteId", siteId);
    PendingIntent pendingIntent = PendingIntent.getActivity(appContext, 0,
            notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Notification.Builder nBuilder = new Notification.Builder(appContext)
            .setContentTitle("Neeco - " + title)
            .setContentText(text)
            .setSmallIcon(R.drawable.clock_start)
            .setContentIntent(pendingIntent);

    boolean updateNotification = true;
    if (pref.getVar("notificationText") != null && !pref.getVar("notificationText").equals(text)) {
        Log.d(TAG, "New text in notification - " + text);
        nBuilder.setSound(alarmSound);
        nBuilder.setPriority(priority);
        pref.setNewVar("notificationText", text);
    } else if (pref.getVar("notificationText") == null) {
        nBuilder.setSound(alarmSound);
        nBuilder.setPriority(priority);
        pref.setNewVar("notificationText", text);
    } else {
        updateNotification = false;
    }

    if (updateNotification) {
        Notification notification = nBuilder.build();
        NotificationManager mNotificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(Config.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
    }

但这不断给我以下例外

@Query("Select pr from Product pr ORDER BY (CASE WHEN (pr.category = :p.category) THEN 1 WHEN (pr.finish = :p.finish) THEN 2 WHEN (pr.productionCompany = :p.productionCompany) THEN 3 ELSE 4 ) LIMIT 5")
List<Product> findRecommendedProducts(@Param("p")Product p);

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我认为您需要在查询中添加ELSE 4 END。我假设这是在Oracle上运行的吗?