Showing posts with label jpa. Show all posts
Showing posts with label jpa. Show all posts

Monday, 8 March 2010

PersistenceUnit - PersistenceContext

But what does it all mean???

I don't know if it is because at this point in time I am suffering from a cold and a pair of ulcers are having a party in my mouth. But I am struggling to come to terms with the difference between and PU and PC...

After a quick Google on @PersistenceUnit vc @PersistenceContext I have deduced the following:
A PU will give you an EntityManagerFactory
A PC will give you an EntityManager

Now when to use either, really comes down to your TX strategy, who is managing your connections etcetc.
Using Spring I have always used PC to inject in a Spring managed EM to do the do with. However why is this?
  • @PersistenceUnit annotation is used in J5SE applications to handle Java Persistence API (JPA) persistence management. You may use a PeristenceUnit in an application inside a container, but it must be managed by the developer instead of the container.
  • @PersistenceContext annotation is used for Container Managed Persistence (CMP). This relieves the developer of having to worry about connection management.
and I think this link describes this nicely:

http://java.dzone.com/tips/how-use-glassfish-managed-jpa

Wednesday, 27 January 2010

Hibernate Libraries

This one is a massive Gar...

It all started with the installation of the Fish v2.1.1 distribution. Because of updated libraries supplied by the fish installation, certain versions of hibernate won't work - mainly the one that is shipped with the Beans.

So this led me to download and manually add the libraries to the Fish (I decided that all my apps use Hibernate JPA so might as well have a mutual library), this is where the fun begins. To begin with there is no single download that will give you all the libs with dependencies required for Hibernate JPA. I will add that there are notes saying from 3.5.x this will all change.

Anyhoo, I figured out I needed the:

Annotations
Core
Entitymanager

downloads, take all the libs from here and place them in the lib folder of the fish. Then deploy an app, you will probably get an error like this:


.NoSuchMethodError: org.objectweb.asm.ClassWriter

That is because another library is required. At time of writing I don't know why this is, but anyhoo download the slf4j distribution and copy the following lib into the folder:

slf4j-jdk14-1.5.10.jar

...

Full list of libs:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
cglib-2.2.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-core.jar
slf4j-api.jar
dom4j.jar
ejb3-persistence.jar
hibernate-entitymanager.jar
javassist.jar
jta.jar
slf4j-jdk14-1.5.10.jar

Also posted a question about this on the hibernate forums... Lets see how many responses I get:

https://forum.hibernate.org/viewtopic.php?f=1&t=1002285

Friday, 8 January 2010

JPA + The Cat + Spring

App context:

<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="false" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
</props>
</property>
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

Persistence.xml

<?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="myPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
  </persistence-unit>
</persistence>

JPA + The Fish + Spring

In the app context
<context:annotation-config />
<tx:jta-transaction-manager />
<tx:annotation-driven transaction-manager="transactionManager" />
<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/myPU"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>

In the web.xml

<persistence-unit-ref>
<persistence-unit-ref-name>persistence/myPU</persistence-unit-ref-name>
<persistence-unit-name>myPU</persistence-unit-name>
</persistence-unit-ref>

In the persistence.xml

<?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="myPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myDatasource</jta-data-source>
<properties>
<property name="hibernate.bytecode.provider" value="cglib"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>

Then to access in code:

@PersistenceContext
private EntityManager entityManager;