Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

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.

Persistence classes scanning

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.

Application.java
.............................
@EntityScan({"org.onap.policy.models.tosca.simple.concepts",
    "org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Implications using Hibernate and SpringBoot scan

persistence.xml file doesn't contain all JPA classes in "org.onap.policy.models.tosca.simple.concepts" package, so SpringBoot will intercept additional classes (not defined into persistence.xml file) and will generate additional tables into controlloop database for them: ToscaEventFilter, ToscaModel, ToscaServiceTemplates, ToscaServiceTemplates_ToscaServiceTemplate, ToscaTimeInterval, ToscaTrigger.


Schema generation

Boolean and TimeStamp

JpaExample
@Embeddable
@Data
public class ExampleKey implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "name", length = 120)
    private String name;

    @Column(name = "version", length = 20)
    private String version;
}

@Entity
@Table(name = "Example")
@Data
@EqualsAndHashCode
public class JpaExample implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    @VerifyKey
    @NotNull
    private ExampleKey key;

    @Column
    private Boolean primed;

    @Column(name = "timeStamp", precision = 3)
    @Temporal(TemporalType.TIMESTAMP)
    @NotNull
    private Date timeStamp;
}

Eclipse-Link

MariaDB [controlloop]> describe Example;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| PRIMED    | tinyint(1)   | YES  |     | 0       |       |
| timeStamp | datetime(3)  | YES  |     | NULL    |       |
| name      | varchar(120) | NO   | PRI | NULL    |       |
| version   | varchar(20)  | NO   | PRI | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

Hibernate

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 of Strings


JpaExampleMap
@Entity
@Table(name = "ExampleMap")
@Data
@EqualsAndHashCode
public class JpaExampleMap implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    @VerifyKey
    @NotNull
    private ExampleKey key;

    @ElementCollection
    @Lob
    private Map<@NotNull String, @NotNull String> attributes;
}


Eclipse-Link

MariaDB [controlloop]> describe ExampleMap;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name    | varchar(120) | NO   | PRI | NULL    |       |
| version | varchar(20)  | NO   | PRI | NULL    |       |
+---------+--------------+------+-----+---------+-------+
MariaDB [controlloop]> SHOW CREATE TABLE JpaExampleMap_ATTRIBUTES;
+--------------------------+----------------------------------------------------------------------------------------------------------+
| Table                    | Create Table                                                                                             |
+--------------------------+----------------------------------------------------------------------------------------------------------+
| JpaExampleMap_ATTRIBUTES | CREATE TABLE `JpaExampleMap_ATTRIBUTES` (
`name` varchar(120) DEFAULT NULL,
  `version` varchar(20) DEFAULT NULL,
`ATTRIBUTES` longtext DEFAULT NULL,
  `ATTRIBUTES_KEY` varchar(255) DEFAULT NULL,
 KEY `FK_JpaExampleMap_ATTRIBUTES_name` (`name`,`version`),
  CONSTRAINT `FK_JpaExampleMap_ATTRIBUTES_name` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleMap` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+--------------------------+----------------------------------------------------------------------------------------------------------+

Hibernate

MariaDB [controlloop]> describe ExampleMap;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name    | varchar(120) | NO   | PRI | NULL    |       |
| version | varchar(20)  | NO   | PRI | NULL    |       |
+---------+--------------+------+-----+---------+-------+
MariaDB [controlloop]> SHOW CREATE TABLE JpaExampleMap_ATTRIBUTES;
+--------------------------+----------------------------------------------------------------------------------------------------------+
| Table                    | Create Table                                                                                             |
+--------------------------+----------------------------------------------------------------------------------------------------------+
| JpaExampleMap_ATTRIBUTES | CREATE TABLE `JpaExampleMap_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 `FKrwvpo54nao070vsy5p23xnps7` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleMap` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+--------------------------+----------------------------------------------------------------------------------------------------------+

JpaToscaCapabilityAssignment_ATTRIBUTES table

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.

Hibernate

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

To be continued

Map of Objects using OneToMany


JpaExampleObjMap
@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;
}

Representation of the example in YAML of what we could save using JpaExampleObjMap entity.

Example
ExampleObjMap:
  name: ExampleObjMap
  version: 1.0.0
  examples:
    MyKey1:
      name: example1
      version: 1.0.0
     MyKey2:
      name: example2
      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

JpaExampleObjMap
@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 |
+-----------------------------+-------------------------------------------------------------------------------------------------------+


Hibernate


  • No labels