public List<Comment> getMyComments(String categoryId) { String request = "SELECT FROM Comment WHERE category.@rid = " + categoryId; try (OObjectDatabaseTx database = getOObjectDatabaseTx()) { List<Comment> posts = database.query(new OSQLSynchQuery<Comment>(request)); return detachList(posts); } }
public Comment getCommentById(String id) { try (OObjectDatabaseTx database = getOObjectDatabaseTx()) { OIdentifiable rec = database.load(new ORecordId(id)); ODocument doc = rec.getRecord(); return oDocumentToComment(doc); } }
public void load(File... files) { int totalCount = 0, fileCount; VocabularyRepository vocabRepo = VocabularyRepository.getInstance(); OObjectDatabaseTx dbConnection = null; ODocument doc = new ODocument(); Map<String, String> baseFields, fields; try { dbConnection = vocabRepo.getInactiveDbConnection(); vocabRepo.initializeModel(true, dbConnection, this.modelClass); dbConnection.declareIntent(new OIntentMassiveInsert()); for (File file : files) { baseFields = this.buildBaseFields(); fields = this.buildFields(); try { fileCount = this.loadFile(vocabRepo, dbConnection, doc, baseFields, fields, file); totalCount += fileCount; logger.debug( String.format( "Loaded %d vocabulary model (name=%s) record(s) from file: %s", fileCount, this.modelName, file.getPath())); } catch (Exception e) { logger.error( String.format( "Unable to load vocabulary model (name=%s) file: %s", this.modelName, file), e); } } logger.info( String.format( "Loaded %d vocabulary model (name=%s) record(s) from %d file(s).", totalCount, this.modelName, files.length)); } catch (Exception e) { logger.error(String.format("Unable to load vocabulary model (name=%s).", this.modelName), e); } finally { if (dbConnection != null) { dbConnection.declareIntent(null); dbConnection.close(); } } }
@Override public void afterRegistration( final OObjectDatabaseTx db, final SchemeDescriptor descriptor, final Field field, final LuceneIndex annotation) { db.getMetadata().getIndexManager().reload(); final String property = field.getName(); final String model = descriptor.schemeClass; final String name = Objects.firstNonNull(Strings.emptyToNull(annotation.name().trim()), model + '.' + property); final OClass clazz = db.getMetadata().getSchema().getClass(model); final OIndex<?> classIndex = clazz.getClassIndex(name); final OClass.INDEX_TYPE type = OClass.INDEX_TYPE.FULLTEXT; if (!descriptor.initialRegistration && classIndex != null) { final IndexValidationSupport support = new IndexValidationSupport(classIndex, logger); support.checkTypeCompatible(type); support.checkFieldsCompatible(property); final boolean correct = support .isIndexSigns( classIndex.getConfiguration().field("algorithm"), getAnalyzer(classIndex)) .matchRequiredSigns( type, OLuceneIndexFactory.LUCENE_ALGORITHM, annotation.value().getName()); if (!correct) { support.dropIndex(db); } else { // index ok return; } } final ODocument metadata = createMetadata(annotation); SchemeUtils.command( db, "create index %s on %s (%s) %s engine %s metadata %s", name, model, property, type.name(), OLuceneIndexFactory.LUCENE_ALGORITHM, metadata.toJSON()); logger.info("Lucene fulltext index '{}' ({} [{}]) created", name, model, property); }
protected void loadDocument( OObjectDatabaseTx dbConnection, ODocument doc, String modelClassName, Map<String, String> fields) { doc.reset(); doc.setClassName(modelClassName); for (String fieldName : fields.keySet()) { doc.field(fieldName, fields.get(fieldName)); } dbConnection.getUnderlying().save(doc); }
public static void main(String[] args) { OObjectDatabaseTx db = new OObjectDatabaseTx("remote:localhost/testdb").open("root", "root"); // REGISTER THE CLASS ONLY ONCE AFTER THE DB IS OPEN/CREATED db.setAutomaticSchemaGeneration(true); db.getEntityManager().registerEntityClasses("ch.rasc.playground.orientdb.domain"); // CREATE A NEW PROXIED OBJECT AND FILL IT Account account = db.newInstance(Account.class); account.setName("Luke"); account.setSurname("Skywalker"); City rome = db.newInstance(City.class, "Rome", db.newInstance(Country.class, "Italy")); account.getAddresses().add(new Address("Residence", rome, "Piazza Navona, 1")); db.save(account); // CREATE A NEW OBJECT AND FILL IT account = new Account(); account.setName("Luke"); account.setSurname("Skywalker"); rome = new City("Rome", new Country("Italy")); account.getAddresses().add(new Address("Residence", rome, "Piazza Navona, 1")); // SAVE THE ACCOUNT: THE DATABASE WILL SERIALIZE THE OBJECT AND GIVE THE PROXIED // INSTANCE account = db.save(account); List<Account> accounts = db.query(new OSQLSynchQuery<Account>("select from Account where name like 'L%'")); for (Account acc : accounts) { Account detached = db.detach(acc, true); System.out.println(detached); System.out.println(detached.getRid()); } }