Hibernate在这个HQL交叉连接上窒息

时间:2013-07-30 09:39:45

标签: java hibernate transactions hql

在摆弄下面的代码之后,我突然得到一个异常,告诉“交易未成功启动”。我似乎没有摆脱它。你能发现Java方法中的错误吗?

数据模型

建议:suggestion_id,name,isReceivedNSD,isSetupNSD,isDecidedUHR,meeting_id,.. 会议:meeting_id,日期
记录:log_id,suggestion_id,..

这里有什么特别之处:

Controlpanel:controlpanel_id,processMeeting_id(总是只有一行,包含当前选项)

Java方法

public static List<Suggestion> getSuggestion(final int maxResults,
     final boolean isReceivedNSD, final boolean isSetupNSD, final boolean isDecidedUHR,
     final Status status) throws GettingException {
  final Transaction transaction = getSession().getTransaction();
  List<Suggestion> list = null;
  final Query query;
  try {
     final String hql = "select f" //
           + " from Controlpanel p, Log L" //
           + " where L.suggestion.isReceivedNSD = :isReceivedNSD" //
           + "   and L.suggestion.isSetupNSD = :isSetupNSD" //
           + "   and L.suggestion.isDecidedUHR = :isDecidedUHR" //
           + "   and L.status.id = :statusId" //
           + "   and L.suggestion.meeting.id = p.processMeeting.id";
     query = getSession().createQuery(hql);
     query.setParameter("isReceivedNSD", isReceivedNSD);
     query.setParameter("isSetupNSD", isSetupNSD);
     query.setParameter("isDecidedUHR", isDecidedUHR);
     query.setParameter("statusId", status.getId());
     query.setMaxResults(maxResults);
     list = castList(Suggestion.class, query.list());
     transaction.commit();
  }
  catch (final HibernateException e) {
     throw new GettingException("Suggestion list. " + e.getMessage());
  }

  finally {
     HibernateCommon.rollbackIfNeededDuringHenting(transaction);
  }
  return list;
}

1 个答案:

答案 0 :(得分:1)

消息很明确:交易未成功启动。你忘了开始交易了:

final Transaction transaction = getSession().getTransaction();
transaction.begin();

或者简单地说:

final Transaction transaction = getSession().beginTransaction();