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

Vaadin JPAContainer

You could save the first step by asking the entity manager from the JPAContainerFactory.

21.4.2. Creating and Accessing Entities

JPAContainer integrates with the JPA entity manager, which you would normally use to create and access entities with JPA. You can use the entity manager for any purposes you may have, and then JPAContainer to bind entities to user interface components such as Table, Tree, any selection components, or a Form.

You can add new entities to a JPAContainer with the addEntity() method. It returns the item ID of the new entity.

Country france = new Country("France");

Object itemId = countries.addEntity(france);

The item ID used by JPAContainer is the value of the ID property (column) defined with the @Id annotation. In our Country entity, it would have Long type. It is generated by the entity manager when the entity is persisted and set with the setter for the ID proeprty.

Notice that the addEntity() method does not attach the entity instance given as the parameter. Instead, it creates a new instance. If you need to use the entity for some purpose, you need to get the actual managed entity from the container. You can get it with the item ID returned by addEntity().

//Create a new entity and add it to a container Country france = new Country("France");

Object itemId = countries.addEntity(france);

//Get the managed entity

france = countries.getItem(itemId).getEntity();

// Use the managed entity in entity references persons.addEntity(new Person("Jeanne Calment", 122, france));

Entity Items

The getItem() method is defined in the normal Vaadin Container interface. It returns an EntityItem, which is a wrapper over the actual entity object. You can get the entity object with getEntity().

An EntityItem can have a number of states: persistent, modified, dirty, and deleted. The dirty and deleted states are meaningful when using container buffering, while the modified state is meaningful when using item buffering. Both levels of buffering can be used together - user input is first written to the item buffer, then to the entity instance, and finally to the database.

The isPersistent() method tells if the item is actually persistent, that is, fetched from a persistent storage, or if it is just a transient entity created and buffered by the container.

The isModified() method checks whether the EntityItem has changes that are not yet committed to the entity instance. It is only relevant if the item buffering is enabled with setWriteThrough(false) for the item.

The isDirty() method checks whether the entity object has been modified after it was fetched from the entity provider.The dirty state is possible only when buffering is enabled for the container.

The isDeleted() method checks whether the item has been marked for deletion with removeItem() in a buffered container.

Creating and Accessing Entities

463

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