函数与HQL语言相加

时间:2016-05-19 14:09:26

标签: java mysql hibernate hql

我有一个问题(抱歉我的英语,我正在学习)! 我到处搜索如何使用命令public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText editTextCurso; private EditText editTextBibliografia; private EditText editTextLocalização; private Button buttonAdd; private Button buttonView; private Button buttonMain; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextCurso = (EditText) findViewById(R.id.editTextCurso); editTextBibliografia = (EditText) findViewById(R.id.editTextBibliografia); editTextLocalização = (EditText) findViewById(R.id.editTextLocalização); buttonAdd = (Button) findViewById(R.id.buttonAdd); buttonView = (Button) findViewById(R.id.buttonView); buttonMain = (Button) findViewById(R.id.buttonMain); buttonAdd.setOnClickListener(this); buttonView.setOnClickListener(this); buttonMain.setOnClickListener(this); } private void addBook(){ final String curso = editTextCurso.getText().toString().trim(); final String bibliografia = editTextBibliografia.getText().toString().trim(); final String localização = editTextLocalização.getText().toString().trim(); class AddBook extends AsyncTask<Void,Void,String>{ ProgressDialog loading; @Override protected void onPreExecute() { super.onPreExecute(); loading = ProgressDialog.show(MainActivity.this,"Adicionando...","Espere...",false,false); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); loading.dismiss(); Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show(); } @Override protected String doInBackground(Void... v) { HashMap<String,String> params = new HashMap<>(); params.put(Config.KEY_EMP_CURSO,curso); params.put(Config.KEY_EMP_BIBLIOGRAFIA,bibliografia); params.put(Config.KEY_EMP_LOCALIZAÇÃO,localização); RequestHandler rh = new RequestHandler(); String res = rh.sendPostRequest(Config.URL_ADD, params); return res; } } AddBook ae = new AddBook(); ae.execute(); } @Override public void onClick(View v) { if(v == buttonAdd){ addBook(); } if(v == buttonView){ Button button = (Button) v; startActivity(new Intent(this, ViewAllBook.class)); } if(v == buttonMain){ Button button = (Button) v; startActivity(new Intent(getApplicationContext(),Main2Activity.class)); } } sum(column) hibernate语言,但我找不到任何东西!

我可以用HQL语言来完成。示例:

SQL

但不是HQL Hibernate!

2 个答案:

答案 0 :(得分:2)

您可以在HQL和SQL中使用聚合函数,查看Hibernate查询语言手册:https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-aggregation

答案 1 :(得分:1)

您可以使用与SQL相同的查询,请尝试以下解决方案:

Session s = OptimazPoolM.getSessionFactory().openSession();
String sumHql = "SELECT SUM(salary) FROM employees WHERE idemployee = 31";
Query sumQuery = s.createQuery(sumHql);
System.out.println(sumQuery.list().get(0));