Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Vaadin JPAContainer

Annotation: @Transient

JPA assumes that all entity properties are persisted. Properties that should not be persisted should be marked as transient with the @Transient annotation.

@Transient

private Boolean superDepartment;

...

@Transient

public String getHierarchicalName() {

...

21.4. Basic Use of JPAContainer

Vaadin JPAContainer offers a highly flexible API that makes things easy in simple cases while allowing extensive flexibility in demanding cases. To begin with, it is a Container, as described in Section 9.5, “Collecting Items in Containers”.

In this section, we look how to create and use JPAContainer instances. We assume that you have defined a domain model with JPA annotations, as described in the previous section.

21.4.1. Creating JPAContainer with JPAContainerFactory

The JPAContainerFactory is the easy way to create JPAContainers. It provides a set of make...() factory methods for most cases that you will likely meet. Each factory method uses a different type of entity provider, which are described in Section 21.5, “Entity Providers”.

The factory methods take the class type of the entity class as the first parameter. The second parameter is either a persistence unit name (persistence context) or an EntityManager instance.

//Create a persistent person container JPAContainer<Person> persons =

JPAContainerFactory.make(Person.class, "book-examples");

//You can add entities to the container as well persons.addEntity(new Person("Marie-Louise Meilleur", 117));

//Set up sorting if the natural order is not appropriate persons.sort(new String[]{"age", "name"},

new boolean[]{false, false});

// Bind it to a component

Table personTable = new Table("The Persistent People", persons); personTable.setVisibleColumns(new String[]{"id","name","age"}); layout.addComponent(personTable);

It's that easy. In fact, if you run the above code multiple times, you'll be annoyed by getting a new set of persons for each run - that's how persistent the container is. The basic make() uses a CachedMutableLocalEntityProvider, which allows modifying the container and its entities, as we do above by adding new entities.

When using just the persistence unit name, the factory creates an instance of EntityManagerFactory for the persistence unit and uses it to build entity managers. You can also create the entity managers yourself, as described later.

Basic Use of JPAContainer

461

Vaadin JPAContainer

The entity providers associated with the different factory methods are as follows:

Table 21.1. JPAContainerFactory Methods

make()

CachingMutableLocalEntityProvider

makeReadOnly()

CachingLocalEntityProvider

makeBatchable()

BatchableLocalEntityProvider

makeNonCached()

MutableLocalEntityProvider

makeNonCachedReadOnly()

LocalEntityProvider

JPAContainerFactory holds a cache of entity manager factories for the different persistence units, making sure that any entity manager factory is created only once, as it is a heavy operation. You can access the cache to get a new entity manager with the createEntityManagerForPersistenceUnit() method.

// Get an entity manager

EntityManager em = JPAContainerFactory. createEntityManagerForPersistenceUnit("book-examples");

// Do a query em.getTransaction().begin();

em.createQuery("DELETE FROM Person p").executeUpdate(); em.persist(new Person("Jeanne Calment", 122)); em.persist(new Person("Sarah Knauss", 119)); em.persist(new Person("Lucy Hannah", 117)); em.getTransaction().commit();

...

Notice that if you use update the persistent data with an entity manager outside a JPAContainer bound to the data, you need to refresh the container as described in Section 21.4.2, “Creating and Accessing Entities”.

Creating JPAContainer Manually

While it is normally easiest to use a JPAContainerFactory to create JPAContainer instances, you may need to create them manually. It is necessary, for example, when you need to use a custom entity provider or extend JPAContainer.

First, we need to create an entity manager and then the entity provider, which we bind to a

JPAContainer.

//We need a factory to create entity manager EntityManagerFactory emf =

Persistence.createEntityManagerFactory("book-examples");

//We need an entity manager to create entity provider EntityManager em = emf.createEntityManager();

//We need an entity provider to create a container CachingMutableLocalEntityProvider<Person> entityProvider =

new CachingMutableLocalEntityProvider<Person>(Person.class,

em);

// And there we have it JPAContainer<Person> persons =

new JPAContainer<Person> (Person.class); persons.setEntityProvider(entityProvider);

462

Creating JPAContainer with JPAContainerFactory

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]