Я новичок в весенней загрузке и пытаюсь объединить два не связанных между собой объекта (Transaction и Item), используя JPA / JPQL. Но я получаю следующую ошибку при использовании thymeleaf для отображения в виде таблицы:
2020-04-08 21:42:42.463 ERROR 73816 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "transactiondetail.tid" (template: "index" - line 27, col 21)] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'tid' cannot be found on object of type 'java.lang.Object[]' - maybe not public or not valid?
Ниже приведена моя реализация thymeleaf:
<tr th:each="transactiondetail : ${listTransactionDetail}">
<td th:text="${transactiondetail.tid}">TID</td>
<td th:text="${transactiondetail.from_uid}">From UID</td>
<td th:text="${transactiondetail.to_uid}">To UID</td>
<td th:text="${transactiondetail.item_name}">Item Name</td>
<td th:text="${transactiondetail.item_count}">Count</td>
<td th:text="${transactiondetail.status}">Status</td>
Ниже приведен контроллер:
@Autowired
QueryService queryservice;
@RequestMapping("/")
public String viewHomePage(Model model) {
List<TransactionDetail> listTransactionDetail = queryservice.JPQLQuery();
model.addAttribute("listTransactionDetail", listTransactionDetail);
return "index";
}
Я могу получить результат, когда просто возвращаю queryservice.JPQLQuery()
, что означает, что объединение и листинг работают. Ниже приведен сервис, который вызывает контроллер:
@Service
public class QueryService implements IQueryService {
@Autowired
EntityManagerFactory emf;
@Override
public List<TransactionDetail> JPQLQuery()
{
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("Select s.tid,s.from_uid,s.to_uid,d.item_name,s.item_count,s.status from Transaction s inner join Item d on s.item_id=d.item_id");
@SuppressWarnings("unchecked")
List<TransactionDetail> tranlist = (List<TransactionDetail>)query.getResultList();
em.close();
return tranlist;
}
}
Ниже показан класс TransactionDetail, который принимает присоединяемые объекты. Как видите, у меня есть как конструкторы, так и геттеры / сеттеры с собственными именами. Все еще не могу правильно отобразить детали.
public class TransactionDetail {
private Long tid;
private int from_uid;
private int to_uid;
private String item_name;
private int item_count;
private int status;
public TransactionDetail() {
}
public TransactionDetail(Long tid, int from_uid, int to_uid, String item_name, int item_count, int status) {
super();
this.tid = tid;
this.from_uid = from_uid;
this.to_uid = to_uid;
this.item_name = item_name;
this.item_count = item_count;
this.status = status;
}
public Long getTid() {
return tid;
}
public void setTid(Long tid) {
this.tid = tid;
}
public int getFrom_uid() {
return from_uid;
}
public void setFrom_uid(int from_uid) {
this.from_uid = from_uid;
}
public int getTo_uid() {
return to_uid;
}
public void setTo_uid(int to_uid) {
this.to_uid = to_uid;
}
public int getItem_count() {
return item_count;
}
public void setItem_count(int item_count) {
this.item_count = item_count;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getItem_name() {
return item_name;
}
public void setItem_name(String item_name) {
this.item_name = item_name;
}
}
Я предполагаю, что проблема связана с тем, как я сохраняю результаты из query.getResultList()
в List<TransactionDetail>
. Так есть идеи, как я могу решить эту проблему? Заранее спасибо!