Hi,

I'm using orientdb-object-2.1.9. With an object returned from database (after 
save or find), accessing the value using any getter method shows 
ODatabaseException, that Database is not set in current thread.

Here is a test case showing the issue: 
https://gist.github.com/prashantbhat/e8b8ae8138d5bf87519d


Please let me know, if the API should be used in a different way or if any 
other configuration I'm missing here.


import javax.persistence.Id;
import javax.persistence.Version;

import com.orientechnologies.orient.core.db.OPartitionedDatabasePool;
import com.orientechnologies.orient.object.db.OObjectDatabaseTx;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ObjectDatabaseTest {
    private MyObjectAuthorRepository repository;
    
    @Before
    public void setUp() {
        repository = new MyObjectAuthorRepository();
    }
    
    @After
    public void tearDown() {
        repository.close();
    }
    
    @Test
    public void testAuthor() {
        Author savedAuthor = repository.saveAuthor(new Author("Author Name"));
        // a detached object also fails when accessing the property
        // Assert.assertEquals("Author Name", savedAuthor.getAuthorName());
        Author author = repository.findAuthor();
        Assert.assertEquals("Author Name", author.getAuthorName());
    }
    
    class MyObjectAuthorRepository {
        private final OPartitionedDatabasePool pool;
    
        public MyObjectAuthorRepository() {
            pool = new OPartitionedDatabasePool("memory:test", "admin", 
"admin");
            pool.setAutoCreate(true);
            try (OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire())) {
                db.setAutomaticSchemaGeneration(true);
                db.getEntityManager().registerEntityClass(Author.class);
            }
        }
    
        private Author saveAuthor(Author author) {
            OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire());
            try {
                db.begin();
                Author savedAuthor = db.save(author);
                db.commit();
                return db.detach(savedAuthor);
            } catch (Exception ex) {
                db.rollback();
                throw ex;
            } finally {
                db.close();
            }
        }
    
        public Author findAuthor() {
            try (OObjectDatabaseTx db = new OObjectDatabaseTx(pool.acquire())) {
                return db.browseClass(Author.class).next();
            }
        }
    
        public void close() {
            pool.close();
        }
    }
    
    @javax.persistence.Entity
    public class Author {
        @Id
        private String id;
        @Version
        private Long version;
        private String authorName;
    
        public Author() {
        }
    
        public Author(String authorName) {
            this.authorName = authorName;
        }
    
        public String getId() {
            return id;
        }
    
        public Long getVersion() {
            return version;
        }
    
        public String getAuthorName() {
            return authorName;
        }
    
        public void setAuthorName(String authorName) {
            this.authorName = authorName;
        }
    }
}


PS, I had posted this question on stack overflow[1], and sorry if this is not 
the right forum. Please inform the right forum to ask questions on using 
orientdb-object api.


Thanks


[1] 
http://stackoverflow.com/questions/34732521/property-access-of-the-returned-object-throws-odatabaseexception-database-not-s

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to