Created
March 27, 2015 12:02
-
-
Save koduki/e776421dc81066abd66d to your computer and use it in GitHub Desktop.
Orient DB Example
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
public class Main { | |
private OServer server; | |
public void exec() throws Exception { | |
OObjectDatabaseTx db = new OObjectDatabaseTx("memory:persondb").create(); | |
db.getEntityManager().registerEntityClass(Person.class); | |
Person nanoha = db.newInstance(Person.class); | |
nanoha.setName("Nanoha"); | |
nanoha.setAge(19); | |
db.save(nanoha); | |
Person fate = db.newInstance(Person.class, "Fate", 10); | |
db.save(fate); | |
System.out.println("--"); | |
System.out.println("show all members."); | |
for (Person person : db.browseClass(Person.class)) { | |
System.out.printf("Name:%s, Age:%d", person.getName(), person.getAge()); | |
System.out.println(); | |
} | |
System.out.println("--"); | |
System.out.println("show members under 12"); | |
List<Person> result = db.query( | |
new OSQLSynchQuery<>("select * from Person where age > 12")); | |
result.stream().forEach(p -> { | |
System.out.printf("Name:%s, Age:%d", p.getName(), p.getAge()); | |
System.out.println(); | |
}); | |
db.close(); | |
} | |
public void shutdown() throws Exception { | |
server.shutdown(); | |
} | |
public void start() throws Exception { | |
server = OServerMain.create() | |
.startup(getClass().getClassLoader().getResourceAsStream("orientdb-server-config.xml")) | |
.activate(); | |
} | |
public static void main(String[] args) throws Exception { | |
Main main = new Main(); | |
main.start(); | |
main.exec(); | |
main.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment