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 | bit(1) | YES | | NULL | |
| timeStamp | datetime(6) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
Map and List of Strings
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
@Entity @Table(name = "ExampleMapExampleCollection") @Data @EqualsAndHashCode public class JpaExampleMapJpaExampleCollection 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; } |
Eclipse-Link
MariaDB [controlloop]> describe ExampleMapExampleCollection;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name | varchar(120) | NO | PRI | NULL | |
| version | varchar(20) | NO | PRI | NULL | |
+---------+--------------+------+-----+---------+-------+
MariaDB [controlloop]> SHOW CREATE TABLE JpaExampleMapJpaExampleCollection_ATTRIBUTES;
+--------------------------+-------+---------------------------------------------------------------------------------------------------+
| Table | Create Table | Create Table |
+--------------------------+-------+---------------------------------------------------------------------------------------------------+
| JpaExampleMapJpaExampleCollection_ATTRIBUTES | CREATE TABLE `JpaExampleMap`JpaExampleCollection_ATTRIBUTES` (
`name` varchar(120) DEFAULT NULL,
`version` varchar(20) DEFAULT NULL,
`ATTRIBUTES` longtext DEFAULT NULL,
`ATTRIBUTES_KEY` varchar(255) DEFAULT NULL,
KEY KEY `FK_JpaExampleMapJpaExampleCollection_ATTRIBUTES_name` (`name`,`version`),
CONSTRAINT `FK_JpaExampleMapJpaExampleCollection_ATTRIBUTES_name` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleMap``ExampleCollection` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+--------------------------+-------+---------------------------------------------------------------------------------------------------+
Hibernate
MariaDB [controlloop]> describe ExampleMapSHOW CREATE TABLE JpaExampleCollection_OCCURRENCES;
+----------------------------------+--------------------+-------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name Table | varchar(120) | NO | PRI | NULLCreate Table | |
| version | varchar(20) | NO | PRI | NULL | |
+---------+--------------+------+-----+---------+-------+MariaDB [controlloop]> SHOW CREATE TABLE JpaExampleMap_ATTRIBUTES; |
+--------------------------+--------+--------------------------------------------------------------------------------------------------+
| TableJpaExampleCollection_OCCURRENCES | CREATE TABLE | Create Table |
+--------------------------+----------------------------------------------------------------------------------------------------------+
| JpaExampleMap_ATTRIBUTES | CREATE TABLE `JpaExampleMap_ATTRIBUTES` (
`JpaExampleCollection_OCCURRENCES` (
`name` varchar(120) DEFAULT NULL,
`version` varchar(20) DEFAULT NULL,
`OCCURRENCES` varchar(255) DEFAULT NULL,
KEY `FK_JpaExampleCollection_OCCURRENCES_name` (`name`,`version`),
`name` varchar(120) NOT NULL,
`version` varchar(20) NOT NULL,
CONSTRAINT `FK_JpaExampleCollection_OCCURRENCES_name`
`attributes` longtext DEFAULT NULL,
`attributes_KEY` varchar(255) NOT NULL,
PRIMARY KEY (`name`,`version`,`attributes_KEY`),FOREIGN KEY (`name`, `version`) REFERENCES `ExampleCollection` (`name`, `version`)
CONSTRAINT `FKrwvpo54nao070vsy5p23xnps7` FOREIGN KEY (`name`, `version`)
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 REFERENCES `ExampleMap` (`name`, `version`)
|
+----------------------------------+--------------------------------------------------------------------------------------------------+
After saving an example:
MariaDB [controlloop]> select * from ExampleCollection;
+-------------------+---------+
| name | version |
+-------------------+---------+
| ExampleCollection | 1.0.0 |
+-------------------+---------+
MariaDB [controlloop]> select * from JpaExampleCollection_ATTRIBUTES;
+-------------------+---------+-----------------+----------------+
| name | version | ATTRIBUTES | ATTRIBUTES_KEY |
+-------------------+---------+-----------------+----------------+
| ExampleCollection | 1.0.0 | ValueAttribute2 | Attribute2 |
| ExampleCollection | 1.0.0 | ValueAttribute1 | Attribute1 |
+-------------------+---------+-----------------+----------------+
MariaDB [controlloop]> select * from JpaExampleCollection_OCCURRENCES;
+-------------------+---------+-------------+
| name | version | OCCURRENCES |
+-------------------+---------+-------------+
| ExampleCollection | 1.0.0 | Occurrence1 |
| ExampleCollection | 1.0.0 | Occurrence2 |
+-------------------+---------+-------------+
Hibernate
MariaDB [controlloop]> describe ExampleCollection;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name | varchar(120) | NO | PRI | NULL | |
| version | varchar(20) | NO | PRI | NULL | |
+---------+--------------+------+-----+---------+-------+
MariaDB [controlloop]> SHOW CREATE TABLE JpaExampleCollection_ATTRIBUTES;
+---------------------------------+---------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------------------------------+---------------------------------------------------------------------------------------------------+
| JpaExampleCollection_ATTRIBUTES | CREATE TABLE `JpaExampleCollection_ATTRIBUTES` (
`name` varchar(120) NOT NULL,
`version` varchar(20) NOT NULL,
`attributes` longtext DEFAULT NULL,
`attributes_KEY` varchar(255) NOT NULL,
PRIMARY KEY (`name`,`version`,`attributes_KEY`),
CONSTRAINT `FKrapsbq2h8eev8wstn9x11mfnr` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleCollection` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+---------------------------------+---------------------------------------------------------------------------------------------------+
MariaDB [controlloop]> SHOW CREATE TABLE JpaExampleCollection_OCCURRENCES;
+----------------------------------+--------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------------------------------+--------------------------------------------------------------------------------------------------+
| JpaExampleCollection_OCCURRENCES | CREATE TABLE `JpaExampleCollection_OCCURRENCES` (
`name` varchar(120) NOT NULL,
`version` varchar(20) NOT NULL,
`occurrences` varchar(255) DEFAULT NULL,
KEY `FKbvv6rdypcejif8yy1kr43vpob` (`name`,`version`),
CONSTRAINT `FKbvv6rdypcejif8yy1kr43vpob` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleCollection` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+----------------------------------+--------------------------------------------------------------------------------------------------+
After saving an example:
MariaDB [controlloop]> select * from ExampleCollection;
+-------------------+---------+
| name ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+ | version |
+-------------------+---------+
| ExampleCollection | 1.0.0 |
+-------------------+---------+
MariaDB [controlloop]> select * from JpaExampleCollection_ATTRIBUTES;
+-------------------+---------+-----------------+----------------+
| name | version | attributes | attributes_KEY |
+-------------------+---------+-----------------+----------------+
| ExampleCollection | 1.0.0 | ValueAttribute1 | Attribute1 |
| ExampleCollection | 1.0.0 | ValueAttribute2 | Attribute2 |
+-------------------+---------+-----------------+----------------+
MariaDB [controlloop]> select * from JpaExampleCollection_OCCURRENCES;
+-------------------+---------+-------------+
| name | version | occurrences |
+-------------------+---------+-------------+
| ExampleCollection | 1.0.0 | Occurrence1 |
| ExampleCollection | 1.0.0 | Occurrence2 |
+-------------------+---------+-------------+
JpaToscaCapabilityAssignment_ATTRIBUTES table
...
MariaDB [controlloop]> SHOW CREATE TABLE JpaToscaCapabilityAssignment_ATTRIBUTES;
+-----------------------------------------+-------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------------------------------------+-------------------------------------------------------------------------------------------+
| JpaToscaCapabilityAssignment_ATTRIBUTES | CREATE TABLE `JpaToscaCapabilityAssignment_ATTRIBUTES` (
`name` varchar(120) NOT NULL,
`version` varchar(20) NOT NULL,
`attributes` longtext DEFAULT NULL,
`attributes_KEY` varchar(255) NOT NULL,
PRIMARY KEY (`name`,`version`,`attributes_KEY`),
CONSTRAINT `FKjuqj4nashp9jx76h5eh5psp7l` FOREIGN KEY (`name`, `version`)
REFERENCES `ToscaCapabilityAssignment` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-----------------------------------------+-------------------------------------------------------------------------------------------+
Implications using Hibernate
...
Map of Objects using OneToMany
...