Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] How to use EclipseLink Native API with JPA

Your DAO's usage of the EclipseLink native API looks fine.  You should not
have to worry about weaving or other such issues.  Although, since you are
using JBoss you may want to use static weaving for any lazy OneToOne or
ManyTOne's.

Since you are using JTA, calling commit() on the UnitOfWork will have no
effect, as you need to commit the JTA transaction.  Also your clone() will
not really clone an object, register will return the managed copy of the
object, there is a copyObject() API you may be able to use to copy and
object.



gsilverman wrote:
> 
> I have a native JPA application and want to incorporate some features of
> Eclipselink, such as query by example, unit-of-work and expressions. I've
> created a utility facade to encapsulate these features as follows:
> 
> public class BaseDAO {
> 	
> 	@PersistenceContext
> 	private EntityManager em;
> 	
> 	private boolean isExternal = false;	
> 		
> 	public boolean isExternal() {
> 		return isExternal;
> 	}
> 
> 	public Session getSession(){
> 		JpaEntityManager emImpl = (JpaEntityManager)em.getDelegate();
> 		return emImpl.getSession();
> 	}
> 		
> 	public UnitOfWork getUnitOfWork(){
> 		UnitOfWork uow = getSession().getActiveUnitOfWork();
> 		if(uow == null){
> 			uow = getSession().acquireUnitOfWork();
> 			if(uow == null)
> 				throw new RuntimeException("No transaction started");
> 			
> 		}else
> 			isExternal = true;
> 		
> 		return uow;
> 	}
> 	
> 	public Object clone(Object entity){
> 		return getUnitOfWork().registerObject(entity);		
> 	}
> 		
> 	public void commit(){
> 		getUnitOfWork().commit();
> 	}
> 	
> 	@SuppressWarnings("unchecked")
> 	public List readAll(Class clazz, Expression expression ){
> 		return (List)getUnitOfWork().readAllObjects(clazz,expression);
> 	}
> 	
> 	@SuppressWarnings("unchecked")
> 	public List readAll(Class clazz){
> 		return (List)getUnitOfWork().readAllObjects(clazz);
> 	}
> 	
> 	@SuppressWarnings("unchecked")
> 	public List readAll(Class clazz, Call call){
> 		return (List)getUnitOfWork().readAllObjects(clazz, call);
> 	}
> 		
> }
> 
> My persistence.xml is pretty basic:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence";
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
> xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
> http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd";>
> 	<persistence-unit name="dyltPersistenceUnit">
> 	   <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
> 	   <jta-data-source>java:/DyltDS</jta-data-source>
> 	   <mapping-file>META-INF/orm-messages.xml</mapping-file>
> 	   <mapping-file>META-INF/orm-allcode.xml</mapping-file>
> 	   <mapping-file>META-INF/orm-zip.xml</mapping-file>
> 	   <properties>
>         <property name="eclipselink.target-server" value="JBoss"/>
>         <property name="eclipselink.logging.level" value="FINEST"/>
>         <property name="eclipselink.cache.size.default" value="500"/>
>         <property name="eclipselink.cache.type.default" value="Full"/>        
>        </properties>
> 	</persistence-unit>	
> </persistence>
> 
> 
> Does anyone have experience combining the two? Do I have to specifically
> configure Eclipselink caching to use unit-of-work and some of the other
> features. And what about weaving? Is it required to use, say, 
> lazy-loading or any other specific Eclipselink capabilities. I mean, I
> thought native JPA can handle lazy loading out-of-the box. The
> documentation on using JPA and Eclipslink is under construction, so any
> help out there would be appreciated. 
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/How-to-use-EclipseLink-Native-API-with-JPA-tp19761716p19779646.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top