...
Code Block |
---|
title | JpaExample |
---|
collapse | true |
---|
|
@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;
} |
...
Code Block |
---|
language | java |
---|
title | JpaExampleObjMap |
---|
collapse | true |
---|
|
@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;
} |
...
Code Block |
---|
language | java |
---|
title | JpaExampleObjMap |
---|
collapse | true |
---|
|
@Entity
@Table(name = "ExampleObjMap")
@Data
@EqualsAndHashCode
public class JpaExampleObjMap implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
@VerifyKey
@NotNull
private ExampleKey key;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(
joinColumns = {@JoinColumn(name = "parent_name", referencedColumnName = "name"),
@JoinColumn(name = "parent_version", referencedColumnName = "version")},
inverseJoinColumns = {@JoinColumn(name = "child_name", referencedColumnName = "name"),
@JoinColumn(name = "child_version", referencedColumnName = "version")})
private Map<@NotNull String, @NotNull JpaExample> examples;
} |
...
Code Block |
---|
language | java |
---|
title | JpaExampleObjMapJpaExampleObjMapEc |
---|
collapse | true |
---|
|
@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;
} |
...
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) NOT NULL,
`version` varchar(20) NOT NULL,
`examples_name` varchar(120) NOT NULL,
`examples_version` varchar(20) NOT NULL,
`examples_KEY` varchar(255) NOT NULL,
PRIMARY KEY (`name`,`version`,`examples_KEY`),
UNIQUE KEY `UK_o5b5lwcueiue1x7wf2j9l0ohb` (`examples_name`,`examples_version`),
CONSTRAINT `FKeu2s8p9mhstus32uliuwib8rr` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleObjMapEc` (`name`, `version`),
CONSTRAINT `FKf66pkeal9tdygic4pvspxhph5` FOREIGN KEY (`examples_name`, `examples_version`)
REFERENCES `Example` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-----------------------------+-------------------------------------------------------------------------------------------------------+
After saving the an example (it needs an extra step: save JpaExample objects before JpaExampleObjMapEc object):
...
Code Block |
---|
language | java |
---|
title | JpaExampleObjMapJpaExampleObjMapEc |
---|
collapse | true |
---|
|
@Data
@Embeddable
public class JpaExampleEmd implements Serializable {
private static final long serialVersionUID = 1L;
@VerifyKey
@NotNull
@AttributeOverride(name = "name", column = @Column(name = "child_name"))
@AttributeOverride(name = "version", column = @Column(name = "child_version"))
private ExampleKey key;
@Column
private Boolean primed;
@Column(name = "timeStamp", precision = 3)
@Temporal(TemporalType.TIMESTAMP)
@NotNull
private Date timeStamp;
}
@Entity
@Table(name = "ExampleObjMapEc")
@Data
@EqualsAndHashCode
public class JpaExampleObjMapEc implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
@VerifyKey
@NotNull
private ExampleKey key;
@ElementCollection
private Map<@NotNull String, @NotNull JpaExampleEmd> examples;
} |
...
MariaDB [controlloop]> SHOW CREATE TABLE JpaExampleObjMapEc_EXAMPLES;
+-----------------------------+-------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------------------------+-------------------------------------------------------------------------------------------------------+
| JpaExampleObjMapEc_EXAMPLES | CREATE TABLE `JpaExampleObjMapEc_EXAMPLES` (
`name` varchar(120) NOT NULL,
`version` varchar(20) NOT NULL,
`child_name` varchar(255) DEFAULT NULL,
`child_version` varchar(255) DEFAULT NULL,
`primed` bit(1) DEFAULT NULL,
`timeStamp` datetime(6) DEFAULT NULL,
`examples_KEY` varchar(255) NOT NULL,
PRIMARY KEY (`name`,`version`,`examples_KEY`),
CONSTRAINT `FKeu2s8p9mhstus32uliuwib8rr` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleObjMapEc` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-----------------------------+-------------------------------------------------------------------------------------------------------+
After saving the an example:
MariaDB [controlloop]> select * from ExampleObjMapEc;
+-----------------+---------+
| name | version |
+-----------------+---------+
| ExampleObjMapEc | 1.0.0 |
+-----------------+---------+
MariaDB [controlloop]> select * from JpaExampleObjMapEc_EXAMPLES;
+-----------------+---------+------------+---------------+--------+-----------+--------------+
| name | version | child_name | child_version | primed | timeStamp | examples_KEY |
+-----------------+---------+------------+---------------+--------+-----------+--------------+
| ExampleObjMapEc | 1.0.0 | example1 | 1.0.0 | NULL | NULL | MyKey1 |
| ExampleObjMapEc | 1.0.0 | example2 | 1.0.0 | NULL | NULL | MyKey2 |
+-----------------+---------+------------+---------------+--------+-----------+--------------+
List of Objects using ElementCollection
Code Block |
---|
language | java |
---|
title | JpaExampleObjListEc |
---|
collapse | true |
---|
|
@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;
} |
Eclipse-Link
MariaDB [controlloop]> describe ExampleObjListEc;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| name | varchar(120) | NO | PRI | NULL | |
| version | varchar(20) | NO | PRI | NULL | |
+---------+--------------+------+-----+---------+-------+
MariaDB [controlloop]> SHOW CREATE TABLE JpaExampleObjListEc_EXAMPLES;
+------------------------------+------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------------------------+------------------------------------------------------------------------------------------------------+
| JpaExampleObjListEc_EXAMPLES | CREATE TABLE `JpaExampleObjListEc_EXAMPLES` (
`name` varchar(120) DEFAULT NULL,
`version` varchar(20) DEFAULT NULL,
`EXAMPLES` longblob DEFAULT NULL,
KEY `FK_JpaExampleObjListEc_EXAMPLES_name` (`name`,`version`),
CONSTRAINT `FK_JpaExampleObjListEc_EXAMPLES_name` FOREIGN KEY (`name`, `version`)
REFERENCES `ExampleObjListEc` (`name`, `version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+------------------------------+------------------------------------------------------------------------------------------------------+
After saving an example:
MariaDB [controlloop]> select * from ExampleObjListEc;
+-------------+---------+
| name | version |
+-------------+---------+
| ExampleList | 1.0.0 |
+-------------+---------+
MariaDB [controlloop]> select * from JpaExampleObjListEc_EXAMPLES;
+-------------+---------+---------------------------+
| name | version | EXAMPLES |
+-------------+---------+---------------------------+
| ExampleList | 1.0.0 | .... <binary code> .... |
| ExampleList | 1.0.0 | .... <binary code> .... |
+-------------+---------+---------------------------+