Jena: how to dynamically create JavaBeans from SPARQL results?
Is it possible to have Jena or开发者_高级运维 some other library automatically generate a Javabean for results returned from a SPARQL query? I find accessing the results via Jena's Resultset to be tiresome so I'm hoping there's a more object oriented way.
Can JenaBean help here? If I have a RDF file, how would I use Jena in conjunction with JeanBean to generate Javabeans from a Resultset?
Sure you can!
Just instanciate a Jena's Model interface and write code like the example below. This is scala.
Your code will look something like this:
val fileName = "ffff.rdf"
var in = new java.io.FileInputStream(fileName)
var model = ModelFactory.createOntologyModel.read(in, null, null)
.asInstanceOf[OntModel]
val modeMetaId = "someid"
val queryString =
"""
PREFIX sbml: <http://wikimodels.cnbc.pt/ontologies/sbml.owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?s WHERE
{ ?s rdf:type sbml:Model .
""" + "?s sbml:metaid \"" + modelMetaId + "\"^^<http://www.w3.org/2001/XMLSchema#string> } "
val l: java.util.LinkedList[SomeBean]
= Sparql.exec(model, classOf[SomeBean], queryString)
This is general enough to work with any Jena backend. I'm using SDB with postgresql behind it.
精彩评论