JEE实现方法 [英] methode JEE implementation

查看:123
本文介绍了JEE实现方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一种方法,该方法允许我自动生成测试(整个问题),但问题是: 该方法显然会使用随机问题的数量,还会生成所生成问题的类别(我有一个entié类别,因此也有一个表)我不知道我将类别放在查询中的什么位置. 其次JPQL不使用RANDOM()我该怎么办?

I implemented a method that allows me to generate a test ( entire of question ) automatically but the problem is : the method will obviously take the number of random questions but also the category of questions generated (I have a entié category and therefore a table too) I don't know where i will put the category in query. and secondly RANDOM() is not taked by JPQL what can i do ?

数据库架构

public List<Question> prepareRandomTest(int number_of_questions, Categorie      categorie){ 
      String jpql = "SELECT q FROM Question q ORDER BY RANDOM() LIMIT "+number_of_questions  ;
     Query query = entityManager.createQuery(jpql);
      return query.getResultList();
    }

推荐答案

您正尝试在此处使用 Java持久性查询语言,因此您的解决方案未考虑到RANDOM.使用本机查询并从本机sql字符串构建Query,本机查询只是没有实体对象引用(如Question)的普通sql语句.这样,普通的sql关键字(例如RANDOM等)就可以轻松阅读.

You are trying to use Java Persistence Query Language here, Hence your solution is not taking RANDOM into account. Use Native query and build Query from native sql string, Native query is just a plain sql statement without Entity object reference (like Question). This way a normal sql keywords like RANDOM etc are easily read.

本地查询教程

代替使用

Native Query Tutorial

Instead of Using

String jpql = "SELECT q FROM Question q ORDER BY RANDOM() LIMIT "+number_of_questions  ;

使用:

"SELECT * FROM question where category="+category+" ORDER BY RANDOM() LIMIT "+number_of_questions;

其他建议: 从代码中的枚举"中获取类别字符串,以匹配数据库类别列中的字符串值.

Additional Advice: Get the category string from an "Enum" in your code to match the string value in the database category column.

这篇关于JEE实现方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆