You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.
Hibernate is the default persistence provider in Seam but it is theoretically possible use other persistence provider. Seam 2.1.1.GA documentation states that in the future, this will be easier.
If you cannot wait the future, this walkthrough describes the changes required to switch a CRUD application from Hibernate to OpenJPA.
Requirements: in order to use OpenJPA as an alternate JPA provider, you will need:
First, let's see the functionality differences between Hibernate and OpenJPA:
Functionality | Hibernate | OpenJPA |
---|---|---|
Query result pagination | Yes | No (see OPENJPA-759) |
Identifier alias 'id' | Yes | No |
Expression in LIKE query parameter | Yes | No (see OPENJPA-920) |
Auditing | 1) JPA-standard 2) HibernateInterceptor |
1) JPA-standard |
Note: JPA-standard Auditing is done via @EntityListeners, @PrePersist, @PreUpdate, ...
There is also syntax differences between these two persistence providers:
Functionality | Hibernate | OpenJPA |
---|---|---|
Key generator interface | org.hibernate.id.IdentifierGenerator | org.apache.openjpa.kernel.Seq |
Key generator annotation | @GenericGenerator | @SequenceGenerator with specific name |
Yes/No type | @org.hibernate.annotations.Type("yes_no") | @ExternalValues("true=Y", "false=N") @org.apache.openjpa.persistence.Type(char.class) |
Logging | proprietary Hibernate format | proprietary OpenJPA format |
//@Name("org.jboss.seam.persistence.persistenceProvider") // see next step @Scope(ScopeType.STATELESS) @BypassInterceptors @Install(precedence=FRAMEWORK, classDependencies={"javax.persistence.EntityManager"}) public class OpenJpaPersistenceProvider extends PersistenceProvider { Log log = Logging.getLog(OpenJpaPersistenceProvider.class); @Override public void setFlushModeManual(EntityManager entityManager) { // see also https://jira.jboss.org/jira/browse/JBSEAM-3030 log.warn("setFlushModeManual is not supported by OpenJPA"); } }
<!-- the persistence provider cannot be a named Seam component with the @Name annotation, otherwise Seam raises an error because HibernatePersistenceProvider already define this name (duplicate component). Thus, we are defining the component here (see http://docs.jboss.com/seam/2.1.1.GA/reference/en-US/html_single/#alt-jpa-providers for details) --> <component name="org.jboss.seam.persistence.persistenceProvider" class="org.your.package.OpenJpaPersistenceProvider"> </component>Note: in the future, this should be easier, see JBSEAM-2785
//replace: //@${pojo.importType("org.hibernate.annotations.Type")}(type="yes_no")} // by: @${pojo.importType("org.apache.openjpa.persistence.ExternalValues")}({"true=Y", "false=N"}) @${pojo.importType("org.apache.openjpa.persistence.Type")}(char.class)Note: you may hit the following issue:
Using the walkthrough above, you will be able to switch from Hibernate to OpenJPA (and vice-versa) just by doing the following operations:
org.jboss.seam.persistence.persistenceProvider)
Convenient, isn't?