Oracle ADF与Hibernate集成问询:从Hibernate/JPA实体读取数据至ADF视图对象
Great question—integrating Hibernate/JPA with Oracle ADF is totally feasible without relying on EJB, and I’ve worked through a few practical approaches that get the job done. Let’s break down how to pull data from your Hibernate entities into ADF View Objects (VOs) directly:
Core Approach: Custom Programmatic View Object
Instead of using ADF’s standard Entity Object (EO)-backed VOs, you’ll create a custom VO that leverages Hibernate’s SessionFactory (or JPA’s EntityManager) to fetch and map data. Here’s a step-by-step guide:
1. Set Up Hibernate/JPA in Your ADF Project
- Add Hibernate dependencies (like
hibernate-core,hibernate-entitymanager) to your ADF project. In JDeveloper, use the Libraries and Classpath settings to include these JARs, or add them to yourpom.xmlif using Maven. - Configure your
hibernate.cfg.xml(orpersistence.xmlfor JPA) with entity mappings, database connection details, and session factory settings. Ensure this file lives in your project’s classpath (e.g.,src/main/resources).
2. Create a Custom View Object Class
Create a new ADF View Object (select Programmatic mode, no Entity Object attached) and override key methods to integrate Hibernate:
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import oracle.jbo.server.ViewObjectImpl; import oracle.jbo.server.ViewRowImpl; import java.util.List; public class HibernateVOImpl extends ViewObjectImpl { private SessionFactory sessionFactory; @Override protected void create() { super.create(); // Initialize Hibernate SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); } @Override protected void executeQuery() { super.executeQuery(); // Clear existing rows to avoid duplicates getRowSetIterator().closeRowSetIterator(); removeAllRows(); // Fetch data from Hibernate entity try (Session session = sessionFactory.openSession()) { List<YourHibernateEntity> entities = session.createQuery( "from YourHibernateEntity", YourHibernateEntity.class ).list(); // Map entity fields to VO attributes for (YourHibernateEntity entity : entities) { ViewRowImpl row = (ViewRowImpl) createRow(); row.setAttribute("EntityId", entity.getId()); row.setAttribute("EntityName", entity.getName()); // Add mappings for other fields as needed insertRow(row); } } catch (Exception e) { throw new RuntimeException("Failed to fetch data from Hibernate: " + e.getMessage(), e); } } }
3. Align VO Attributes with Hibernate Entity Fields
- In your VO’s definition, create attributes that match the fields in your Hibernate entity (e.g.,
EntityIdforLong id,EntityNameforString name). - Ensure attribute data types match exactly to avoid mapping errors.
4. Integrate with ADF Application Module and View Layer
- Add your custom VO to your ADF Application Module.
- Bind it to your JSF page like any standard ADF VO—use components like
af:tableoraf:inputTextto display the data from your Hibernate entities.
Alternative: JPA EntityManager Approach
If you prefer JPA over raw Hibernate, replace the Hibernate Session code with JPA’s EntityManager:
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; // ... other imports @Override protected void executeQuery() { super.executeQuery(); removeAllRows(); EntityManagerFactory emf = Persistence.createEntityManagerFactory("your-persistence-unit-name"); EntityManager em = emf.createEntityManager(); List<YourJpaEntity> entities = em.createQuery( "SELECT e FROM YourJpaEntity e", YourJpaEntity.class ).getResultList(); // Map to VO rows same as above em.close(); emf.close(); }
Key Considerations
- Transaction Management: Since you’re bypassing ADF BC’s built-in transactions, handle Hibernate/JPA transactions manually (use
session.beginTransaction()/commit()orem.getTransaction()for write operations). - Performance: For large datasets, add pagination with
setFirstResult()andsetMaxResults()in your Hibernate/JPA queries to avoid loading all data into memory. - Caching: Use Hibernate’s second-level cache wisely to boost performance, but ensure it’s configured to avoid stale data in your ADF views.
- Maintenance: Keep in mind you’ll be maintaining two persistence layers (ADF BC and Hibernate/JPA). If possible, evaluate if standardizing on one layer makes sense for your project.
内容的提问来源于stack exchange,提问作者Atul

