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

August 22, 2012

Create a JPA EntityManager programmatically

I've been using this kind of PersistenceUnit creation for some unit tests, because they had to run along with some integration tests, and because of that the persistence.xml files were messing between them.

So, how to create an EntityManager programmatically?

  • Solution link: http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html/configuration.html

March 18, 2010

Get metadata from JPA entities

I needed to extract a column length from a EJB-JPA Entity configured with annotations, this is how I did it:
import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
public class Person {

   @Column(length=30)
   private String firstName;

   public static void main(String[] args) throws SecurityException,     NoSuchFieldException {
      Object person = new Person();
      println(person.getClass().getDeclaredField("firstName").getAnnotation(Column.class).length());
   }
}

Solution source

October 13, 2009

JPA Entity Manager y la operación 'merge'

Durante el desarrollo de un proyecto, nos encontramos con el problema de que muchas actualizaciones que se realizaban a entidades (JPA) no se escribían en la base de datos. Revisando el log de Hibernate, encontramos que muchas veces la operación 'merge' solamente ejecutaba una selección de datos, y no asi su modificacion.

Revisando encontramos la siguiente referencia en el libro 'Seam in action' :
Merging is a crude operation and should be avoided if possible
Además el mismo autor recomienda hacer uso de las operaciones 'flush' y 'refresh' para actualizar datos o deshacer cambios. Esto nos salvo muchos problemas, ademas de habernos aclarado algunas dudas, ojala les sea útil.