Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
292 additions
and
0 deletions.
There are no files selected for viewing
239 changes: 239 additions & 0 deletions
239
.../java/org/hibernate/envers/test/integration/collection/EntityMapCompositeElementTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
/* | ||
* 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.envers.test.integration.collection; | ||
|
||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.hibernate.envers.AuditReader; | ||
import org.hibernate.envers.Audited; | ||
import org.hibernate.envers.query.AuditEntity; | ||
import org.hibernate.envers.query.AuditQuery; | ||
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; | ||
import org.hibernate.envers.test.Priority; | ||
import org.junit.Test; | ||
|
||
import org.hibernate.testing.transaction.TransactionUtil; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
/** | ||
* @author Chris Cranford | ||
*/ | ||
public class EntityMapCompositeElementTest extends BaseEnversJPAFunctionalTestCase { | ||
|
||
private Category category; | ||
private Item item; | ||
|
||
@Test | ||
@Priority(10) | ||
public void initData() { | ||
TransactionUtil.doInJPA( this::entityManagerFactory, entityManager-> { | ||
final Item item = new Item( "The Item" ); | ||
entityManager.persist( item ); | ||
|
||
final Category category = new Category( "The Category" ); | ||
category.setDescription( "The description" ); | ||
category.setValue( item, new Value( "The Value", 4711L ) ); | ||
category.setText( item, "The text" ); | ||
entityManager.persist( category ); | ||
|
||
this.category = category; | ||
this.item = item; | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testRevisionHistory() { | ||
final AuditReader reader = getAuditReader(); | ||
|
||
AuditQuery categoryQuery = reader.createQuery().forRevisionsOfEntity( Category.class, false, true ) | ||
.addOrder( AuditEntity.revisionProperty( "timestamp" ).asc() ) | ||
.add( AuditEntity.id().eq( category.getId() ) ); | ||
|
||
@SuppressWarnings( "unchecked" ) | ||
List<Object[]> history = (List<Object[]>) categoryQuery.getResultList(); | ||
assertNotNull( history ); | ||
assertEquals( 1, history.size() ); | ||
|
||
final Category category = (Category) reader.createQuery().forEntitiesAtRevision( Category.class, 1 ) | ||
.add( AuditEntity.property( "id" ).eq( this.category.getId() ) ) | ||
.setMaxResults( 1 ) | ||
.getSingleResult(); | ||
|
||
assertEquals( this.category.getName(), category.getName() ); | ||
assertEquals( this.category.getDescription(), category.getDescription() ); | ||
assertEquals( "The text", category.getText( this.item ) ); | ||
|
||
final Value value = category.getValue( this.item ); | ||
assertEquals( "The Value", value.getText() ); | ||
assertEquals( Long.valueOf( 4711L ), value.getNumber() ); | ||
} | ||
|
||
@Override | ||
protected String[] getMappings() { | ||
return new String[] { | ||
"mappings/collections/Category.hbm.xml", | ||
"mappings/collections/Item.hbm.xml" | ||
}; | ||
} | ||
|
||
@Audited | ||
public static class Category { | ||
private Long id; | ||
private String name; | ||
private String description; | ||
private Map<Item, String> textItem = new HashMap<>(); | ||
private Map<Item, Value> categoryItem = new HashMap<>(); | ||
|
||
Category() { | ||
|
||
} | ||
|
||
Category(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public Map<Item, String> getTextItem() { | ||
return textItem; | ||
} | ||
|
||
public void setTextItem(Map<Item, String> textItem) { | ||
this.textItem = textItem; | ||
} | ||
|
||
public Map<Item, Value> getCategoryItem() { | ||
return categoryItem; | ||
} | ||
|
||
public void setCategoryItem(Map<Item, Value> categoryItem) { | ||
this.categoryItem = categoryItem; | ||
} | ||
|
||
public void setValue(Item key, Value value) { | ||
this.categoryItem.put( key, value ); | ||
} | ||
|
||
public Value getValue(Item key) { | ||
return this.categoryItem.get( key ); | ||
} | ||
|
||
public void setText(Item key, String value) { | ||
this.textItem.put( key, value ); | ||
} | ||
|
||
public String getText(Item key) { | ||
return this.textItem.get( key ); | ||
} | ||
} | ||
|
||
@Audited | ||
public static class Item { | ||
private Long id; | ||
private String name; | ||
|
||
Item() { | ||
|
||
} | ||
|
||
Item(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if ( this == o ) { | ||
return true; | ||
} | ||
if ( o == null || getClass() != o.getClass() ) { | ||
return false; | ||
} | ||
|
||
Item item = (Item) o; | ||
|
||
return id != null ? id.equals( item.id ) : item.id == null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return id != null ? id.hashCode() : 0; | ||
} | ||
} | ||
|
||
public static class Value implements Serializable { | ||
private String text; | ||
private Long number; | ||
|
||
Value() { | ||
|
||
} | ||
|
||
Value(String text, Long number) { | ||
this.text = text; | ||
this.number = number; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
public void setText(String text) { | ||
this.text = text; | ||
} | ||
|
||
public Long getNumber() { | ||
return number; | ||
} | ||
|
||
public void setNumber(Long number) { | ||
this.number = number; | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
hibernate-envers/src/test/resources/mappings/collections/Category.hbm.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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>. | ||
--> | ||
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> | ||
<hibernate-mapping default-access="field"> | ||
<class name="org.hibernate.envers.test.integration.collection.EntityMapCompositeElementTest$Category" table="category"> | ||
<id name="id" column="id" type="long"> | ||
<generator class="sequence"> | ||
<param name="sequence_name">category_id_seq</param> | ||
</generator> | ||
</id> | ||
|
||
<property name="name" column="name" not-null="true" /> | ||
<property name="description" column="description" /> | ||
|
||
<map name="textItem" table="text_item"> | ||
<key column="category_id" not-null="true" /> | ||
<map-key-many-to-many column="item_id" class="org.hibernate.envers.test.integration.collection.EntityMapCompositeElementTest$Item" /> | ||
<element type="string" column="text" /> | ||
</map> | ||
|
||
<map name="categoryItem" table="category_item"> | ||
<key column="category_id" not-null="true" /> | ||
<map-key-many-to-many column="item_id" class="org.hibernate.envers.test.integration.collection.EntityMapCompositeElementTest$Item" /> | ||
<composite-element class="org.hibernate.envers.test.integration.collection.EntityMapCompositeElementTest$Value"> | ||
<property name="number" column="number" /> | ||
<property name="text" column="text" /> | ||
</composite-element> | ||
</map> | ||
</class> | ||
</hibernate-mapping> |
18 changes: 18 additions & 0 deletions
18
hibernate-envers/src/test/resources/mappings/collections/Item.hbm.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?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>. | ||
--> | ||
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> | ||
<hibernate-mapping default-access="field"> | ||
<class name="org.hibernate.envers.test.integration.collection.EntityMapCompositeElementTest$Item" table="item"> | ||
<id name="id" column="id" type="long"> | ||
<generator class="sequence"> | ||
<param name="sequence_name">item_id_seq</param> | ||
</generator> | ||
</id> | ||
<property name="name" column="name" not-null="true" /> | ||
</class> | ||
</hibernate-mapping> |