I want to replace the javax.persistence.EntityManager implementation


#1

I want to move my applications to couchbase.

Many of those applications use jpa/Hibernate and implement the javax.persistence.EntityManager Interface.

I have started work and the following class is my implementation so far.

My question is, do you have an example of another similar implementation that would save me the time for re-inventing this wheel?

package com.smart.soa.model;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.metamodel.Metamodel;
import javax.swing.Spring;

import com.couchbase.client.CouchbaseClient;
import com.google.gson.Gson;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class CouchManager implements EntityManager {
// (Subset) of nodes in the cluster to establish a connection
private List hosts = null;
// Name of the Bucket to connect to
private String bucket = “default”;

    // Password of the bucket (empty) string if none
    private String password = "";

    private CouchbaseClient client = null;

    private String idStr = "";

    private Gson gson = null;

public String getIdStr() {
	return idStr;
}

public void setIdStr(String idStr) {
	this.idStr = idStr;
}

public CouchManager() {
	// Initialize
	this.hosts = new ArrayList<URI>();
	gson = new Gson();

    // Add one or more nodes of your cluster (exchange the IP with yours)
    hosts.add(URI.create("http://127.0.0.1:8091/pools"));

	// Connect to the Cluster
    try {
		client = new CouchbaseClient(hosts, bucket, password);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

public CouchManager(CouchPersistedI entity) {
	// Initialize
	gson = new Gson();
            this.hosts = new ArrayList<URI>();
	
       // Add one or more nodes of your cluster (exchange the IP with yours)
       hosts.add(URI.create("http://127.0.0.1:8091/pools"));

	//set the idstr
	this.idStr = entity.getIdStr();
	 try {
		client = new CouchbaseClient(hosts, bucket, password);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

//=============== Couchbase Properties ====================

public List<URI> getHosts() {
	return hosts;
}

public void setHosts(List<URI> hosts) {
	this.hosts = hosts;
}

public String getBucket() {
	return bucket;
}

public void setBucket(String bucket) {
	this.bucket = bucket;
}

public String getPassword() {
	return password;
}

public void setPassword(String password) {
	this.password = password;
}

public CouchbaseClient getClient() {
	return client;
}

public void setClient(CouchbaseClient client) {
	this.client = client;
}

//===================================EntityManager Interface implementations =============================

public void close() {
	// shutdown
	client.shutdown();
}

public boolean contains(Object arg0, String idStrArg) {
	this.idStr = idStrArg;
	return contains(arg0);
}

public boolean contains(Object arg0) {
	// idStr must be set prior to calling contains
	Object stored = client.get(idStr);
	return (stored == arg0);
}

public Query createNamedQuery(String arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO createNamedQuery1");
	return null;
}

public <T> TypedQuery<T> createNamedQuery(String arg0, Class<T> arg1) {
	// TODO Auto-generated method stub
	System.out.println("TODO createNamedQuery2");
	return null;
}

public Query createNativeQuery(String arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO createNativedQuery1");
	return null;
}

public Query createNativeQuery(String arg0, Class arg1) {
	// TODO Auto-generated method stub
	System.out.println("TODO createNativeQuery2");
	return null;
}

public Query createNativeQuery(String arg0, String arg1) {
	// TODO Auto-generated method stub
	System.out.println("TODO createNativeQuery3");
	return null;
}

public Query createQuery(String arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO createQuery1");
	return null;
}

public <T> TypedQuery<T> createQuery(CriteriaQuery<T> arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO createQuery2");
	return null;
}

public void detach(Object arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO detach");
}

@SuppressWarnings("unchecked")
public <T> T find(Class<T> arg0, Object arg1) {
	// set idstr
	Long id = (Long) arg1;
	String idStr = id.toString();
	System.out.println("TODO find");
	return (T) gson.fromJson(client.get(idStr).toString(), arg0);
}

public <T> T find(Class<T> arg0, Object arg1, Map<String, Object> arg2) {
	// TODO Auto-generated method stub
	System.out.println("TODO find2");
	return null;
}

public <T> T find(Class<T> arg0, Object arg1, LockModeType arg2) {
	// TODO Auto-generated method stub
	System.out.println("TODO find3");
	return null;
}

public <T> T find(Class<T> arg0, Object arg1, LockModeType arg2,
		Map<String, Object> arg3) {
	// TODO Auto-generated method stub
	System.out.println("TODO find4");
	return null;
}

public CriteriaBuilder getCriteriaBuilder() {
	// TODO Auto-generated method stub
	System.out.println("TODO getCriteriaBuilder");
	return null;
}

public Object getDelegate() {
	// TODO Auto-generated method stub
	System.out.println("TODO getDelegate");
	return null;
}

public EntityManagerFactory getEntityManagerFactory() {
	// TODO Auto-generated method stub
	System.out.println("TODO getEntityManagerFactory()");
	return null;
}

public FlushModeType getFlushMode() {
	// TODO Auto-generated method stub
	System.out.println("TODO getFlushMode");
	return null;
}

public LockModeType getLockMode(Object arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO getLockMode");
	return null;
}

public Metamodel getMetamodel() {
	// TODO Auto-generated method stub
	System.out.println("TODO getMetaModel");
	return null;
}

public Map<String, Object> getProperties() {
	// TODO Auto-generated method stub
	System.out.println("TODO getProperties");
	return null;
}

public <T> T getReference(Class<T> arg0, Object arg1) {
	// TODO Auto-generated method stub
	System.out.println("TODO getReference");
	return null;
}

public EntityTransaction getTransaction() {
	// TODO Auto-generated method stub
	System.out.println("TODO getTransaction");
	return null;
}

public boolean isOpen() {
	// TODO Auto-generated method stub
	System.out.println("TODO isOpen");
	return false;
}

public void joinTransaction() {
	// TODO Auto-generated method stub
	System.out.println("TODO joinTransactoin");
}

public void lock(Object arg0, LockModeType arg1) {
	// TODO Auto-generated method stub
	System.out.println("TODO lock");
}

public void lock(Object arg0, LockModeType arg1, Map<String, Object> arg2) {
	// TODO Auto-generated method stub
	System.out.println("TODO lock2");
}

public <T> T merge(T arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO merge");
	return null;
}

public void persist(Object entity, String idStrArg) {
	this.idStr = idStrArg;
	persist(entity);
}

public void persist(Object entity) {
	//idStr must be set prior to persist
	if (idStr.isEmpty()) return;
	
	// save to couchbase
	 try {
			client.set(idStr, gson.toJson(entity)).get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}

public void refresh(Object arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO refresh");
}

public void refresh(Object arg0, Map<String, Object> arg1) {
	// TODO Auto-generated method stub
	System.out.println("TODO refresh2");
}

public void refresh(Object arg0, LockModeType arg1) {
	// TODO Auto-generated method stub
	System.out.println("TODO refresh3");
}

public void refresh(Object arg0, LockModeType arg1, Map<String, Object> arg2) {
	// TODO Auto-generated method stub
	System.out.println("TODO refresh4");
}

public void remove(Object arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO remove");
}

public void setFlushMode(FlushModeType arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO setFlushMode");
}

public void setProperty(String arg0, Object arg1) {
	// TODO Auto-generated method stub
	System.out.println("TODO setProperty");
}

public <T> T unwrap(Class<T> arg0) {
	// TODO Auto-generated method stub
	System.out.println("TODO unwrap");
	return null;
}

public void clear() {
	// TODO Auto-generated method stub
	System.out.println("TODO clear");
}

public <T> TypedQuery<T> createQuery(String arg0, Class<T> arg1) {
	// TODO Auto-generated method stub
	System.out.println("TODO createQueryx");
	return null;
}

public void flush() {
	// TODO Auto-generated method stub
	System.out.println("TODO Flush");	
}

}

Once I have implemented all these equivalents I will share this back to the community if I get some assistance… :wink:


#2

Hello,

It is a good idea but not a simple project. On my side I have started to work on a Hibernate OGM implementation for Couchbase: https://github.com/tgrall/hibernate-ogm but still lot of work to do. (need to get back to it when I am not traveling.

I will be pleased to help you if you want, but don’t you think it will be easier to learn how to use Couchbase “natively” ? You can see the steps here:
http://www.osintegrators.com/opensoftwareintegrators|HowtoteachaJavaEEappnewNoSQLtricks

Regards
Tug
@tgrall


#3

Tug thanks,
“Easier” Well if all I wanted to do was learn Couchbase, yes. But I have already learned enough and done POC to know I want to migrate to Couchbase and away from jpa/Hibernate.

So I have a bunch of applications that already have jpa/hibernate and the classes already use the javax.persistence.EntityManager interface. So “Easier” means if I write my own extended EntityManager then I can replace the jpa/hibernate with my couchbase EntityManager without changing any or at least no much in my code for all those applications that use jpa/entitymanager today.

Ollie