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.
...
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.
...
Code Block | ||||
---|---|---|---|---|
| ||||
@Entity @Table(name = "ExampleObjMap") @Data @EqualsAndHashCode public class JpaExampleObjMap implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId @VerifyKey @NotNull private ExampleKey key; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) @Lob private Map<@NotNull String, @NotNull JpaExample> examples; } |
Eclipse-Link
Hibernate
...
Representation of the example in YAML of what we could save using JpaExampleObjMap entity.
Code Block | ||||
---|---|---|---|---|
| ||||
@EntityExampleObjMap: @Table(name = "ExampleObjMapEc") @Data @EqualsAndHashCode public class JpaExampleObjMapEc implements Serializable { name: ExampleObjMap version: 1.0.0 examples: MyKey1: private static final longname: serialVersionUIDexample1 = 1L; @EmbeddedIdversion: 1.0.0 MyKey2: @VerifyKey @NotNull name: example2 private ExampleKey key; version: 1.0.0 |
Eclipse-Link
MariaDB [controlloop]>
...
describe ExampleObjMap;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| EXAMPLES | longblob | YES | | NULL | |
| name | varchar(120) | NO | PRI | NULL | |
| version | varchar(20) | NO | PRI | NULL | |
+----------+--------------+------+-----+---------+-------+
After saving the example:
MariaDB [controlloop]> select * from ExampleObjMap;
+--------------------------+---------------+---------+
| EXAMPLES | name | version |
+--------------------------+---------------+---------+
| .... <binary code> .... | ExampleObjMap | 1.0.0 |
+--------------------------+---------------+---------+
Removing @Lob Annotation
The keys name and version of the JpaExampleObjMap as the same as JpaExample, so Eclipse-Link creates a wrong ExampleObjMap_Example table.
MariaDB [controlloop]> describe ExampleObjMap;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name | varchar(120) | NO | PRI | NULL | |
| version | varchar(20) | NO | PRI | NULL | |
+---------+--------------+------+-----+---------+-------+
MariaDB [controlloop]> SHOW CREATE TABLE ExampleObjMap_Example;
+-----------------------+-------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------------------+-------------------------------------------------------------------------------------------------------------+
| ExampleObjMap_Example | CREATE TABLE `ExampleObjMap_Example` (
`name` varchar(120) NOT NULL,
`version` varchar(20) NOT NULL,
`EXAMPLES_KEY` varchar(255) DEFAULT NULL,
PRIMARY KEY (`name`,`version`),
CONSTRAINT `FK_ExampleObjMap_Example_name` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleObjMap` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-----------------------+-------------------------------------------------------------------------------------------------------------+
Using these tables I have got this error: Cannot add or update a child row: a foreign key constraint fails (`controlloop`.`ExampleObjMap_Example`, CONSTRAINT `FK_ExampleObjMap_Example_name` FOREIGN KEY (`name`, `version`) REFERENCES `ExampleObjMap` (`name`, `version`))
Error Code: 1452
Hibernate
Map of Objects using ElementCollection
Code Block | ||||
---|---|---|---|---|
| ||||
@Entity
@Table(name = "ExampleObjMapEc")
@Data
@EqualsAndHashCode
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;
} |
Eclipse-Link
MariaDB [controlloop]> describe ExampleObjMapEc;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name | varchar(120) | NO | PRI | NULL | |
| version | varchar(20) | NO | PRI | NULL | |
+---------+--------------+------+-----+---------+-------+
MariaDB [controlloop]> SHOW CREATE TABLE JpaExampleObjMapEc_EXAMPLES;
+-----------------------------+-------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------------------------+-------------------------------------------------------------------------------------------------------+
| JpaExampleObjMapEc_EXAMPLES | CREATE TABLE `JpaExampleObjMapEc_EXAMPLES` (
`name` varchar(120) DEFAULT NULL,
`version` varchar(20) DEFAULT NULL,
`EXAMPLES` longblob DEFAULT NULL,
`EXAMPLES_KEY` varchar(255) DEFAULT NULL,
KEY `FK_JpaExampleObjMapEc_EXAMPLES_name` (`name`,`version`),
CONSTRAINT `FK_JpaExampleObjMapEc_EXAMPLES_name` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleObjMapEc` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-----------------------------+-------------------------------------------------------------------------------------------------------+