Starting a Java Persistence project Object/relational mapping metadata, (Top web site)
Starting a Java Persistence project Object/relational mapping metadata, but here we want to show how you can use a Hibernate API in your JPA application, when needed. Obviously, importing a Hibernate API into your code makes porting the code to a different JPA provider more difficult. Hence, it becomes critically important to isolate these parts of your code properly, or at least to document why and when you used a native Hibernate feature. You can fall back to Hibernate APIs from their equivalent JPA interfaces and get, for example, a Configuration, a SessionFactory, and even a Session whenever needed. For example, instead of creating an EntityManagerFactory with the Persistence static class, you can use a Hibernate Ejb3Configuration: Ejb3Configuration cfg = new Ejb3Configuration(); EntityManagerFactory emf = cfg.configure(”/custom/hibernate.cfg.xml”) .setProperty(”hibernate.show_sql”, “false”) .setInterceptor( new MyInterceptor() ) .addAnnotatedClass( hello.Message.class ) .addResource( “/Foo.hbm.xml”) .buildEntityManagerFactory(); AnnotationConfiguration hibCfg = cfg.getHibernateConfiguration(); The Ejb3Configuration is a new interface that duplicates the regular Hibernate Configuration instead of extending it (this is an implementation detail). This means you can get a plain AnnotationConfiguration object from an Ejb3Configuration, for example, and pass it to a SchemaExport instance programmatically. The SessionFactory interface is useful if you need programmatic control over the second-level cache regions. You can get a SessionFactory by casting the EntityManagerFactory first: HibernateEntityManagerFactory hibEMF = (HibernateEntityManagerFactory) emf; SessionFactory sf = hibEMF.getSessionFactory(); The same technique can be applied to get a Session from an EntityManager: HibernateEntityManager hibEM = (HibernateEntityManager) em; Session session = hibEM.getSession(); This isn t the only way to get a native API from the standardized EntityManager. The JPA specification supports a getDelegate() method that returns the underlying implementation:
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.