The goal of this document is to show implications for tosca model template to move from Eclipse-Link to Hibernate. Primary focus is to show the database compatibility and eventually schema changes where is necessary.
In all these tests I am using some strategies to make sure that tables and fields name generated by Hibernate will be the same as Eclipse-Link.
...
Definitions
- ElementCollection: Specifies a collection of instances of a basic type or embeddable class. Must be specified if the collection is to be mapped by means of a collection table.
- OneToMany: Specifies a many-valued association with one-to-many multiplicity. If the collection is defined using generics to specify the element type, the associated target entity type need not be specified; otherwise the target entity class must be specified. If the relationship is bidirectional, the
mappedBy
element must be used to specify the relationship field or property of the entity that is the owner of the relationship. TheOneToMany
annotation may be used within an embeddable class contained within an entity class to specify a relationship to a collection of entities. If the relationship is bidirectional, themappedBy
element must be used to specify the relationship field or property of the entity that is the owner of the relationship. When the collection is ajava.util.Map
, thecascade
element and theorphanRemoval
element apply to the map value.
...
Control Loop runtime uses persistence.xml
file: is the deployment descriptor file for persistence using JPA. It specifies the persistence units and declares the managed persistence classes, the object/relation mapping, and the database connection details.
Hibernate
SpringBoot auto-configuration can automatically scan entity classes. In Control Loop Runtime we can use @EntityScan annotation because entity classes are not placed in the main application package or its sub-packages. In this situation, we need declare the package or list of packages in the main configuration class within @EntityScan annotation.
...
MariaDB [controlloop]> describe Example;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| name | varchar(120) | NO | PRI | NULL | |
| version | varchar(20) | NO | PRI | NULL | |
| primed | tinyint(1) | YES | | NULL | |
| timeStamp | datetime(3) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
Map and List of Strings
When @ElementCollection is used properly there is enough compatibility. We can see that in the scenario shown below: there is only a small difference in how the primary keys are build.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Entity @Table(name = "ExampleCollection") @Data public class JpaExampleCollection implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId @VerifyKey @NotNull private ExampleKey key; @ElementCollection @Lob private Map<@NotNull String, @NotNull String> attributes; @ElementCollection private List<String> occurrences; } |
...
JpaToscaCapabilityAssignment_ATTRIBUTES table
Currently in tosca model template, due the complexity of the schema there are some issues in foreign keys.
Eclipse-Link
MariaDB [controlloop]> SHOW CREATE TABLE JpaToscaCapabilityAssignment_ATTRIBUTES;
+-----------------------------------------+-------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------------------------------------+-------------------------------------------------------------------------------------------+
| JpaToscaCapabilityAssignment_ATTRIBUTES | CREATE TABLE `JpaToscaCapabilityAssignment_ATTRIBUTES` (
`name` varchar(120) DEFAULT NULL,
`version` varchar(20) DEFAULT NULL,
`ATTRIBUTES` longtext DEFAULT NULL,
`ATTRIBUTES_KEY` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-----------------------------------------+-------------------------------------------------------------------------------------------+
Currently in controlloop database, I have not found out why foreign key has not be generated in this table and in all '_ATTRIBUTES', '_META' and '_CONSTRAINTS' tables as well.
...
ElementCollection should be used with basic type or embeddable class, this example shows side effects using it with entity.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Entity @Table(name = "ExampleObjMapEc") @Data public class JpaExampleObjMapEc implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId @VerifyKey @NotNull private ExampleKey key; @ElementCollection @Lob private Map<@NotNull String, @NotNull JpaExample> examples; } |
...
ElementCollection should be used with basic type or embeddable class, this example shows side effects using it with entity.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Entity @Table(name = "ExampleObjListEc") @Data public class JpaExampleObjListEc implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId @VerifyKey @NotNull private ExampleKey key; @ElementCollection @Lob private List<@NotNull JpaExample> examples; } |
...
MariaDB [controlloop]> select * from ExampleObjMap;
+---------------+---------+
| name | version |
+---------------+---------+
| ExampleObjMap | 1.0.0 |
+---------------+---------+
MariaDB [controlloop]> select * from Example;
+----------+---------+--------+-----------+
| name | version | primed | timeStamp |
+----------+---------+--------+-----------+
| example1 | 1.0.0 | NULL | NULL |
| example2 | 1.0.0 | NULL | NULL |
+----------+---------+--------+-----------+
MariaDB [controlloop]> select * from ExampleObjMap_Example;
+---------------+----------------+------------+---------------+--------------+
| parent_name | parent_version | child_name | child_version | examples_KEY |
+---------------+----------------+------------+---------------+--------------+
| ExampleObjMap | 1.0.0 | example1 | 1.0.0 | MyKey1 |
| ExampleObjMap | 1.0.0 | example2 | 1.0.0 | MyKey2 |
+---------------+----------------+------------+---------------+--------------+
Multi templates
Right now using Spring repositories it is possible to create more than one service template, as service templates in policy-models/tosca. It works in both Eclipse-Link and Hibernate.
...
- The presence of longblob types used to store whole objects is definitely not so maintainable. The fix for those issues could be done in both Eclipse-Link and Hibernate. Before to apply the fix it needs to check the max length of varchar fields.
- Document databases store all information for a given object in a single instance in the database, and every stored object can be different from every other. So, using MongoDB/Cassandra will solve all issues.
JpaRepository
andMongoRepository
extendCrudRepository
and expose the capabilities of the underlying persistence technology in addition to the rather generic persistence technology-agnostic interfaces like e.g.CrudRepository
.