@Column(name = "PASSWORD")
protected String password;
//getters and setters
}
Now we have got an additional requirement from some of our customers to add the ‘home address’ field to users. To do that we extend the User entity in the extension:
@Entity(name = "ext$User")
@Extends(User.class)
public class ExtUser extends User {
@Column(name = "ADDRESS", length = 100)
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
As you may have already noticed, all annotations, except for @Extends
one, are common JPA annotations. The @Extends
attribute is a part of the CUBA engine which globally substitutes the User entity to ExtUser
, even across the product functionality.
Using the @Extends
attribute we force the platform to:
-
always create an entity of the ‘latest child’ type
User user = metadata.create(User.class); //ExtUser entity will be created
-
transform all the JPQL queries before the execution so that they always return the ‘latest childset’
select u from product$User u where u.name = :name //returns a list of ExtUsers
-
always use the ‘latest child’ in the associated entities
userSession.getUser(); //returns an instance of ExtUser type
In other words, if an extended entity is declared, the base one is abandoned across the entire solution (product and extension) and is globally overridden by the extended one.
Screens Customization
So, we have extended the User entity by adding the address attribute and now want the changes to be reflected in the user interface. First, let us have a look on the original (product) screen declaration:
<window
datasource="userDs"
caption="msg://caption"
class="com.haulmont.cuba.gui.app.security.user.edit.UserEditor"
messagesPack="com.haulmont.cuba.gui.app.security.user.edit">
&lt;dsContext&gt;
&lt;datasource
id=&quot;userDs&quot;
class=&quot;com.haulmont.cuba.security.entity.User&quot;
view=&quot;user.edit&quot;&gt;
&lt;/datasource&gt;
&lt;/dsContext&gt;
&lt;layout&gt;
&lt;fieldGroup id=&quot;fieldGroup&quot; datasource=&quot;userDs&quot;&gt;
&lt;column&gt;
&lt;field id=&quot;login&quot;/&gt;
&lt;field id=&quot;password&quot;/&gt;
&lt;/column&gt;
&lt;/fieldGroup&gt;
&lt;iframe id=&quot;windowActions&quot; screen=&quot;editWindowActions&quot;/&gt;
&lt;/layout&gt;
</window>