Changeset 5118


Ignore:
Timestamp:
Oct 8, 2009, 9:12:17 AM (12 years ago)
Author:
Nicklas Nordborg
Message:

References #1400: Store the item id together with annotation sets

Location:
trunk/src/core/net/sf/basedb/core
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/AnnotationSet.java

    r5056 r5118  
    118118
    119119  /**
     120    @since 2.14
     121  */
     122  static int loadItemId(org.hibernate.Session session, Item itemType, int annotationSetId)
     123  {
     124    String hql = "SELECT item.id"+
     125      " FROM "+itemType.getDataClass().getName()+" item"+
     126      " WHERE item.annotationSet = :annotationSet";
     127    org.hibernate.Query query = HibernateUtil.createQuery(session, hql);
     128    query.setInteger("annotationSet", annotationSetId);
     129    return HibernateUtil.loadData(Integer.class, query);
     130  }
     131 
     132  /**
    120133    The item this annotation set belongs to.
    121134  */
     
    138151  {
    139152    super(annotationSetData);
    140     this.item = item;   
     153    setItem(item);
    141154  }
    142155
     
    175188  {
    176189    Set<ItemProxy> using = super.getUsingItems();
    177     Annotatable item = getItem();
    178     using.add(new ItemProxy(item.getId(), item.getType()));
     190    using.add(new ItemProxy(getItemId(), getItemType()));
    179191    return using;
    180192  }
     
    242254    if (item == null)
    243255    {
    244       if (dc == null) dc = getDbControl();
    245       Item type = getItemType();
    246       String hql = "SELECT item"+
    247         " FROM "+type.getDataClass().getName()+" item"+
    248         " WHERE item.annotationSet = :annotationSet";
    249       org.hibernate.Query query = HibernateUtil.createQuery(dc.getHibernateSession(), hql);
    250       query.setEntity("annotationSet", this.getData());
    251       item = (Annotatable)dc.getItem(type.getItemClass(), HibernateUtil.loadData(type.getDataClass(), query));
     256      Integer itemId = getData().getItemId();
     257      if (itemId == null)
     258      {
     259        if (dc == null) dc = getDbControl();
     260        itemId = loadItemId(dc.getHibernateSession(), getItemType(), getId());
     261        getData().setItemId(itemId);
     262      }
     263      item = (Annotatable)getItemType().getById(dc, itemId);     
    252264    }
    253265    else if (dc != null)
     
    259271 
    260272  /**
     273    Get the ID of the item this annotation set belongs to. This may
     274    need to hit the database unless the id is already known.
     275    @since 2.14
     276  */
     277  public int getItemId()
     278  {
     279    Integer itemId = getData().getItemId();
     280    if (itemId == null)
     281    {
     282      itemId = loadItemId(getDbControl().getHibernateSession(), getItemType(), getId());
     283      getData().setItemId(itemId);
     284    }
     285    return itemId;
     286  }
     287 
     288  /**
    261289    Set the item this annotation set belongs to.
    262290  */
     
    264292  {
    265293    this.item = item;
     294    if (item.getId() != 0) getData().setItemId(item.getId());
    266295  }
    267296 
  • trunk/src/core/net/sf/basedb/core/Install.java

    r5077 r5118  
    114114    method.
    115115  */
    116   public static final int NEW_SCHEMA_VERSION = Integer.valueOf(75).intValue();
     116  public static final int NEW_SCHEMA_VERSION = Integer.valueOf(76).intValue();
    117117 
    118118  public static synchronized void createTables(boolean update, final ProgressReporter progress)
  • trunk/src/core/net/sf/basedb/core/Update.java

    r5077 r5118  
    3636
    3737import net.sf.basedb.core.data.AnnotationData;
     38import net.sf.basedb.core.data.AnnotationSetData;
    3839import net.sf.basedb.core.data.ArrayDesignData;
    3940import net.sf.basedb.core.data.ChangeHistoryData;
     
    839840    </td>
    840841  </tr>
     842  <tr>
     843    <td>76</td>
     844    <td>
     845      Added {@link AnnotationSetData#getItemId()}.
     846      The update sets the value for all existing annotation
     847      sets.
     848    </td>
     849  </tr>
    841850  </table>
    842851
     
    11281137      }
    11291138     
     1139      if (schemaVersion < 76)
     1140      {
     1141        if (progress != null) progress.display((int)(75*progress_factor), "--Updating schema version: " + schemaVersion + " -> 76...");
     1142        schemaVersion = updateToSchemaVersion76(session);
     1143      }
     1144
    11301145      sc.logout();
    11311146      if (progress != null) progress.display(100, "Database updated successfully.");
     
    25192534    return schemaVersion;
    25202535  }
     2536 
     2537  /**
     2538    Sets the {@link AnnotationSetData#getItemId()} for
     2539    all annotation sets with a null value.
     2540    @return The new schema version (=76)
     2541  */
     2542  private static int updateToSchemaVersion76(org.hibernate.Session session)
     2543    throws BaseException
     2544  {
     2545    final int schemaVersion = 76;
     2546    org.hibernate.Transaction tx = null;
     2547    try
     2548    {
     2549      tx = HibernateUtil.newTransaction(session);
     2550     
     2551      // Load all annotation sets with a null id
     2552      org.hibernate.Query query = HibernateUtil.createQuery(session,
     2553        "SELECT a FROM AnnotationSetData a WHERE a.itemId IS NULL");
     2554      List<AnnotationSetData> annotationSets =
     2555        HibernateUtil.loadList(AnnotationSetData.class, query, null);
     2556      Map<Item, org.hibernate.Query> itemQueries = new HashMap<Item, org.hibernate.Query>();
     2557      for (AnnotationSetData a : annotationSets)
     2558      {
     2559        Item itemType = Item.fromValue(a.getItemType());
     2560        org.hibernate.Query findItem = itemQueries.get(itemType);
     2561        if (findItem == null)
     2562        {
     2563          String hql = "SELECT item.id"+
     2564            " FROM "+itemType.getDataClass().getName()+" item"+
     2565            " WHERE item.annotationSet = :annotationSet";
     2566          findItem = HibernateUtil.createQuery(session, hql);
     2567          itemQueries.put(itemType, findItem);
     2568        }
     2569        findItem.setInteger("annotationSet", a.getId());
     2570        a.setItemId(HibernateUtil.loadData(Integer.class, findItem));
     2571      }
     2572 
     2573      // Update the schema version number
     2574      setSchemaVersion(session, schemaVersion);
     2575 
     2576      // Commit the changes
     2577      HibernateUtil.commit(tx);
     2578      log.info("updateToSchemaVersion76: OK");
     2579    }
     2580    catch (BaseException ex)
     2581    {
     2582      if (tx != null) HibernateUtil.rollback(tx);
     2583      log.error("updateToSchemaVersion76: FAILED", ex);
     2584      throw ex;
     2585    }
     2586    return schemaVersion;
     2587  }
     2588
    25212589 
    25222590  /**
  • trunk/src/core/net/sf/basedb/core/data/AnnotationSetData.java

    r5068 r5118  
    5858  }
    5959
     60  private Integer itemId;
     61  /**
     62    The ID if the item this annotation set is associated with. Can be null
     63    if this is not know. Use a query based on the itemType and annotation set
     64    ID to find the item in this case.
     65    @since 2.14
     66    @hibernate.property column="`item_id`" type="int" not-null="false"
     67  */
     68  public Integer getItemId()
     69  {
     70    return itemId;
     71  }
     72  public void setItemId(Integer itemId)
     73  {
     74    this.itemId = itemId;
     75  }
     76 
    6077  private Map<AnnotationTypeData, AnnotationData> annotations;
    6178  /**
Note: See TracChangeset for help on using the changeset viewer.