Models Annotation
Models Annotation
As mentioned previously, Artemis has annotations that make the Java developer life easier; these annotations have two categories:
Annotation Models
Qualifier annotation
Annotation Models
The annotation model is to convert the entity model to the entity on communication, the Diana entity:
Entity
Column
MappedSuperclass
Id
Subentity
Embeddable
Convert
The JNoSQL Artemis does not require the getter and setter methods to the fields, however, the Entity class must have a non-private constructor with no parameters.
Entity
This annotation maps the class to Artemis. It has an unique attribute called name
. This attribute is to inform either the column family name or the document collection name, etc. The default value is the simple name of a class, for example, given the org.jnosql.demo.Person class the default name will Person
.
Column
This annotation it to define which fields on an Entity will be persisted. It also has a unique attribute name to specify that name on Database, and the default value is the field name.
MappedSuperclass
If this annotation is on the parent class, Artemis will persist its information as well. So beyond the son class, Artemis will store any field that is in Parent class with Column annotation.
On this sample above, when saves a Dog
instance, it saves the Animal
case too, explicitly will save the fields name
, race
and age
.
Id
It shows which attribute is the id, or the key in key-value types, thus the value will be the remaining information. The way of storing the class will depend on the database driver.
Subentity
Specifies a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity. Each of the persistent properties or fields of the embedded object is mapped to the database table for the entity.
Embeddable
Defines a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the object. So when converts an Embeddable instance to either save or update this is going to be either subdocument or subcolumn.
Convert
As Diana, Artemis has a converter at abstraction level. This feature is useful, e.g., to cipher a field, String to String, or just to do a converter to a custom type using annotation. The Converter
annotation has a parameter, and an AttributeConverter implementation class can be used. Eg. The sample bellow shows how to create a converter to a custom Money class.
Qualifier annotation
That is important to work with more than one type of the same application.
Two injections with the same interface, CDI throws an ambiguous exception. There is the Database
qualifier to fix this problem. It has two attributes:
DatabaseType: The database type, key-value, document, column, graph.
provider: The provider database name, eg. "cassandra, "hbase", "mongodb". So using the
Database
qualifier:
Beyond this annotation, the producer method with the entity manager is required.
The benefits using this qualifier instead of creating a new one is that if the Manager Entity is produced using Database
as a qualifier, Artemis will create classes such as DocumentRepository, ColumnRepository, etc. automatically.
ConfigurationUnit
Storage the database's configuration such as password and user outside the code is important, Eclipse JNoSQL Artemis has the ConfigurationUnit annotation. that reads the configuration from a file such as XML and JSON file and inject to create a factory. The default configuration structure is within either META-INF or WEB-INF folder.
JSON file structure
XML file structure
Injection the code
With the configuration file, the next step is to inject the dependency in the application. The default behavior supports the following classes:
BucketManagerFactory
DocumentCollectionManagerAsyncFactory
DocumentCollectionManagerAsyncFactory
ColumnFamilyManagerAsyncFactory
ColumnFamilyManagerAsyncFactory
ConfigurationUnit Dependency
To configuration unit annotations needs the JNoSQL Artemis configuration dependency.
Last updated