Created
January 7, 2012 19:26
-
-
Save pcdinh/1575726 to your computer and use it in GitHub Desktop.
Trying to use an embedded list as a tags container in OrientDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Schema | |
| schema = db.getMetadata().getSchema(); | |
| person = schema.createClass("Person"); | |
| person.createProperty("name", OType.STRING); | |
| person.createProperty("age", OType.INTEGER); | |
| person.createProperty("tags", OType.EMBEDDEDLIST, OType.STRING); | |
| // Luke | |
| ODocument luke = new ODocument(db, "Person"); | |
| luke.field("name", "Luke"); | |
| luke.field("age", 36); | |
| HashSet<String> lukeTags = new HashSet(); | |
| lukeTags.add("jedi"); | |
| lukeTags.add("cool"); | |
| luke.field("tags", lukeTags); | |
| luke.save(); | |
| // Yoda | |
| ODocument yoda = new ODocument(db, "Person"); | |
| yoda.field("name", "Yoda"); | |
| yoda.field("age", 1435); | |
| HashSet<String> yoda_tags = new HashSet(); | |
| yoda_tags.add("jedi"); | |
| yoda_tags.add("short"); | |
| yoda.field("tags", yoda_tags); | |
| yoda.save(); | |
| // Leia | |
| ODocument leia = new ODocument(db, "Person"); | |
| leia.field("name", "Leia"); | |
| leia.field("age", 34); | |
| HashSet<String> leia_tags = new HashSet(); | |
| leia_tags.add("cool"); | |
| leia_tags.add("princess"); | |
| leia.field("tags", leia_tags); | |
| leia.save(); | |
| // Han | |
| ODocument han = new ODocument(db, "Person"); | |
| han.field("name", "Leia"); | |
| han.field("age", 38); | |
| HashSet<String> han_tags = new HashSet(); | |
| han_tags.add("cool"); | |
| han_tags.add("fighter"); | |
| han.field("tags", han_tags); | |
| han.save(); | |
| // Chewbaca | |
| ODocument chewb = new ODocument(db, "Person"); | |
| chewb.field("name", "Chewbaca"); | |
| chewb.field("age", 45); | |
| HashSet<String> chewb_tags = new HashSet(); | |
| chewb_tags.add("hairy"); | |
| chewb_tags.add("fighter"); | |
| chewb.field("tags", chewb_tags); | |
| chewb.save(); | |
| // Get all fighters | |
| String qry2str = "SELECT * FROM person WHERE 'fighter' IN tags"; | |
| OSQLSynchQuery<ODocument> qry2 = new OSQLSynchQuery<ODocument>(qry2str); | |
| List<ODocument> res2 = db.query(qry2); | |
| // Wrong, returns 2 | |
| // Person@5:3{tags:[2],name:Leia,age:38} | |
| // Person@5:4{tags:[2],name:Chewbaca,age:45} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment