[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Object Version in Eclipse Link

I found the way to solve this issue, here is code snippet (I used named
query) , may be it helpful to somebody

1. All entity extends Persitable interface (act as marker interface)
2. Enitity which require time validity check implement interface
PersistableWithTimeValidity

DAO looks like

public interface GenericDAO<T extends Persistable> {
	
	public void create(T t);

	public T update(T t);

	@SuppressWarnings("unchecked")
	public T get(Class clas, Serializable id);

	@SuppressWarnings("unchecked")
	public void delete(Class clas, Serializable id);
	
	public void delete(T t);
	
	public List<T> findByExample(T exampleCriteria);	
	
	@SuppressWarnings("hiding")
	public <T> List<T> selectAll(Class<T> clazz,String orderClause);
	
	@SuppressWarnings("hiding")
	public <T> T findFresh(Class<T> entityClass, Object primaryKey);
	
	public <T> List<T> getNamedQuery(final Class<T> entityClass, final String
namedQuery,
			final Object... binds);
	public <T> List<T> getNamedQuery(final Class<T> entityClass, final String
namedQuery,
			final Map<String,Object> binds);
	

}

Service looks like
public interface CrudService<T extends Persistable> {

	/**
	 * Retrieve the object from db given its ID.
	 * 
	 * @param id
	 *            the id
	 * 
	 * @return the Persistable object
	 */
	@SuppressWarnings("unchecked")
	public T retrieve(Class klass, Serializable id);
	
	/**
	 * Creates a new Persistable in the system.
	 * 
	 * @param persistable
	 * 
	 * @return the persisted persistable (possibly with an ID)
	 */
	public T create(T persistable);
	

	/**
	 * Updates the persistable to db.
	 * 
	 * @param persistable
	 */
	public T update(T persistable);
	

	@SuppressWarnings("unchecked")
	public void remove(Class klass, Serializable id);
	
	
	public List<T> findByExample(T exampleCriteria);	
	
	@SuppressWarnings("hiding")
	public <T> List<T> selectAll(Class<T> clazz, String orderClause);
	
	@SuppressWarnings("hiding")
	public <T> T findFresh(Class<T> entityClass, Object primaryKey);
	
	@SuppressWarnings("hiding")
	public <T> List<T> getNamedQuery(final Class<T> entityClass, final String
namedQuery,
			final Map<String,Object> binds);
	
	public boolean isTimeValid(T persistable);

}

Time validity check :-
		// Oh yes, perform the check for time validity
		PersistableWithTimeValidity persistTv = null;
		
		String msg = "This entity cannot be checked for time validity";
		Assert.isInstanceOf(PersistableWithTimeValidity.class, persistable, msg);

//		if (persistable instanceof PersistableWithTimeValidity) {
		persistTv = (PersistableWithTimeValidity) persistable;
		Timestamp startDate = persistTv.getStartDate();
		Timestamp endDate = persistTv.getEndDate();

		Map<String, Object> keyMap = persistTv.getKeyForTimeValidity();
		Set<String> keySet = keyMap.keySet();
		Map<String,Object> mp = new LinkedHashMap<String, Object>();
		for (String key : keySet) {
			mp.put(key, keyMap.get(key));
		}
		
		mp.put("startDate", startDate);
		mp.put("endDate", endDate);
		
		Class klass = persistTv.getClass();
		String nQry =  persistTv.getClass().getSimpleName()+
NAMED_QUERY_TIMEVALIDITY;
		List<PersistableWithTimeValidity> list = null;
		list = (List<PersistableWithTimeValidity>)
genericDao.getNamedQuery(klass,nQry, mp);


Eclipse link rocks :-) (complement to team. I have been using Hibernate and
Hibernate + AndroMDA for long. Eclipse link is quicker to learn and vast
features)





Gaurav Malhotra wrote:
> 
> Aim:- To provide object versioning. Each table in the schema contain a
> column called ObjectVersion
> 
> What is an equivalent of http://www.jboss.org/envers/ in eclipse link?
> 

-- 
View this message in context: http://www.nabble.com/Object-Version-in-Eclipse-Link-tp18711728p18799981.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.