SpringBot Sharing Attributes Across Entities
Client-side
Adding to the View
Attributes can be added to all entities by updating the abstract.model.ts
file, which all entities inherit from. This can be found at clientside/src/app/lib/models/abstract.model.ts
.
For example, to add a new string attribute called Name
to every entity, we would do the following:
- Activate the protected region called
Add any additional default model properties here
inabstract.model.ts
. -
Add the attribute:
// % protected region % [Add any additional default model properties here] on begin { name: 'name', displayName: 'Name', type: ModelPropertyType.STRING, }, // % protected region % [Add any additional default model properties here] end
You will now have a Name
attribute visible on all entities, however, you will not be able to utilise it as it doesn’t exist within the persistence layer.
Adding to the Persistence Layer
- Activate the protected region called
Add any additional class fields here
inabstract.model.ts
. -
Add the following:
// % protected region % [Add any additional class fields here] on begin name: string; // % protected region % [Add any additional class fields here] end
- Activate the protected region called
Add any additional assignments here
inabstract.model.ts
. -
Add the following into it:
// % protected region % [Add any additional assignments here] on begin this.name = json.name; // % protected region % [Add any additional assignments here] end
- Activate the protected region called
Add additional toJSON attributes here
inabstract.model.ts
. -
Add the following into it:
// % protected region % [Add additional toJSON attributes here] on begin name: this.name, // % protected region % [Add additional toJSON attributes here] end
- Open the file
clientside/src/app/lib/services/http/abstract-http.service.ts
and activate the protected region calledAdd additional base properties here
. -
Add the following:
// % protected region % [Add additional base properties here] on begin extraBaseProperties += 'name'; // % protected region % [Add additional base properties here] end
Server-side
Similar to the client-side, we can make use of our AbstractEntity.java
to add additional attributes. This file can be found at serverside/src/main/java/[projectName]/entities/AbstractEntity.java
Following the example started in the client-side section, we will achieve this by completing the following steps:
- Activate the protected region called
Add any additional class methods here
inAbstractEntity.java
. -
Add the attribute using the following code:
// % protected region % [Add any additional class methods here] on begin @ApiModelProperty(notes = "The name.") @Column(name = "name") private String name; // % protected region % [Add any additional class methods here] end
You will notice that we also add the API documentation to this attribute using the
ApiModelProperty
annotation. This is important to ensure that our API documentation stays up to date.
References can be added in a similar manner, but be careful, since all entities inherit from theAbstractEntity
you may end up adding a self reference to the source entity. - Open
serverside/src/main/resources/graphql/schemas/myuser.schema.graphql
and activate the protected region calledAdd any additional field definition here
. -
Add the following to it:
# % protected region % [Add any additional field definition here] on begin name: String # % protected region % [Add any additional field definition here] end
- In the same file, activate the protected region called
Add any additional field definition for input type here
-
Add the following:
# % protected region % [Add any additional field definition for input type here] on begin name: String # % protected region % [Add any additional field definition for input type here] end
Was this article helpful?