Changeset 5118
- Timestamp:
- Oct 8, 2009, 9:12:17 AM (14 years ago)
- 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 118 118 119 119 /** 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 /** 120 133 The item this annotation set belongs to. 121 134 */ … … 138 151 { 139 152 super(annotationSetData); 140 this.item = item;153 setItem(item); 141 154 } 142 155 … … 175 188 { 176 189 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())); 179 191 return using; 180 192 } … … 242 254 if (item == null) 243 255 { 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); 252 264 } 253 265 else if (dc != null) … … 259 271 260 272 /** 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 /** 261 289 Set the item this annotation set belongs to. 262 290 */ … … 264 292 { 265 293 this.item = item; 294 if (item.getId() != 0) getData().setItemId(item.getId()); 266 295 } 267 296 -
trunk/src/core/net/sf/basedb/core/Install.java
r5077 r5118 114 114 method. 115 115 */ 116 public static final int NEW_SCHEMA_VERSION = Integer.valueOf(7 5).intValue();116 public static final int NEW_SCHEMA_VERSION = Integer.valueOf(76).intValue(); 117 117 118 118 public static synchronized void createTables(boolean update, final ProgressReporter progress) -
trunk/src/core/net/sf/basedb/core/Update.java
r5077 r5118 36 36 37 37 import net.sf.basedb.core.data.AnnotationData; 38 import net.sf.basedb.core.data.AnnotationSetData; 38 39 import net.sf.basedb.core.data.ArrayDesignData; 39 40 import net.sf.basedb.core.data.ChangeHistoryData; … … 839 840 </td> 840 841 </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> 841 850 </table> 842 851 … … 1128 1137 } 1129 1138 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 1130 1145 sc.logout(); 1131 1146 if (progress != null) progress.display(100, "Database updated successfully."); … … 2519 2534 return schemaVersion; 2520 2535 } 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 2521 2589 2522 2590 /** -
trunk/src/core/net/sf/basedb/core/data/AnnotationSetData.java
r5068 r5118 58 58 } 59 59 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 60 77 private Map<AnnotationTypeData, AnnotationData> annotations; 61 78 /**
Note: See TracChangeset
for help on using the changeset viewer.