@Test public void testMotivationBookmarking() throws Exception { // Create test annotation Annotation annotation = new Annotation(); // Create and add motivation Motivation motivation = new Bookmarking(); annotation.setMotivatedBy(motivation); // Persist annotation connection.addObject(annotation); // Query persisted object List<Bookmarking> result = connection.getObjects(Bookmarking.class).asList(); // Tests assertEquals(1, result.size()); assertEquals(motivation.getResource().toString(), result.get(0).getResource().toString()); }
/** * Imports the instance into the RDF store if not previously imported. If an object with the same * Resource identifier has already been imported into the store during through this connection, * the Resource identifier is returned and the object is not imported. * * @see #addObject(Resource, Object) * @return the given instance's {@link Resource} identifier or {@link Literal} representation */ public Value addObject(Object instance) throws RepositoryException { if (instance instanceof RDFObjectBehaviour) { RDFObjectBehaviour support = (RDFObjectBehaviour) instance; Object entity = support.getBehaviourDelegate(); if (entity != instance) return addObject(entity); } if (instance instanceof RDFObject) { if (((RDFObject) instance).getObjectConnection() == this) return ((RDFObject) instance).getResource(); } else { if (of.isDatatype(instance.getClass())) return of.createLiteral(instance); } Class<?> type = instance.getClass(); if (RDFObject.class.isAssignableFrom(type) || isEntity(type)) { Resource resource = assignResource(instance); if (!isAlreadyMerged(resource)) { addObject(resource, instance); } return resource; } return of.createLiteral(instance); }
/** Imports the entity into the RDF store using the given handle. */ public void addObject(Resource resource, Object entity) throws RepositoryException { if (entity instanceof RDFObjectBehaviour) { RDFObjectBehaviour support = (RDFObjectBehaviour) entity; Object delegate = support.getBehaviourDelegate(); if (delegate != entity) { addObject(resource, delegate); return; } } synchronized (merged) { merged.add(resource); } boolean autoCommit = isAutoCommit(); if (autoCommit) { setAutoCommit(false); } try { Class<?> proxy = entity.getClass(); Set<URI> list = getTypes(proxy, new HashSet<URI>(4)); for (URI type : list) { types.addTypeStatement(resource, type); } Object result = of.createObject(resource, list); if (result instanceof Mergeable) { ((Mergeable) result).merge(entity); } if (autoCommit) { setAutoCommit(true); } cachedObjects.remove(resource); } finally { if (autoCommit && !isAutoCommit()) { rollback(); setAutoCommit(true); } } }
/** Imports the entity into the RDF store using the given URI. */ public void addObject(String uri, Object entity) throws RepositoryException { addObject(getValueFactory().createURI(uri), entity); }