Complete guide to implementing MetaObjects in your enterprise architecture
Get up and running with MetaObjects in minutes
Add the MetaObjects dependency to your Maven project:
<dependency>
<groupId>com.draagon.metaobjects</groupId>
<artifactId>metaobjects-core</artifactId>
<version>5.1.0</version>
</dependency>
Create a metadata/model.json
file with your object definitions:
{
"metadata": {
"package": "com.example",
"children": [
{
"object": {
"name": "User",
"type": "pojo",
"children": [
{"field": {"name": "id", "type": "long"}},
{"field": {"name": "email", "type": "string"}},
{"field": {"name": "name", "type": "string"}}
]
}
}
]
}
}
Add the MetaObjects Maven plugin to generate code during build:
<plugin>
<groupId>com.draagon.metaobjects</groupId>
<artifactId>metaobjects-maven-plugin</artifactId>
<version>5.1.0</version>
<configuration>
<generators>
<generator>
<className>com.draagon.meta.generator.JavaInterfaceGenerator</className>
</generator>
</generators>
</configuration>
</plugin>
Run the Maven plugin to generate your code:
mvn metaobjects:generate
Import and use the generated classes in your application:
// Load metadata with proper configuration
FileMetaDataLoader loader = new FileMetaDataLoader(
new FileLoaderOptions()
.addParser("*.json", JsonMetaDataParser.class)
.addSources(new LocalFileMetaDataSources(
Arrays.asList(
"com/draagon/meta/loader/xml/metaobjects.types.json",
"metadata/users/user.metadata.json"
)
)),
"user-example"
).init();
MetaObject userMeta = loader.getMetaObject("User");
// Create instance
Object user = userMeta.newInstance();
userMeta.setFieldValue(user, "email", "user@example.com");
userMeta.setFieldValue(user, "name", "John Doe");
// Validate
ValidationResult result = userMeta.performValidation(user);
if (result.isValid()) {
System.out.println("User is valid!");
}
Technical specifications for MetaObjects
Understanding the core components of MetaObjects
Core metadata models and types that define the foundation of the system.
Build-time integration that generates code during your Maven build process.
Runtime components that power the metadata-driven behavior in your applications.
High-level abstraction layer for data persistence and querying.
Key interfaces and classes you'll work with
The core interface for working with metadata-driven objects.
newInstance()
- Create new objectsgetFieldValue()
- Get field valuessetFieldValue()
- Set field valuesperformValidation()
- Validate objectsgetMetaFields()
- Get field metadataLoads metadata definitions from various sources.
Comprehensive validation system for data integrity.
Extensible code generation system.
Proven patterns for successful MetaObjects implementations
How to apply MetaObjects in different scenarios
Generate consistent DTOs and validation across services. Perfect for microservices architectures where multiple services need to share data models.
@RestController
public class UserController {
@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody User user) {
// Validation handled automatically by metadata
return ResponseEntity.ok(userService.save(user));
}
}
Generate JPA entities with proper annotations directly from your metadata definitions.
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String email;
// Generated getters and setters
}
Generate TypeScript interfaces and validators for your frontend applications.
// Auto-generated TypeScript interface
export interface User {
id: number;
email: string;
name: string;
}
// Auto-generated validation
export const validateUser = (user: User): ValidationResult => {
// Consistent validation rules from metadata
};
Ensure consistent data models between services and external systems.
// Service A
UserDTO user = metaObjectService.convert(userEntity, UserDTO.class);
// Service B - automatically compatible
UserResponse response = externalService.createUser(user);
// Same metadata ensures compatibility
How to adopt MetaObjects in existing projects
Gradual adoption strategy for existing codebases:
Integration strategies for popular frameworks:
Additional documentation and examples
Comprehensive JavaDoc documentation for all classes and interfaces
Browse API Docs