使用param按集合大小查询dsl顺序

时间:2017-04-15 10:05:21

标签: java sql-order-by querydsl

在QueryDSL中是否可以按照具有额外参数的集合大小进行排序?
例如,当我按集合大小OrderSpecifier<Integer> severity = alarmSet.alarms.size().desc();排序时,它会生成下一个SQL:

ORDER BY (SELECT 
        COUNT(alarms.AL_ALARM_SET_ID)
    FROM
        ALARM alarms
    WHERE
        alarmset.ID = alarms.AL_ALARM_SET_ID) DESC

但是我希望它为WHERE AND alarms.AL_STATUS = 1添加条件并生成下一个:

    ORDER BY (SELECT 
            COUNT(alarms.AL_ALARM_SET_ID)
        FROM
            ALARM alarms
        WHERE
            alarmsets.ID = alarms.AL_ALARM_SET_ID 
            AND alarms.AL_STATUS = 1) DESC

1 个答案:

答案 0 :(得分:0)

看起来在QueryDsl中做这种工作是不可能的(如果可能请提供解决方案!)。 但我找到了其他方法 - 通过此列添加包含警报计数和订单的自定义列。

OrderSpecifier<Long> severity = new OrderSpecifier<>(Order.DESC, Expressions.numberPath(Long.class, "customCount"));

自定义列之后的订单:

query.select(Projections.tuple(room, countExpression)).fetch();

使用Tuple选择数据:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Spinner spinner;
    ImageView imageView,imgpt1,imgpt2,imgpt3,imgpt4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner=(Spinner)findViewById(R.id.spinner);
        imageView=(ImageView)findViewById(R.id.imageViewspinner);
        imgpt1=(ImageView)findViewById(R.id.imagepart1);
        imgpt2=(ImageView)findViewById(R.id.imagepart2);
        imgpt3=(ImageView)findViewById(R.id.imagepart3);
        imgpt4=(ImageView)findViewById(R.id.imagepart4);

        imgpt1.setOnClickListener(this);
        imgpt2.setOnClickListener(this);
        imgpt3.setOnClickListener(this);
        imgpt4.setOnClickListener(this);
        List<String> categories=new ArrayList<String>();
        categories.add("Front View");
        categories.add("Side View");
        categories.add("Back View");
        categories.add("Top View");

        ArrayAdapter<String> stringArrayAdapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,categories);
        stringArrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        spinner.setAdapter(stringArrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position==0)
                {
                    imageView.setImageResource(R.drawable.carfrontview);
                    imgpt1.setImageResource(R.drawable.frontbumpers);
                    imgpt1.setTag("frontbumpers");
                    imgpt2.setImageResource(R.drawable.frontfenders);
                    imgpt2.setTag("frontfenders");
                    imgpt3.setImageResource(R.drawable.frontgrilles);
                    imgpt2.setTag("frontgrilles");
                    imgpt4.setImageResource(R.drawable.frontheadlight);
                    imgpt2.setTag("frontheadlight");
                }
                else if(position==1)
                {
                    imageView.setImageResource(R.drawable.carsideview);
                    imgpt1.setImageResource(R.drawable.sidebumpers);
                    imgpt2.setImageResource(R.drawable.sidedoor);
                    imgpt3.setImageResource(R.drawable.sidefuelcovers);
                    imgpt4.setImageResource(R.drawable.sidemirror);

                }
                else if(position==2)
                {
                    imageView.setImageResource(R.drawable.carbackview);
                    imgpt1.setImageResource(R.drawable.backrearbumpers);
                    imgpt2.setImageResource(R.drawable.backrearmirror);
                    imgpt3.setImageResource(R.drawable.backrearwindow);
                    imgpt4.setImageResource(R.drawable.backtaillights);
                }
                else if(position==3)
                {
                    imageView.setImageResource(R.drawable.cartopview);
                    imgpt1.setImageResource(R.drawable.topbattery);
                    imgpt2.setImageResource(R.drawable.topfuelinjection);
                    imgpt3.setImageResource(R.drawable.topoildipstick);
                    imgpt4.setImageResource(R.drawable.topradiator);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        String message="";
        if(imgpt1.getTag()=="frontbumpers")
        {
            message="This is Bumper";
        }
        else if(imgpt1.getTag()=="frontfenders")
        {
            message="This is Fenders";
        }
        else if(imgpt1.getTag()=="frontheadlight")
        {
            message="This is headlight";
        }
        else if(imgpt1.getTag()=="frontgrilles")
        {
            message="This is grilles";
        }

        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Car Parts");
        builder.setMessage(message);
        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        }).create().show();

    }
}