ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 01.01.2026

Просмотров: 2612

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

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



Vaadin JPAContainer

Refreshing JPAContainer

In cases where you change JPAContainer items outside the container, for example by through an EntityManager, or when they change in the database, you need to refresh the container.

The EntityContainer interface implemented by JPAContainer provides two methods to refresh a container. The refresh() discards all container caches and buffers and refreshes all loaded items in the container. All changes made to items provided by the container are discarded. The refreshItem() refreshes a single item.

21.4.3. Nested Properties

If you have a one-to-one or many-to-one relationship, you can define the properties of the referenced entity as nested in a JPAContainer. This way, you can access the properties directly through the container of the first entity type as if they were its properties. The interface is the same as with BeanContainer described in Section 9.5.4, “BeanContainer”. You just need to add each nested property with addNestedContainerProperty() using dot-separated path to the property.

//Have a persistent container JPAContainer<Person> persons =

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

//Add a nested property to a many-to-one property persons.addNestedContainerProperty("country.name");

//Show the persons in a table, except the "country" column,

//which is an object - show the nested property instead

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

"country.name"});

// Have a nicer caption for the country.name column personTable.setColumnHeader("country.name", "Nationality");

The result is shown in Figure 21.5, “Nested Properties”. Notice that the country property in the container remains after adding the nested property, so we had to make that column invisible. Alternatively, we could have redefined the toString() method in the country object to show the name instead of an object reference.

Figure 21.5. Nested Properties

You can use the * wildcard to add all properties in a nested item, for example, "country.*".

464

Nested Properties