Skip to content
doe300 edited this page Nov 21, 2015 · 4 revisions

As of version 0.5, JActiveRecord provides another easy way to add attributes, associations and validations to record-types.

NOTE: All of these AnnotationProcessors also provide useful compiler-checks to warn about possibly wrong implementation, e.g. naming or type-incompatibilities.

Attribute-Generator

You can let the attribute getter- and setter-methods be generated by an AnnotationProcessor during compilation. To declare a record-type, you just have to create a type (interface or class), add attributes with the AddAttribute annotation and extend/implement the generated interface from the same package. The last part is necessary, because the java-compiler doesn't allow modifying existing source files with annotation-processors and thus the methods must be written to a new file.

Additionally, all the properties from @Attribute (like nullness, SQL-type, foregin-key, ...) can be specified in @AddAttribute.

Need an example? Here it is:

@AddAttributes({
    @AddAttribute(name = "attr1", type = Integer.class),
    @AddAttribute(name = "attr2", type = Boolean.class, hasSetter = false)
})
public interface MyRecord extends MyRecordAttributes {
    ...
}

//MyRecordAttributes.java 
public interface MyRecordAttributes extends ActiveRecord {
    @Attribute(name = "attr1", type = Integer.class, ...)
    public default Integer getAttr1() 
    { ... }
    public default void setAttr1(Integer value) 
    { ... }
    @Attribute(name = "attr2", type = Boolean.class, ...)
    public default Boolean isAttr2()
    { ... }
}

Association-Generator

The same concept can be used to generate attributes for associated records. To use this, annotate your record-type with @Has, @BelongsTo, @HasManyThrough or the compound annotation @Associations and extend or implement the generated interface. @Has can be configured to generate a has-one or a has-many association, whereas @HasManyThrough can be used to associate via an association-table. Here is an example:

@Associations(
    belongsTo = {
        @BelongsTo(name = "parent", associatedType = MyRecord.class)
    },
    has = {
        @Has(name = "children", associatedType = MyRecord.class, isReadOnly = true)
    }
)
public interface MyRecord extends MyRecordAssociations {
    ...
}

//MyRecordAssociations .java 
public interface MyRecordAssociations extends ActiveRecord {
    @Attribute(name = "parent", type = Integer.class, ...)
    public default MyRecord getParent() 
    { ... }
    public default void setParent(MyRecord value) 
    { ... }
    public default RecordSet<MyRecord> getChildren()
    { ... }
}

Validations-Generator

The ValidationGenerator creates implementations for the validation-methods from ValidatedRecord. To specify validations, annotate your record-type with @Validate or @Validates. Although the @Validation-annotations are natively supported by the RecordBase, using the Validations-generator will remove the need to link the validations on runtime and therefore improve performance. Example follows:

@Validates({
    @Validate(attribute= "name", type = ValidationType.NOT_NULL),
    @Validate(attribute= "parent", type = ValidationType.POSITIVE)
})
public interface MyRecord extends MyRecordValidations {
    ...
}

//MyRecordValidations .java 
public interface MyRecordValidations extends ValidatedRecord {
    public default boolean isValid()
    { ... }
    public default void validate() throws ValidationFailed
    { ... }
}
Clone this wiki locally