4.4. Generator examples
Once you download an example from the site, it becomes part of your process. You can change the code to suit your needs, you can add it to your git repository, feed it to LLM for processing or distribute it.
The code of the examples on the site can change, new examples can be added, but this will not affect the example you downloaded. If you want to share your generator example or correct the existing one, please send it to info@ormfactory.com or create a new issue.
Later we will move the code of all generators to the GitHub repository.
License
All code in this article is licensed under CC0 1.0. You are free to use, modify, and distribute the code for any purpose, including commercial use, without asking for permission.
The code is provided "as is", without warranty of any kind, express or implied.
For more visit https://creativecommons.org/publicdomain/zero/1.0/.
Hibernate for MySql
This example script was written for the sakila sample database and was tested on it. The script generates an entity class and a repository class for each Java table. A mandatory requirement is that the table has a primary key. In the case of a composite primary key, the script generates a third class - the key class.
The script works with a limited subset of MySql types. The correspondence between the DB and Java types is performed in the resolve_type
method.
In accordance with the accepted naming conventions, the name conversion option in the generator settings must be set to CamelCase. In this case, OrmFactory will properly convert the table and column names to the standard in the fields ClassName and FieldName.
The entity class is generated by the generate_table_entity
method. This method creates a class with private variables and get/set accessors. In addition, the method generates references to other objects by foreign keys (one-to-many reference), as well as references from other objects to this one in the form of a list of referencing entities of another type (many-to-one reference).
The method places annotations for the table class and for the fields in accordance with the data schema. Primary key fields receive the @Id
annotation, and if autoincrement is enabled, also with @GeneratedValue(strategy=GenerationType.AUTO)
.
Fields that refer to other entities receive the type of this entity and are annotated by @ManyToOne(fetch = FetchType.LAZY)
and @JoinColumn(name = "...", insertable=false, updatable=false)
.
The repository class is generated by the method generate_table_repository
. This method ignores the ability to use the RepositoryName tag and names the class by appending Repository
to ClassName. However, you can change the behavior of this method.
At the beginning of the script, there is a settings section:
PACKAGE_NAME = "com.example"
INDENT = " "
NEWLINE = "\n"
ROOT_PATH = "Models"
In this section you can set indentation symbols, line feed symbols, package name and path to the folder with generated files. The path is specified relative to the location of the project file.
Direct link to download the script - my-hibernate.py or view online.
Entity Framework Core for MySql
This script is used to generate models for this site. It generates a single file with model classes and one context class where repository variable properties are located. One file is used for all classes so that during subsequent regeneration you do not have to manually remove unused model classes.
The data context class inherits from DbContext
. Repositories of the DbSet<ClassType>
type are automatically added inside this class. The class is made partial
so that you can override the OnConfiguring(DbContextOptionsBuilder options)
method in another file.
The generator is designed to have the option of converting names to CamelCase enabled to comply with C# naming standards.
Please pay attention to the is_database_generated
method at the very beginning of the script. It determines whether the field is autogenerated by the database, which will allow Entity Framework to correctly create insert and update statements. In this example, it is set to only determine the MySql's current_timestamp
default value. You can extend this method to suit your needs.
If a column or table has a comment, the generator will copy it into the code, framing it with summary
tags.
Entity Framework does not require mandatory specification of the table name or column name if they match the class and field names respectively. Therefore, the generator code has a check for name matches, and if the names do not match, the [Table("")]
and [Column("")]
annotations will be generated.
For foreign keys, the [ForeignKey("")]
annotation will always be generated.
At the very beginning there is a settings section where you can adjust the format and redefine your names:
INDENT = "\t"
NEWLINE = "\n"
OUTPUT_PATH = "../OrmFactoryCom/Model/DataContextGenerated.cs"
NAMESPACE = "OrmFactoryCom.Model"
CONTEXT_NAME = "DataContext"
Direct link to download the script - my-0f-ef.py or view online.