List of usage examples for org.hibernate Session get
Object get(String entityName, Serializable id);
From source file:au.com.nicta.ct.experiment.coordinates.CtLimits.java
License:Open Source License
protected void insertLimits(CtExperiments e) { String sql = " SELECT c.fk_coordinate_type, MIN( c.value ) as limit1, MAX( c.value ) as limit2 " + " FROM ct_images_coordinates ic " + " INNER JOIN ct_coordinates c ON ic.fk_coordinate = c.pk_coordinate" + " INNER JOIN ct_images i ON ic.fk_image = i.pk_image" + " WHERE fk_experiment = " + e.getPkExperiment() //--- for efficiency only update specific expt + " GROUP BY i.fk_experiment, fk_coordinate_type" + " ORDER BY i.fk_experiment, fk_coordinate_type"; Session session = CtSession.Current(); // 2 queries: first is a native one which is tuned to efficiently get // the PKs of the sequence, in order.. // Sorry - 1 + 3n queries where n is number of dimensions... Query query = session.createSQLQuery(sql); Iterator i = query.list().iterator(); while (i.hasNext()) { Object[] o = (Object[]) i.next(); int fkCoordinateType = ((Integer) o[0]).intValue(); int limit1 = ((Integer) o[1]).intValue(); int limit2 = ((Integer) o[2]).intValue(); sql = " SELECT min( pk_coordinate ), value " + " FROM ct_images_coordinates ic " + " INNER JOIN ct_coordinates c ON ic.fk_coordinate = c.pk_coordinate " + " INNER JOIN ct_images i on ic.fk_image = i.pk_image" + " WHERE fk_experiment = " + e.getPkExperiment()// --- for efficiency only update specific expt + " AND c.fk_coordinate_type = " + fkCoordinateType + " AND (c.value = " + limit1 + " OR c.value = " + limit2 + " ) " + " GROUP BY c.value " + " ORDER BY c.value "; Query query2 = session.createSQLQuery(sql); Iterator i2 = query2.list().iterator(); int pkCoordinate1 = -1; int pkCoordinate2 = -1; // should be 2 rows: int row = 0; while (i2.hasNext()) { Object[] o2 = (Object[]) i2.next(); int pkCoordinate = ((Integer) o2[0]).intValue(); if (row == 0) { pkCoordinate1 = pkCoordinate; } else { pkCoordinate2 = pkCoordinate; }/*from w w w . j ava 2 s. com*/ ++row; } CtCoordinates c1 = (CtCoordinates) session.get(CtCoordinates.class, pkCoordinate1); CtCoordinates c2 = null; if (limit1 == limit2) { c2 = c1; } else { c2 = (CtCoordinates) session.get(CtCoordinates.class, pkCoordinate2); } CtExperimentsAxes ea = new CtExperimentsAxes(); ea.setCtExperiments(e); ea.setCtCoordinatesByFkCoordinate1(c1); ea.setCtCoordinatesByFkCoordinate2(c2); Transaction t = session.beginTransaction(); session.save(ea); // session.persist( ea ); t.commit(); //you might even want to wrap this in another try/catch block. // session.delete( ea ); // session.save( ea ); // session.getTransaction().commit(); } // session.getTransaction().commit(); }
From source file:au.edu.anu.metadatastores.rdf.RDFService.java
License:Open Source License
/** * Update the record with the given id/* www. j a va 2 s . c o m*/ * * @param id The id */ public void update(Long id) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { Object item = session.get(Item.class, id); update(Arrays.asList((Item) item)); } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.datacommons.DataCommonsService.java
License:Open Source License
/** * Set the reverse relationships// ww w . j a v a 2s.com * * @param item The dublin core item to set the relation for * @param dublinCore the dublin core to set relations for * @param session2 The hibernate session */ protected void setReverseRelations(DublinCoreItem item, DublinCore dublinCore, Session session2) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { Query query = session .createQuery("FROM ItemAttribute WHERE attrType = :attrType AND attrValue = :attrValue"); query.setParameter("attrType", StoreAttributes.RELATION_VALUE); dublinCore.getIdentifiers(); String identifier = null; for (String id : dublinCore.getIdentifiers()) { if (id.startsWith(StoreProperties.getProperty("relation.id.format.datacommons"))) { identifier = id; break; } } if (identifier != null) { query.setParameter("attrValue", identifier); @SuppressWarnings("unchecked") List<ItemAttribute> relatedAttributes = query.list(); LOGGER.debug("Number of reverse relationships: {}", relatedAttributes.size()); ItemRelationId id = null; for (ItemAttribute relatedAttribute : relatedAttributes) { String relationText = relatedAttribute.getItemAttribute().getAttrValue(); String[] relationParts = getRelationParts(relationText); String relationType = getRelationType(relationParts[0]); id = new ItemRelationId(relatedAttribute.getItem().getIid(), relationType, item.getIid()); ItemRelation relation = (ItemRelation) session.get(ItemRelation.class, id); if (relation == null) { LOGGER.debug("Does not have relation"); ItemRelation newRelation = new ItemRelation(); newRelation.setId(id); item.getItemRelationsForRelatedIid().add(newRelation); } else { LOGGER.debug("has relation"); } } } } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.grants.GrantService.java
License:Open Source License
/** * Add the people involved with the grant * /*from ww w . j a va 2s .co m*/ * @param item The grant item to add people to * @param grant The grant that holds the people who need to be associated * @param session The session */ private void associatePeople(GrantItem item, Grant grant, Session session2) { //TODO add the removing of people who are no longer associated Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { LOGGER.debug("Associate People"); for (Person person : grant.getAssociatedPeople()) { PersonItem personItem = personService_.getPersonItem(person.getExtId()); ItemRelationId relationId = null; if (personItem != null) { if (grant.getFirstInvestigator() != null && person.getExtId().equals(grant.getFirstInvestigator().getExtId())) { relationId = new ItemRelationId(item.getIid(), "hasPrincipalInvestigator", personItem.getIid()); } else { relationId = new ItemRelationId(item.getIid(), "hasAssociationWith", personItem.getIid()); } ItemRelation relation = (ItemRelation) session.get(ItemRelation.class, relationId); if (relation == null) { LOGGER.debug("Adding Relation: {}, {}, {}", relationId.getIid(), relationId.getRelatedIid(), relationId.getRelationValue()); relation = new ItemRelation(); relation.setId(relationId); item.getItemRelationsForIid().add(relation); } else { LOGGER.debug("Found Relation: {}, {}, {}", relationId.getIid(), relationId.getRelatedIid(), relationId.getRelationValue()); } } else { LOGGER.error("Error finding and saving person with an id of: {}", person.getExtId()); } } } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.misc.RelationService.java
License:Open Source License
/** * Updates the relation with a confirmation or denial of the relationship * // w ww. ja v a 2s . co m * @param relation The relation to confirm or deny * @param isRelation Whether the relation is one or not */ public void confirmOrDenyRelation(Relation relation, Boolean isRelation) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { session.beginTransaction(); PotentialRelationId id = new PotentialRelationId(relation.getIid(), relation.getRelationValue(), relation.getRelatedIid()); PotentialRelation potentialRelation = (PotentialRelation) session.get(PotentialRelation.class, id); if (potentialRelation == null) { potentialRelation = new PotentialRelation(); potentialRelation.setId(id); } potentialRelation.setRequireCheck(Boolean.FALSE); potentialRelation.setIslink(isRelation); session.merge(potentialRelation); ItemRelationId itemRelationId = new ItemRelationId(id.getIid(), id.getRelationValue(), id.getRelatedIid()); ItemRelation itemRelation = (ItemRelation) session.get(ItemRelation.class, itemRelationId); if (isRelation == Boolean.TRUE) { if (itemRelation == null) { itemRelation = new ItemRelation(); itemRelation.setId(itemRelationId); itemRelation.setUserUpdated(Boolean.TRUE); session.save(itemRelation); } else { itemRelation.setUserUpdated(Boolean.TRUE); session.merge(itemRelation); } } else { if (itemRelation != null) { session.delete(itemRelation); } } session.getTransaction().commit(); } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.misc.RelationService.java
License:Open Source License
/** * Get the item for the given item id/*from ww w .j av a 2 s .c o m*/ * * @param iid The item id * @return The item */ public Item getItem(Long iid) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { session.enableFetchProfile("item-with-attributes"); Item item = (Item) session.get(Item.class, iid); return item; } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.search.SearchService.java
License:Open Source License
/** * Get the item by id/*w w w .j a va 2s . com*/ * * @param id The id * @return The item object */ public Object getItemById(Long id) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { Object item = session.get(Item.class, id); return ItemResolver.resolve(item); } finally { session.close(); } }
From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.ConfigDaoTester.java
License:Open Source License
@Test public void testCreate() { String key = "testkey"; String val = "testval"; Config conf = this.dao.create(key, val); assertEquals(key, conf.getKey());/*from w w w. j a va2s .c o m*/ assertEquals(val, conf.getValue()); assertTrue(conf.getId() > 0); this.dao.closeSession(); Session ses = DataAccessActivator.getNewSession(); Config loaded = (Config) ses.get(Config.class, conf.getId()); assertNotNull(loaded); assertEquals(conf.getId(), loaded.getId()); assertEquals(conf.getKey(), loaded.getKey()); assertEquals(conf.getValue(), loaded.getValue()); ses.beginTransaction(); ses.delete(loaded); ses.getTransaction().commit(); }
From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.GenericDaoTester.java
License:Open Source License
/** * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.GenericDao#persist(java.lang.Object)}. *//*from w w w . j av a2 s . c om*/ public void testPersist() { String key = "persist_test_key"; String val = "persist_test_val"; Config conf = new Config(key, val); conf = this.dao.persist(conf); assertEquals(key, conf.getKey()); assertEquals(val, conf.getValue()); assertTrue(conf.getId() > 0); this.dao.closeSession(); Session ses = DataAccessActivator.getNewSession(); Config loaded = (Config) ses.get(Config.class, conf.getId()); assertNotNull(loaded); assertEquals(conf.getId(), loaded.getId()); assertEquals(conf.getKey(), loaded.getKey()); assertEquals(conf.getValue(), loaded.getValue()); ses.beginTransaction(); ses.delete(loaded); ses.getTransaction().commit(); }
From source file:au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.tests.GenericDaoTester.java
License:Open Source License
/** * Test method for {@link au.edu.uts.eng.remotelabs.schedserver.dataaccess.dao.GenericDao#delete(java.lang.Object)}. */// w w w .j av a 2 s . c om public void testDelete() { Session ses = DataAccessActivator.getNewSession(); ses.beginTransaction(); Serializable id = ses.save(new Config("delete_conf_key", "delete_conf_val")); ses.getTransaction().commit(); ses.close(); ses = DataAccessActivator.getNewSession(); assertNotNull(ses.get(Config.class, id)); ses.close(); this.dao.delete(this.dao.get(id)); ses = DataAccessActivator.getNewSession(); assertNull(ses.get(Config.class, id)); ses.close(); }