Changeset 7378


Ignore:
Timestamp:
May 22, 2017, 10:58:35 AM (5 years ago)
Author:
Nicklas Nordborg
Message:

References #2084: Upgrade to Hibernate 5.2

Getting rid of some more calls to deprecated method. Most have simple replacements, but the various NativeQuery.addXXXX() are a bit more complicated. Lycklily we can either replace them with a Object[] return type or by re-adding the possibility to speify a return type (as long as it matches a defined entity). See [7375]. A new test case in TestReporterCloneTemplate was added to check that this works.

Location:
branches/hibernate-5.2/src
Files:
5 edited

Legend:

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

    r7377 r7378  
    193193    org.hibernate.Session session = dc.getHibernateSession();
    194194    NativeQuery<?> sqlQuery = HibernateUtil.createSqlQuery(session, sql);
    195     sqlQuery.addScalar("annotationset_id", TypeWrapper.H_INTEGER);
    196     if (options.includeInherited)
    197     {
    198       sqlQuery.addScalar("id", TypeWrapper.H_INTEGER);
    199       sqlQuery.addScalar("source", TypeWrapper.H_INTEGER);
    200     }
    201195    sqlQuery.setParameter("annotationType", annotationTypeId, TypeWrapper.H_INTEGER);
    202196    setRestrictionParameters(sqlQuery);
     
    235229       
    236230        NativeQuery<Integer> sqlQuery2 = HibernateUtil.createSqlQuery(session, sql, Integer.class);
    237         sqlQuery2.addScalar("sid", TypeWrapper.H_INTEGER);
    238231        if (debugSqlEnabled) logSql.debug("Executing annotation query: " + sql);
    239232        List<Integer> ids = HibernateUtil.loadList(Integer.class, sqlQuery2, sc);
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/HibernateUtil.java

    r7377 r7378  
    11401140    try
    11411141    {
    1142       SessionBuilder builder = sf.withOptions();
     1142      SessionBuilder<?> builder = sf.withOptions();
    11431143      if (interceptor != null) builder.interceptor(interceptor);
    11441144      if (c != null) builder.connection(c);
     
    11461146      // FlushMode.COMMIT means that all changes
    11471147      // are automatically flushed before a commit is performed
    1148       session.setFlushMode(FlushMode.COMMIT);
     1148      session.setHibernateFlushMode(FlushMode.COMMIT);
    11491149      /*
    11501150        If you get an exception for setTransactionIsolation it may be a problem
     
    19451945    try
    19461946    {
    1947       NativeQuery<R> q = session.createNativeQuery(sql);
     1947      NativeQuery<R> q;
     1948      // Specifying non-entity (for example, Integer) return type makes Hibernate go bananas
     1949      // Guess it is a bug, but until it has been fixed, we need to use the non-specific method call
     1950      if (BasicData.class.isAssignableFrom(returnType))
     1951      {
     1952        q = session.createNativeQuery(sql, returnType);
     1953      }
     1954      else
     1955      {
     1956        q = session.createNativeQuery(sql);
     1957      }
    19481958      ((Query<R>)q).setFetchSize(getJdbcFetchSize());
    19491959      return q;
     
    19891999    try
    19902000    {
     2001      NativeQuery<R> q;
    19912002      // Specifying non-entity (for example, Integer) return type makes Hibernate go bananas
    19922003      // Guess it is a bug, but until it has been fixed, we need to use the non-specific method call
    1993       NativeQuery<R> q = session.createNativeQuery(sql); //, returnType);
     2004      if (BasicData.class.isAssignableFrom(returnType))
     2005      {
     2006        q = session.createNativeQuery(sql, returnType);
     2007      }
     2008      else
     2009      {
     2010        q = session.createNativeQuery(sql);
     2011      }
    19942012      ((Query<R>)q).setFetchSize(getJdbcFetchSize());
    19952013      return q;
     
    22022220    return createSqlQuery(session, PredefinedQuery.getQueryString(name, replacements));
    22032221  }
    2204  
     2222
    22052223  /**
    22062224    Get a predefined SQL query with a known return type.
     
    22082226    @since 3.12
    22092227  */
    2210   static <R> Query<R> getPredefinedSQLQuery(Session session, String name, Class<R> returnType, String... replacements)
     2228  static <R> NativeQuery<R> getPredefinedSQLQuery(Session session, String name, Class<R> returnType, String... replacements)
    22112229    throws BaseException
    22122230  {
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/Reporter.java

    r7374 r7378  
    114114      NativeQuery<ReporterData> q = HibernateUtil.createSqlQuery(dc.getStatelessSession(), sql.toString(), ReporterData.class);
    115115      q.setParameter("id", id, TypeWrapper.H_INTEGER);
    116       q.addEntity(ReporterData.class);
    117116      ReporterData rd = HibernateUtil.loadData(q);
    118117      if (rd == null) throw new ItemNotFoundException("Reporter[id="+id+"]");
     
    190189      NativeQuery<ReporterData> q = HibernateUtil.createSqlQuery(dc.getStatelessSession(), sql.toString(), ReporterData.class);
    191190      q.setParameter("id", externalId, TypeWrapper.H_STRING);
    192       q.addEntity(ReporterData.class);
    193191      ReporterData rd = HibernateUtil.loadData(q);
    194192      if (rd == null) throw new ItemNotFoundException("Reporter[externalId="+externalId+"]");
  • branches/hibernate-5.2/src/core/net/sf/basedb/core/ServiceSessionControl.java

    r7377 r7378  
    273273    @param dc An open DbControl
    274274  */
    275   public Session newSession(SessionBuilder builder, DbControl dc)
     275  public Session newSession(SessionBuilder<?> builder, DbControl dc)
    276276  {
    277277    Session s = builder.connection(HibernateUtil.getConnection(dc.getHibernateSession())).openSession();
    278     s.setFlushMode(FlushMode.COMMIT);
     278    s.setHibernateFlushMode(FlushMode.COMMIT);
    279279    dc.addTransactionalAction(new FlushSessionAction(s));
    280280    return s;
  • branches/hibernate-5.2/src/test/TestReporterCloneTemplate.java

    r5886 r7378  
    3737import net.sf.basedb.core.ReporterCloneTemplate;
    3838import net.sf.basedb.core.ReporterCloneBatcher.CloneSource;
     39import net.sf.basedb.core.data.ReporterData;
    3940import net.sf.basedb.core.query.Dynamic;
    4041import net.sf.basedb.core.query.SqlResult;
     
    8687      test_list_cloned_reporters(experimentId);
    8788
     89      test_externalId("B-actin_1/4", experimentId);
     90     
    8891      test_clone_reporters(id, experimentId, CloneSource.RAW, 572);
    8992      test_list_cloned_reporters(experimentId);
     
    360363  }
    361364
     365 
     366  static void test_externalId(String externalId, int experimentId)
     367  {
     368    if (experimentId == 0) return;
     369    DbControl dc = null;
     370    try
     371    {
     372      dc = TestUtil.getDbControl();
     373     
     374      Experiment exp = Experiment.getById(dc, experimentId);
     375     
     376      ReporterCloneTemplate template = exp.getVirtualDb().getReporterCloneTemplate();
     377      StringBuilder header = new StringBuilder("  ");
     378      int numProperties = 0;
     379      for (ClonableProperty cp : template.getClonableProperties())
     380      {
     381        header.append("\t").append(cp.getTitle());
     382        numProperties++;
     383      }
     384      if (!TestUtil.getSilent())
     385      {
     386        write(header.toString());
     387        write(header.toString().replaceAll("[^\t]", "-"));
     388      }
     389     
     390      ReporterData rd = Reporter.getByExternalId(dc, externalId, exp);
     391      StringBuilder data = new StringBuilder();
     392      data.append(1);
     393      for (ClonableProperty cp : template.getClonableProperties())
     394      {
     395        Object value = null;
     396        String propertyName = cp.getName();
     397        if (cp.isExtendedProperty())
     398        {
     399          value = rd.getExtended(propertyName);
     400        }
     401        else if (propertyName.equals(ClonableProperty.EXTERNAL_ID))
     402        {
     403          value = rd.getExternalId();
     404        }
     405        else if (propertyName.equals(ClonableProperty.NAME))
     406        {
     407          value = rd.getName();
     408        }
     409        else if (propertyName.equals(ClonableProperty.ID))
     410        {
     411          value = rd.getId();
     412        }
     413        data.append("\t").append(value);
     414      }
     415      if (!TestUtil.getSilent()) write(data.toString());
     416
     417      write("--Get cloned by external id OK");
     418    }
     419    catch (Throwable ex)
     420    {
     421      write("--Get cloned by external id FAILED");
     422      ex.printStackTrace();
     423      ok = false;
     424    }
     425    finally
     426    {
     427      if (dc != null) dc.close();
     428    }
     429  }
     430
     431 
    362432}
Note: See TracChangeset for help on using the changeset viewer.