Search in sources :

Example 1 with Morphia

use of org.mongodb.morphia.Morphia in project morphia by mongodb.

the class EntityScannerTest method testScanning.

@Test
public void testScanning() throws Exception {
    final Morphia m = new Morphia();
    assertFalse(m.isMapped(E.class));
    new EntityScanner(m, Predicates.equalTo(E.class.getName() + ".class"));
    assertTrue(m.isMapped(E.class));
    assertFalse(m.isMapped(F.class));
    new EntityScanner(m, new Predicate<String>() {

        @Override
        public boolean apply(final String input) {
            return input.startsWith(EntityScannerTest.class.getPackage().getName());
        }
    });
    assertTrue(m.isMapped(F.class));
}
Also used : Morphia(org.mongodb.morphia.Morphia) Test(org.junit.Test)

Example 2 with Morphia

use of org.mongodb.morphia.Morphia in project morphia by mongodb.

the class TestBase method setUp.

@Before
public void setUp() {
    final MongoClient mongoClient;
    try {
        mongoClient = new MongoClient();
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    mongoClient.dropDatabase("morphia_test");
    morphia = new Morphia();
    this.db = mongoClient.getDB("morphia_test");
    this.ds = this.morphia.createDatastore(mongoClient, this.db.getName());
}
Also used : MongoClient(com.mongodb.MongoClient) Morphia(org.mongodb.morphia.Morphia) Before(org.junit.Before)

Example 3 with Morphia

use of org.mongodb.morphia.Morphia in project morphia by mongodb.

the class Employee method main.

public static void main(final String[] args) throws UnknownHostException {
    final Morphia morphia = new Morphia();
    // tell morphia where to find your classes
    // can be called multiple times with different packages or classes
    morphia.mapPackage("org.mongodb.morphia.example");
    // create the Datastore connecting to the database running on the default port on the local host
    final Datastore datastore = morphia.createDatastore(new MongoClient(), "morphia_example");
    datastore.getDB().dropDatabase();
    datastore.ensureIndexes();
    final Employee elmer = new Employee("Elmer Fudd", 50000.0);
    datastore.save(elmer);
    final Employee daffy = new Employee("Daffy Duck", 40000.0);
    datastore.save(daffy);
    final Employee pepe = new Employee("Pepé Le Pew", 25000.0);
    datastore.save(pepe);
    elmer.getDirectReports().add(daffy);
    elmer.getDirectReports().add(pepe);
    datastore.save(elmer);
    Query<Employee> query = datastore.find(Employee.class);
    final List<Employee> employees = query.asList();
    Assert.assertEquals(3, employees.size());
    List<Employee> underpaid = datastore.find(Employee.class).filter("salary <=", 30000).asList();
    Assert.assertEquals(1, underpaid.size());
    underpaid = datastore.find(Employee.class).field("salary").lessThanOrEq(30000).asList();
    Assert.assertEquals(1, underpaid.size());
    final Query<Employee> underPaidQuery = datastore.find(Employee.class).filter("salary <=", 30000);
    final UpdateOperations<Employee> updateOperations = datastore.createUpdateOperations(Employee.class).inc("salary", 10000);
    final UpdateResults results = datastore.update(underPaidQuery, updateOperations);
    Assert.assertEquals(1, results.getUpdatedCount());
    final Query<Employee> overPaidQuery = datastore.find(Employee.class).filter("salary >", 100000);
    datastore.delete(overPaidQuery);
}
Also used : MongoClient(com.mongodb.MongoClient) Morphia(org.mongodb.morphia.Morphia) Datastore(org.mongodb.morphia.Datastore) UpdateResults(org.mongodb.morphia.query.UpdateResults)

Example 4 with Morphia

use of org.mongodb.morphia.Morphia in project querydsl by querydsl.

the class MongodbSerializerTest method before.

@Before
public void before() {
    serializer = new MorphiaSerializer(new Morphia());
    entityPath = new PathBuilder<Object>(Object.class, "obj");
    title = entityPath.getString("title");
    year = entityPath.getNumber("year", Integer.class);
    gross = entityPath.getNumber("gross", Double.class);
    longField = entityPath.getNumber("longField", Long.class);
    shortField = entityPath.getNumber("shortField", Short.class);
    byteField = entityPath.getNumber("byteField", Byte.class);
    floatField = entityPath.getNumber("floatField", Float.class);
    date = entityPath.getDate("date", Date.class);
    dateTime = entityPath.getDateTime("dateTime", Timestamp.class);
}
Also used : MorphiaSerializer(com.querydsl.mongodb.morphia.MorphiaSerializer) Morphia(org.mongodb.morphia.Morphia) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Timestamp(java.sql.Timestamp) Date(java.util.Date) Before(org.junit.Before)

Aggregations

Morphia (org.mongodb.morphia.Morphia)4 MongoClient (com.mongodb.MongoClient)2 Before (org.junit.Before)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 MorphiaSerializer (com.querydsl.mongodb.morphia.MorphiaSerializer)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 Test (org.junit.Test)1 Datastore (org.mongodb.morphia.Datastore)1 UpdateResults (org.mongodb.morphia.query.UpdateResults)1