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
This commit is contained in:
parent
87ce2670e7
commit
b9b956cd2c
|
@ -1,6 +1,8 @@
|
|||
[[dynamic-model]]
|
||||
=== Dynamic Model
|
||||
:sourcedir: extras
|
||||
:sourcedir: ../../../../../test/java/org/hibernate/userguide/mapping/dynamic
|
||||
:mappingdir: ../../../../../test/resources/org/hibernate/userguide/mapping/dynamic
|
||||
:extrasdir: extras
|
||||
|
||||
[IMPORTANT]
|
||||
====
|
||||
|
@ -12,18 +14,47 @@ On the other hand, Hibernate can work with both POJO entities as well as with dy
|
|||
==== Dynamic mapping models
|
||||
|
||||
Persistent entities do not necessarily have to be represented as POJO/JavaBean classes.
|
||||
Hibernate also supports dynamic models (using `Map`s of `Map`s at runtime).
|
||||
Hibernate also supports dynamic models (using `Map` of `Maps` at runtime).
|
||||
With this approach, you do not write persistent classes, only mapping files.
|
||||
|
||||
A given entity has just one entity mode within a given SessionFactory.
|
||||
This is a change from previous versions which allowed to define multiple entity modes for an entity and to select which to load.
|
||||
Entity modes can now be mixed within a domain model; a dynamic entity might reference a POJO entity, and vice versa.
|
||||
|
||||
.Working with Dynamic Domain Models
|
||||
[[mapping-model-dynamic-example]]
|
||||
.Dynamic domain model Hibernate mapping
|
||||
====
|
||||
[source,xml]
|
||||
----
|
||||
include::{mappingdir}/Book.hbm.xml[tag=mapping-model-dynamic-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
After you defined your entity mapping, you need to instruct Hibernate to use the dynamic mapping mode:
|
||||
|
||||
[[mapping-model-dynamic-setting-example]]
|
||||
.Dynamic domain model Hibernate mapping
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{sourcedir}/dynamic/listing10.java[]
|
||||
include::{sourcedir}/DynamicEntityTest.java[tag=mapping-model-dynamic-setting-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
When you are going to save the following `Book` dynamic entity,
|
||||
Hibernate is going to generate the following SQL statement:
|
||||
|
||||
[[mapping-model-dynamic-setting-example]]
|
||||
.Persist dynamic entity
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{sourcedir}/DynamicEntityTest.java[tag=mapping-model-dynamic-example, indent=0]
|
||||
----
|
||||
|
||||
[source,sql]
|
||||
----
|
||||
include::{extrasdir}/dynamic/mapping-model-dynamic-example.sql[indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
Session s = openSession();
|
||||
Transaction tx = s.beginTransaction();
|
||||
|
||||
// Create a customer entity
|
||||
Map<String, String>david = new HashMap<>();
|
||||
david.put( "name","David" );
|
||||
|
||||
// Create an organization entity
|
||||
Map<String, String>foobar = new HashMap<>();
|
||||
foobar.put( "name","Foobar Inc." );
|
||||
|
||||
// Link both
|
||||
david.put( "organization",foobar );
|
||||
|
||||
// Save both
|
||||
s.save( "Customer",david );
|
||||
s.save( "Organization",foobar );
|
||||
|
||||
tx.commit();
|
||||
s.close();
|
|
@ -0,0 +1,10 @@
|
|||
insert
|
||||
into
|
||||
Book
|
||||
(title, author, isbn)
|
||||
values
|
||||
(?, ?, ?)
|
||||
|
||||
-- binding parameter [1] as [VARCHAR] - [High-Performance Java Persistence]
|
||||
-- binding parameter [2] as [VARCHAR] - [Vlad Mihalcea]
|
||||
-- binding parameter [3] as [VARCHAR] - [978-9730228236]
|
|
@ -0,0 +1,46 @@
|
|||
package org.hibernate.userguide.mapping.dynamic;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
//tag::access-field-mapping-example[]
|
||||
@Entity(name = "Book")
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String author;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::access-field-mapping-example[]
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
//tag::access-field-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.dynamic;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.EntityMode;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class DynamicEntityTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected String[] getMappings() {
|
||||
return new String[] {
|
||||
"org/hibernate/userguide/mapping/dynamic/Book.hbm.xml"
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map buildSettings() {
|
||||
Map settings = super.buildSettings();
|
||||
//tag::mapping-model-dynamic-setting-example[]
|
||||
settings.put( "hibernate.default_entity_mode", "dynamic-map" );
|
||||
//end::mapping-model-dynamic-setting-example[]
|
||||
return settings;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
//tag::mapping-model-dynamic-example[]
|
||||
|
||||
Map<String, String> book = new HashMap<>();
|
||||
book.put( "isbn", "978-9730228236" );
|
||||
book.put( "title", "High-Performance Java Persistence" );
|
||||
book.put( "author", "Vlad Mihalcea" );
|
||||
|
||||
entityManager
|
||||
.unwrap(Session.class)
|
||||
.save( "Book", book );
|
||||
//end::mapping-model-dynamic-example[]
|
||||
} );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0"?>
|
||||
<!--
|
||||
~ Hibernate, Relational Persistence for Idiomatic Java
|
||||
~
|
||||
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<!--tag::mapping-model-dynamic-example[]-->
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping>
|
||||
<class entity-name="Book">
|
||||
<id name="isbn" column="isbn" length="32" type="string"/>
|
||||
|
||||
<property name="title" not-null="true" length="50" type="string"/>
|
||||
|
||||
<property name="author" not-null="true" length="50" type="string"/>
|
||||
|
||||
</class>
|
||||
</hibernate-mapping>
|
||||
<!--end::mapping-model-dynamic-example[]-->
|
Loading…
Reference in New Issue