Add ORM to your project in four steps

Let's say your project urgently needs an ORM and you want to implement it as quickly as possible. In this article, I want to show you how you can do this in just four steps using an example of using the open source Apache Cayenne project .





To begin with, I will briefly describe the mechanism of working with this library. The database and model schema is described in an xml file that can be generated through a GUI application or through the console. Then, based on the xml file, java objects are generated, which are the corresponding mapping of tables in the database. The last step is to create ServerRuntimean object that encapsulates the entire Apache Cayenne stack.



So let's move on to an example. What needs to be done:



  • Create database schema
  • Import the schema into the project, that is, get xml files with the schema description
  • Create object model, i.e. generate java classes
  • Initialize ServerRuntimeto access the database from the application


? maven gradle , Java 1.8+ . maven, java 14 Apache Cayenne 4.2.M1. mysql. 4.1 .

.





, : , , .





CREATE SCHEMA IF NOT EXISTS cars_demo; USE cars_demo;
CREATE TABLE car_brand (ID INT NOT NULL AUTO_INCREMENT, NAME VARCHAR(200) NULL, COUNTRY VARCHAR(200) NULL, PRIMARY KEY (ID)) ENGINE=InnoDB;
CREATE TABLE car_model (ID INT NOT NULL AUTO_INCREMENT, NAME VARCHAR(200) NULL, CAR_BRAND_ID INT NULL, PRIMARY KEY (ID)) ENGINE=InnoDB;
CREATE TABLE feedback (CAR_MODEL_ID INT NULL, ID INT NOT NULL AUTO_INCREMENT, FEEDBACK VARCHAR(200) NULL, PRIMARY KEY (ID)) ENGINE=InnoDB;
ALTER TABLE car_model ADD FOREIGN KEY (CAR_BRAND_ID) REFERENCES car_brand (ID) ON DELETE CASCADE;
ALTER TABLE feedback ADD FOREIGN KEY (CAR_MODEL_ID) REFERENCES car_model (ID) ON DELETE CASCADE;


, .





. :



            <plugin>
                <groupId>org.apache.cayenne.plugins</groupId>
                <artifactId>cayenne-maven-plugin</artifactId>
                <version>${cayenne.version}</version>
                <configuration>
                    <dataSource> <!--1-->
                        <driver>com.mysql.jdbc.Driver</driver> 
                        <url>jdbc:mysql://127.0.0.1:3306/cars_demo</url> 
                        <username>root</username> 
                        <password>root</password>
                    </dataSource>
                    <cayenneProject>${project.basedir}/src/main/resources/cayenne/cayenne-project.xml</cayenneProject> <!--2-->
                    <map>${project.basedir}/src/main/resources/cayenne/datamap.map.xml</map> <!--3-->
                    <dbImport> <!--4-->
                        <defaultPackage>cayenne.note.project.model</defaultPackage>
                        <catalog>cars_demo</catalog>
                    </dbImport>
                </configuration>
                <dependencies>
                    <dependency> <!--5-->
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                </dependencies>
            </plugin>


  • (1) DataSource,
  • (2) , xml, Cayenne
  • (3) , xml
  • (4) ,
  • (5) mysql-connector mysql


:



mvn cayenne:cdbimport


, (2) (3). , cayenne-project.xml , . datamap.map.xml — , .



cdbimport: , . . , , , . .





, java , . , :



mvn cayenne:cgen


, , . , . auto . , . , auto. .





, Apache Cayenne.

ServerRuntime — Cayenne, .



ObjectContext — , .



ServerRuntime cayenneRuntime = ServerRuntime.builder()
                .dataSource(DataSourceBuilder
                        .url("jdbc:mysql://127.0.0.1:3306/cars_demo")
                        .driver("com.mysql.cj.jdbc.Driver")
                        .userName("root") // Need to change to your username
                        .password("root") // Need to change to your password
                        .build())
                .addConfig("cayenne/cayenne-project.xml")
                .build();
        ObjectContext context = cayenneRuntime.newContext();


:



CarBrand carBrand = context.newObject(CarBrand.class);
carBrand.setName("BMW");
carBrand.setCountry("Germany");

CarModel carModel = context.newObject(CarModel.class);
carModel.setName("i3");
carModel.setCarBrand(carBrand);

Feedback feedback = context.newObject(Feedback.class);
feedback.setFeedback("Like");
feedback.setCarModel(carModel);

context.commitChanges();


, ObjectContext, context.commitChanges().



API sql ejbql API. .



Apache Cayenne:



List<CarBrand> carBrans = ObjectSelect.query(CarBrand.class).select(context);


That's all for me. As you can see from this example, in a few steps you can get a production ORM that is fully operational. I hope the article was helpful. A complete example can be found here .




All Articles