Changeset 7371


Ignore:
Timestamp:
May 17, 2017, 1:10:51 PM (5 years ago)
Author:
Nicklas Nordborg
Message:

References #2084: Upgrade to Hibernate 5.2

Started to replace org.hibernate.Query with org.hibernate.query.Query in the remaining code. Compilation errors up again to 306 and warnings to 1198.

Location:
branches/hibernate-5.2/src/core/net/sf/basedb/core
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/AbstractHqlQuery.java

    r6875 r7371  
    4040import java.util.regex.Pattern;
    4141
    42 import org.hibernate.Query;
     42import org.hibernate.query.Query;
    4343
    4444/**
     
    100100    The most recent main query.
    101101  */
    102   private org.hibernate.Query lastMainQuery;
     102  private org.hibernate.query.Query<?> lastMainQuery;
    103103 
    104104  /**
    105105    The most recent count query.
    106106  */
    107   private org.hibernate.Query lastCountQuery;
     107  private org.hibernate.query.Query<Long> lastCountQuery;
    108108 
    109109  /**
     
    111111    @since 3.5
    112112  */
    113   private org.hibernate.Query lastIdQuery;
     113  private org.hibernate.query.Query<Integer> lastIdQuery;
    114114 
    115115  /**
     
    177177    try
    178178    {
    179       Query query = getCountHqlQuery(dc);
     179      Query<Long> query = getCountHqlQuery(dc);
    180180      count = isStateless() ?
    181181        QueryExecutor.loadData(Long.class, query, dc.getStatelessSession(), dc.getSessionControl()) :
     
    226226    @return A <code>org.hibernate.Query</code> object
    227227  */
    228   org.hibernate.Query getMainHqlQuery(DbControl dc)
     228  org.hibernate.query.Query<?> getMainHqlQuery(DbControl dc)
    229229  {
    230230    if (lastMainQuery == null || lastMainDc != dc)
     
    247247    if (getMaxResults() > 0) lastMainQuery.setMaxResults(getMaxResults());
    248248    return lastMainQuery;
     249  }
     250 
     251  // TODO - is this the best solution??
     252  <T> org.hibernate.query.Query<T> getMainHqlQuery(DbControl dc, Class<T> returnType)
     253  {
     254    return (Query<T>)getMainHqlQuery(dc);
    249255  }
    250256 
     
    258264    @return A <code>org.hibernate.Query</code> object
    259265  */
    260   org.hibernate.Query getCountHqlQuery(DbControl dc)
     266  org.hibernate.query.Query<Long> getCountHqlQuery(DbControl dc)
    261267  {
    262268    if (lastCountQuery == null || lastCountDc != dc)
     
    266272      if (stateless)
    267273      {
    268         lastCountQuery =  HibernateUtil.createQuery(dc.getStatelessSession(), countHql);
     274        lastCountQuery =  HibernateUtil.createQuery(dc.getStatelessSession(), countHql, Long.class);
    269275      }
    270276      else
    271277      {
    272         lastCountQuery =  HibernateUtil.createQuery(dc.getHibernateSession(), countHql);
     278        lastCountQuery =  HibernateUtil.createQuery(dc.getHibernateSession(), countHql, Long.class);
    273279      }
    274280    }
     
    289295    @since 3.5
    290296  */
    291   org.hibernate.Query getIdHqlQuery(DbControl dc)
     297  org.hibernate.query.Query<Integer> getIdHqlQuery(DbControl dc)
    292298  {
    293299    if (lastIdQuery == null || lastIdDc != dc)
     
    297303      if (stateless)
    298304      {
    299         lastIdQuery =  HibernateUtil.createQuery(dc.getStatelessSession(), idHql);
     305        lastIdQuery =  HibernateUtil.createQuery(dc.getStatelessSession(), idHql, Integer.class);
    300306      }
    301307      else
    302308      {
    303         lastIdQuery =  HibernateUtil.createQuery(dc.getHibernateSession(), idHql);
     309        lastIdQuery =  HibernateUtil.createQuery(dc.getHibernateSession(), idHql, Integer.class);
    304310      }
    305311    }
     
    389395    @param parameters A map containing parameter names and values
    390396  */
    391   private void setParameters(org.hibernate.Query query, Map<String, QueryParameter> parameters)
     397  private void setParameters(org.hibernate.query.Query<?> query, Map<String, QueryParameter> parameters)
    392398  {
    393399    if (parameters == null) return;
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/DbControl.java

    r7370 r7371  
    5252import java.util.LinkedHashMap;
    5353import java.util.Set;
     54
     55import org.hibernate.query.Query;
     56
    5457import java.lang.reflect.Constructor;
    5558
     
    267270      @throws ConnectionClosedException If connection to database has been closed.
    268271    @throws BaseException If there is another error
    269     @deprecated In 3.11. Applications should use separate DbControl instead.
     272    @deprecated In 3.12. Applications should use separate DbControl instead.
    270273  */
    271274  @Deprecated
     
    280283    @throws ConnectionClosedException If connection to database has been closed.
    281284    @throws BaseException If there is another error.
    282     @deprecated In 3.11. Applications should use separate DbControl instead.
     285    @deprecated In 3.12. Applications should use separate DbControl instead.
    283286  */
    284287  @Deprecated
     
    694697    location and quota type.
    695698  */
     699  @SuppressWarnings("unchecked")
    696700  long getDiskUsage(UserData user, QuotaTypeData quotaType, Location location)
    697701    throws BaseException
     
    700704    assert location != null : "location == null";
    701705    assert quotaType != null : "quotaType == null";
    702     org.hibernate.Query query;
     706    org.hibernate.query.Query<Long> query;
    703707    if (quotaType.getId() == SystemItems.getId(QuotaType.TOTAL))
    704708    {
    705       query = HibernateUtil.getPredefinedQuery(hSession, "GET_TOTAL_DISKUSAGE_FOR_USER");
     709      query = (Query<Long>)HibernateUtil.getPredefinedQuery(hSession, "GET_TOTAL_DISKUSAGE_FOR_USER");
    706710      /*
    707711        SELECT SUM(du.bytes)
     
    713717    else
    714718    {
    715       query = HibernateUtil.getPredefinedQuery(hSession, "GET_SPECIFIC_DISKUSAGE_FOR_USER");
     719      query = (Query<Long>)HibernateUtil.getPredefinedQuery(hSession, "GET_SPECIFIC_DISKUSAGE_FOR_USER");
    716720      /*
    717721        SELECT SUM(du.bytes)
     
    733737    location and quota type.
    734738  */
     739  @SuppressWarnings("unchecked")
    735740  long getDiskUsage(GroupData group, QuotaTypeData quotaType, Location location)
    736741    throws BaseException
     
    739744    assert location != null : "location == null";
    740745    assert quotaType != null : "quotaType == null";
    741     org.hibernate.Query query;
     746    org.hibernate.query.Query<Long> query;
    742747    if (quotaType.getId() == SystemItems.getId(QuotaType.TOTAL))
    743748    {
    744       query = HibernateUtil.getPredefinedQuery(hSession, "GET_TOTAL_DISKUSAGE_FOR_GROUP");
     749      query = (Query<Long>)HibernateUtil.getPredefinedQuery(hSession, "GET_TOTAL_DISKUSAGE_FOR_GROUP");
    745750      /*
    746751        SELECT SUM(du.bytes)
     
    752757    else
    753758    {
    754       query = HibernateUtil.getPredefinedQuery(hSession, "GET_SPECIFIC_DISKUSAGE_FOR_GROUP");
     759      query = (Query<Long>)HibernateUtil.getPredefinedQuery(hSession, "GET_SPECIFIC_DISKUSAGE_FOR_GROUP");
    755760      /*
    756761        SELECT SUM(du.bytes)
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/HibernateUtil.java

    r7368 r7371  
    9292import org.hibernate.StaleStateException;
    9393import org.hibernate.FlushMode;
    94 import org.hibernate.Query;
    95 import org.hibernate.SQLQuery;
     94import org.hibernate.query.Query;
     95import org.hibernate.query.NativeQuery;
    9696import org.hibernate.Filter;
    9797import org.hibernate.ScrollableResults;
     
    16531653        " FROM "+clazz.getName()+" item"+
    16541654        " WHERE item.id = " + id;
    1655       Query query = session.createQuery(hql);
    1656       List<?> result = query.list();
    1657       return (Long)result.get(0) != 0;
     1655      Query<Long> query = session.createQuery(hql, Long.class);
     1656      return query.uniqueResult() != 0;
    16581657    }
    16591658    catch (HibernateException ex)
     
    17151714      item of the specified class
    17161715  */
    1717   static <T> T loadData(Class<T> clazz, Query query)
     1716  static <T> T loadData(Class<T> clazz, Query<T> query)
    17181717    throws BaseException
    17191718  {
     
    17371736    @return The number of affected items
    17381737  */
    1739   static int executeUpdate(Query query)
     1738  static int executeUpdate(Query<?> query)
    17401739    throws BaseException
    17411740  {
     
    18171816    Create a Hibernate query.
    18181817  */
    1819   static Query createQuery(Session session, String hql)
     1818  @SuppressWarnings({ "unchecked", "rawtypes" })
     1819  static Query<?> createQuery(Session session, String hql)
    18201820    throws BaseException
    18211821  {
     
    18241824    try
    18251825    {
    1826       Query q = session.createQuery(hql);
     1826      Query<?> q = session.createQuery(hql);
    18271827      q.setFetchSize(getJdbcFetchSize());
    1828       return new EntityQueryWrapper((org.hibernate.query.Query)q);
     1828      return new EntityQueryWrapper(q);
    18291829    }
    18301830    catch (HibernateException ex)
     
    18331833    }
    18341834  }
     1835 
     1836  /**
     1837    Create a Hibernate query with a known return type.
     1838    @since 3.12
     1839  */
     1840  static <R> Query<R> createQuery(Session session, String hql, Class<R> returnType)
     1841    throws BaseException
     1842  {
     1843    assert session != null : "session == null";
     1844    assert hql != null : "hql == null";
     1845    try
     1846    {
     1847      Query<R> q = session.createQuery(hql, returnType);
     1848      q.setFetchSize(getJdbcFetchSize());
     1849      return new EntityQueryWrapper<R>(q);
     1850    }
     1851    catch (HibernateException ex)
     1852    {
     1853      throw new BaseException(ex);
     1854    }
     1855  }
    18351856
    18361857  /**
    18371858    Create a Hibernate query using a stateless session.
    18381859  */
    1839   static Query createQuery(StatelessSession session, String hql)
     1860  @SuppressWarnings({ "unchecked", "rawtypes" })
     1861  static Query<?> createQuery(StatelessSession session, String hql)
    18401862    throws BaseException
    18411863  {
     
    18441866    try
    18451867    {
    1846       Query q = session.createQuery(hql);
     1868      Query<?> q = session.createQuery(hql);
    18471869      q.setFetchSize(getJdbcFetchSize());
    1848       return new EntityQueryWrapper((org.hibernate.query.Query)q);
     1870      return new EntityQueryWrapper(q);
    18491871    }
    18501872    catch (HibernateException ex)
     
    18531875    }
    18541876  }
     1877 
     1878  /**
     1879    Create a Hibernate query using a stateless session and a known return type.
     1880    @since 3.12
     1881  */
     1882  static <R> Query<R> createQuery(StatelessSession session, String hql, Class<R> returnType)
     1883    throws BaseException
     1884  {
     1885    assert session != null : "session == null";
     1886    assert hql != null : "hql == null";
     1887    try
     1888    {
     1889      Query<R> q = session.createQuery(hql, returnType);
     1890      q.setFetchSize(getJdbcFetchSize());
     1891      return new EntityQueryWrapper<R>(q);
     1892    }
     1893    catch (HibernateException ex)
     1894    {
     1895      throw new BaseException(ex);
     1896    }
     1897  }
    18551898
    18561899  /**
    18571900    Create a Hibernate SQL query.
    18581901  */
    1859   static SQLQuery createSqlQuery(Session session, String sql)
     1902  static NativeQuery<?> createSqlQuery(Session session, String sql)
    18601903    throws BaseException
    18611904  {
     
    18651908    try
    18661909    {
    1867       SQLQuery q = session.createSQLQuery(sql);
    1868       q.setFetchSize(getJdbcFetchSize());
     1910      NativeQuery<?> q = session.createNativeQuery(sql);
     1911      ((Query<?>)q).setFetchSize(getJdbcFetchSize());
    18691912      return q;
    18701913    }
     
    18781921    Create a Hibernate SQL query using a stateless session.
    18791922  */
    1880   static SQLQuery createSqlQuery(StatelessSession session, String sql)
     1923  static NativeQuery<?> createSqlQuery(StatelessSession session, String sql)
    18811924    throws BaseException
    18821925  {
     
    18861929    try
    18871930    {
    1888       SQLQuery q = session.createSQLQuery(sql);
    1889       q.setFetchSize(getJdbcFetchSize());
     1931      NativeQuery<?> q = session.createNativeQuery(sql);
     1932      ((Query<?>)q).setFetchSize(getJdbcFetchSize());
    18901933      return q;
    18911934    }
     
    19401983    @param sc A optional SessionControl that is automatically protected from
    19411984      timeouts during the time the query is running
    1942   */
    1943   @SuppressWarnings({"unchecked"})
    1944   static <T> List<T> loadList(Class<T> clazz, Query query, SessionControl sc)
     1985     
     1986    TODO -- remove?
     1987  */
     1988  static <T> List<T> loadList(Class<T> clazz, Query<T> query, SessionControl sc)
     1989    throws BaseException
     1990  {
     1991    return loadList(query, sc);
     1992  }
     1993 
     1994  /**
     1995    Load a <code>List</code> of items from the database
     1996    using a query.
     1997   
     1998    @param clazz The list should contain objects of this class
     1999    @param query The query to execute
     2000    @param sc A optional SessionControl that is automatically protected from
     2001      timeouts during the time the query is running
     2002  */
     2003  static <T> List<T> loadList(Query<T> query, SessionControl sc)
    19452004    throws BaseException
    19462005  {
     
    19502009    {
    19512010      if (sc != null) pinger = Application.newPinger(sc);
    1952       return (List<T>)query.list();
     2011      return query.list();
    19532012    }
    19542013    catch (Throwable ex)
     
    19682027    @param sc A optional SessionControl that is automatically protected from
    19692028      timeouts during the time the query is running
    1970   */
    1971   static <T> ScrollIterator<T> loadIterator(Class<T> clazz, Query query, SessionControl sc)
     2029    TODO - remove ???
     2030  */
     2031  static <T> ScrollIterator<T> loadIterator(Class<T> clazz, Query<T> query, SessionControl sc)
     2032    throws BaseException
     2033  {
     2034    return loadIterator(query, sc);
     2035  }
     2036 
     2037  /**
     2038    Scroll through the result of a query using an iterator.
     2039    @param clazz The iterator returns objects of this class
     2040    @param query The query to execute
     2041    @param sc A optional SessionControl that is automatically protected from
     2042      timeouts during the time the query is running
     2043    @since 3.12
     2044  */
     2045  static <T> ScrollIterator<T> loadIterator(Query<T> query, SessionControl sc)
    19722046    throws BaseException
    19732047  {
     
    19782052      if (sc != null) pinger = Application.newPinger(sc);
    19792053      ScrollableResults result = query.scroll(ScrollMode.FORWARD_ONLY);
    1980       return new ScrollIterator<T>(clazz, result);
     2054      return new ScrollIterator<T>(result);
    19812055    }
    19822056    catch (Throwable ex)
     
    19892063    }
    19902064  }
    1991 
     2065 
    19922066  /**
    19932067    Get a predefined HQL query.
    19942068    @see PredefinedQuery
    19952069  */
    1996   static Query getPredefinedQuery(Session session, String name)
     2070  static Query<?> getPredefinedQuery(Session session, String name)
    19972071    throws BaseException
    19982072  {
     
    20062080    @see PredefinedQuery
    20072081  */
    2008   static Query getPredefinedQuery(Session session, String name, String... replacements)
     2082  static Query<?> getPredefinedQuery(Session session, String name, String... replacements)
    20092083    throws BaseException
    20102084  {
     
    20192093    @see PredefinedQuery
    20202094  */
    2021   static Query getPredefinedQuery(StatelessSession session, String name)
     2095  static Query<?> getPredefinedQuery(StatelessSession session, String name)
    20222096    throws BaseException
    20232097  {
     
    20312105    @see PredefinedQuery
    20322106  */
    2033   static Query getPredefinedSQLQuery(Session session, String name, String... replacements)
     2107  static Query<?> getPredefinedSQLQuery(Session session, String name, String... replacements)
    20342108    throws BaseException
    20352109  {
     
    20432117    @see PredefinedQuery
    20442118  */
    2045   static Query getPredefinedSQLQuery(StatelessSession session, String name)
     2119  static NativeQuery<?> getPredefinedSQLQuery(StatelessSession session, String name)
    20462120    throws BaseException
    20472121  {
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/ItemQuery.java

    r4889 r7371  
    9090    SessionControl sc = dc.getSessionControl();
    9191   
    92     List<? extends BasicData> result = HibernateUtil.loadList(dataClass, getMainHqlQuery(dc), sc);
     92    List<? extends BasicData> result = HibernateUtil.loadList(getMainHqlQuery(dc, dataClass), sc);
    9393    long totalCount = result.size();
    9494   
     
    123123
    124124    ScrollIterator<? extends BasicData> result =
    125       HibernateUtil.loadIterator(dataClass, getMainHqlQuery(dc), sc);
     125      HibernateUtil.loadIterator(getMainHqlQuery(dc, dataClass), sc);
    126126    disableFilters(dc);
    127127    return new ItemResultIterator<I>(result, dc, itemClass, totalCount);
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/QueryExecutor.java

    r7075 r7371  
    3737import net.sf.basedb.core.hibernate.StatelessSessionWrapper;
    3838
    39 import org.hibernate.Query;
     39import org.hibernate.query.Query;
    4040import org.hibernate.ScrollMode;
    4141import org.hibernate.ScrollableResults;
     
    120120    @since 2.7
    121121  */
    122   public static <T> ScrollIterator<T> loadIterator(Class<T> clazz, Query query,
     122  public static <T> ScrollIterator<T> loadIterator(Class<T> clazz, Query<T> query,
    123123    StatelessSession session, SessionControl sc)
    124124    throws InterruptedException
     
    147147    @since 2.7
    148148  */
    149   public static <T> T loadData(Class<T> clazz, Query query,
     149  public static <T> T loadData(Class<T> clazz, Query<T> query,
    150150    StatelessSession session, SessionControl sc)
    151151    throws InterruptedException
     
    306306    implements Callable<ScrollIterator<T>>
    307307  {
    308     private final Query query;
     308    private final Query<T> query;
    309309    private final Class<T> clazz;
    310     FutureScrollIterator(Class<T> clazz, Query query)
     310    FutureScrollIterator(Class<T> clazz, Query<T> query)
    311311    {
    312312      this.clazz = clazz;
     
    318318    {
    319319      ScrollableResults result = query.scroll(ScrollMode.FORWARD_ONLY);
    320       return new ScrollIterator<T>(clazz, result);
     320      return new ScrollIterator<T>(result);
    321321    }
    322322  }
     
    325325    implements Callable<T>
    326326  {
    327     private final Query query;
     327    private final Query<T> query;
    328328    private final Class<T> clazz;
    329     FutureItem(Class<T> clazz, Query query)
     329    FutureItem(Class<T> clazz, Query<T> query)
    330330    {
    331331      this.clazz = clazz;
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/ScrollIterator.java

    r6127 r7371  
    3939{
    4040
    41   private final Class<E> dataClass;
    4241  private final ScrollableResults results;
    4342  private boolean isClosed;
    4443  private E nextElement;
    4544
    46   ScrollIterator(Class<E> dataClass, ScrollableResults results)
     45  ScrollIterator(ScrollableResults results)
    4746  {
    48     assert dataClass != null : "dataClass == null";
    4947    assert results != null : "results == null";
    50     this.dataClass = dataClass;
    5148    this.results = results;
    5249    this.isClosed = false;
     
    5855    ---------------------------
    5956  */
     57  @SuppressWarnings("unchecked")
    6058  @Override
    6159  public boolean hasNext()
     
    6765        if (results.next())
    6866        {
    69           nextElement = dataClass.cast(results.get(0));
     67          nextElement = (E)results.get(0);
    7068        }
    7169        else
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/SpecialQuery.java

    r6875 r7371  
    6060    SessionControl sc = dc.getSessionControl();
    6161    List<C> transformedResults = null;
    62     List<Object> results = HibernateUtil.loadList(Object.class, getMainHqlQuery(dc), sc);
     62    List<Object> results = HibernateUtil.loadList(getMainHqlQuery(dc, Object.class), sc);
    6363    long totalCount = results.size();
    6464   
Note: See TracChangeset for help on using the changeset viewer.