Skip to content

Commit b9b956c

Browse files
committed
HHH-11290 - Migrate all documentation snippets that derive the source code from extras instead of actual Unit Tests
Fixed in the Dynamic entity mapping chapter
1 parent 87ce267 commit b9b956c

File tree

6 files changed

+171
-24
lines changed

6 files changed

+171
-24
lines changed

documentation/src/main/asciidoc/userguide/chapters/domain/dynamic_model.adoc

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[[dynamic-model]]
22
=== Dynamic Model
3-
:sourcedir: extras
3+
:sourcedir: ../../../../../test/java/org/hibernate/userguide/mapping/dynamic
4+
:mappingdir: ../../../../../test/resources/org/hibernate/userguide/mapping/dynamic
5+
:extrasdir: extras
46

57
[IMPORTANT]
68
====
@@ -12,18 +14,47 @@ On the other hand, Hibernate can work with both POJO entities as well as with dy
1214
==== Dynamic mapping models
1315

1416
Persistent entities do not necessarily have to be represented as POJO/JavaBean classes.
15-
Hibernate also supports dynamic models (using `Map`s of `Map`s at runtime).
17+
Hibernate also supports dynamic models (using `Map` of `Maps` at runtime).
1618
With this approach, you do not write persistent classes, only mapping files.
1719

1820
A given entity has just one entity mode within a given SessionFactory.
1921
This is a change from previous versions which allowed to define multiple entity modes for an entity and to select which to load.
2022
Entity modes can now be mixed within a domain model; a dynamic entity might reference a POJO entity, and vice versa.
2123

22-
.Working with Dynamic Domain Models
24+
[[mapping-model-dynamic-example]]
25+
.Dynamic domain model Hibernate mapping
26+
====
27+
[source,xml]
28+
----
29+
include::{mappingdir}/Book.hbm.xml[tag=mapping-model-dynamic-example, indent=0]
30+
----
31+
====
32+
33+
After you defined your entity mapping, you need to instruct Hibernate to use the dynamic mapping mode:
34+
35+
[[mapping-model-dynamic-setting-example]]
36+
.Dynamic domain model Hibernate mapping
37+
====
38+
[source,java]
39+
----
40+
include::{sourcedir}/DynamicEntityTest.java[tag=mapping-model-dynamic-setting-example, indent=0]
41+
----
42+
====
43+
44+
When you are going to save the following `Book` dynamic entity,
45+
Hibernate is going to generate the following SQL statement:
46+
47+
[[mapping-model-dynamic-setting-example]]
48+
.Persist dynamic entity
2349
====
2450
[source,java]
2551
----
26-
include::{sourcedir}/dynamic/listing10.java[]
52+
include::{sourcedir}/DynamicEntityTest.java[tag=mapping-model-dynamic-example, indent=0]
53+
----
54+
55+
[source,sql]
56+
----
57+
include::{extrasdir}/dynamic/mapping-model-dynamic-example.sql[indent=0]
2758
----
2859
====
2960

documentation/src/main/asciidoc/userguide/chapters/domain/extras/dynamic/listing10.java

Lines changed: 0 additions & 20 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
insert
2+
into
3+
Book
4+
(title, author, isbn)
5+
values
6+
(?, ?, ?)
7+
8+
-- binding parameter [1] as [VARCHAR] - [High-Performance Java Persistence]
9+
-- binding parameter [2] as [VARCHAR] - [Vlad Mihalcea]
10+
-- binding parameter [3] as [VARCHAR] - [978-9730228236]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.hibernate.userguide.mapping.dynamic;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
//tag::access-field-mapping-example[]
8+
@Entity(name = "Book")
9+
public class Book {
10+
11+
@Id
12+
@GeneratedValue
13+
private Long id;
14+
15+
private String title;
16+
17+
private String author;
18+
19+
//Getters and setters are omitted for brevity
20+
//end::access-field-mapping-example[]
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public String getTitle() {
31+
return title;
32+
}
33+
34+
public void setTitle(String title) {
35+
this.title = title;
36+
}
37+
38+
public String getAuthor() {
39+
return author;
40+
}
41+
42+
public void setAuthor(String author) {
43+
this.author = author;
44+
}
45+
//tag::access-field-mapping-example[]
46+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.userguide.mapping.dynamic;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
import org.hibernate.EntityMode;
13+
import org.hibernate.Session;
14+
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
15+
16+
import org.junit.Test;
17+
18+
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
19+
20+
/**
21+
* @author Vlad Mihalcea
22+
*/
23+
public class DynamicEntityTest extends BaseEntityManagerFunctionalTestCase {
24+
25+
@Override
26+
protected String[] getMappings() {
27+
return new String[] {
28+
"org/hibernate/userguide/mapping/dynamic/Book.hbm.xml"
29+
};
30+
}
31+
32+
@Override
33+
protected Map buildSettings() {
34+
Map settings = super.buildSettings();
35+
//tag::mapping-model-dynamic-setting-example[]
36+
settings.put( "hibernate.default_entity_mode", "dynamic-map" );
37+
//end::mapping-model-dynamic-setting-example[]
38+
return settings;
39+
}
40+
41+
@Test
42+
public void test() {
43+
doInJPA( this::entityManagerFactory, entityManager -> {
44+
//tag::mapping-model-dynamic-example[]
45+
46+
Map<String, String> book = new HashMap<>();
47+
book.put( "isbn", "978-9730228236" );
48+
book.put( "title", "High-Performance Java Persistence" );
49+
book.put( "author", "Vlad Mihalcea" );
50+
51+
entityManager
52+
.unwrap(Session.class)
53+
.save( "Book", book );
54+
//end::mapping-model-dynamic-example[]
55+
} );
56+
}
57+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Hibernate, Relational Persistence for Idiomatic Java
4+
~
5+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
6+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
7+
-->
8+
<!--tag::mapping-model-dynamic-example[]-->
9+
<!DOCTYPE hibernate-mapping PUBLIC
10+
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
11+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
12+
13+
<hibernate-mapping>
14+
<class entity-name="Book">
15+
<id name="isbn" column="isbn" length="32" type="string"/>
16+
17+
<property name="title" not-null="true" length="50" type="string"/>
18+
19+
<property name="author" not-null="true" length="50" type="string"/>
20+
21+
</class>
22+
</hibernate-mapping>
23+
<!--end::mapping-model-dynamic-example[]-->

0 commit comments

Comments
 (0)