Java Reference
In-Depth Information
<hibernate-configuration>
<session-factory>
...
<!-- For Hibernate XML mappings -->
<!--
<mapping resource="com/apress/springenterpriserecipes/course/
Course.hbm.xml" />
-->
<!-- For JPA annotations -->
<mapping class="com.apress.springenterpriserecipes.course.Course" />
</session-factory>
</hibernate-configuration>
In the Hibernate DAO implementation, the Configuration class you used is for reading XML
mappings. If you use JPA annotations to define mapping metadata for Hibernate, you have to use its
subclass, AnnotationConfiguration , instead.
package com.apress.springenterpriserecipes.course.hibernate;
...
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateCourseDao implements CourseDao {
private SessionFactory sessionFactory;
public HibernateCourseDao() {
// For Hibernate XML mapping
// Configuration configuration = new Configuration().configure();
// For JPA annotation
Configuration configuration = new AnnotationConfiguration().configure();
sessionFactory = configuration.buildSessionFactory();
}
...
}
Persisting Objects Using JPA with Hibernate as the Engine
In addition to persistent annotations, JPA defines a set of programming interfaces for object persistence.
However, JPA is not a persistence implementation; you have to pick up a JPA-compliant engine to
provide persistence services. Hibernate can be JPA-compliant through the Hibernate EntityManager
extension module. With this extension, Hibernate can work as an underlying JPA engine to persist
objects. This lets you retain both the valuable investment in Hibernate (perhaps it's faster or handles
certain operations more to your satisfaction) and write code that is JPA-compliant and portable among
other JPA engines. This can also be a useful way to transition a code base to JPA. New code is written
strictly against the JPA APIs, and older code is transitioned to the JPA interfaces.
Search WWH ::




Custom Search