Skip to content

Commit

Permalink
HHH-13760 Fix ClassCastException when Envers inserts audit rows that …
Browse files Browse the repository at this point in the history
…use lazy many-to-one mappings
  • Loading branch information
Naros authored and dreab8 committed Jan 27, 2020
1 parent c5581e6 commit 92bd6f8
Show file tree
Hide file tree
Showing 11 changed files with 510 additions and 4 deletions.
Expand Up @@ -277,7 +277,9 @@ private void addOneToManyAttached(boolean fakeOneToManyBidirectional) {
// The mapper will only be used to map from entity to map, so no need to provide other details
// when constructing the PropertyData.
new PropertyData( auditMappedBy, null, null, null ),
referencingEntityName, false
referencingEntityName,
false,
false
);

final String positionMappedBy;
Expand Down
Expand Up @@ -28,6 +28,7 @@
*
* @author Adam Warski (adam at warski dot org)
* @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
* @author Chris Cranford
*/
public final class ToOneRelationMetadataGenerator {
private final AuditMetadataGenerator mainGenerator;
Expand Down Expand Up @@ -99,11 +100,13 @@ void addToOne(
parent.add( element );
}

boolean lazy = ( (ToOne) value ).isLazy();

// Adding mapper for the id
final PropertyData propertyData = propertyAuditingData.getPropertyData();
mapper.addComposite(
propertyData,
new ToOneIdMapper( relMapper, propertyData, referencedEntityName, nonInsertableFake )
new ToOneIdMapper( relMapper, propertyData, referencedEntityName, nonInsertableFake, lazy )
);
}

Expand Down
Expand Up @@ -19,26 +19,31 @@
import org.hibernate.envers.internal.tools.EntityTools;
import org.hibernate.envers.internal.tools.query.Parameters;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.proxy.HibernateProxy;

/**
* @author Adam Warski (adam at warski dot org)
* @author HernпїЅn Chanfreau
* @author Michal Skowronek (mskowr at o2 dot pl)
* @author Chris Cranford
*/
public class ToOneIdMapper extends AbstractToOneMapper {
private final IdMapper delegate;
private final String referencedEntityName;
private final boolean nonInsertableFake;
private final boolean lazyMapping;

public ToOneIdMapper(
IdMapper delegate,
PropertyData propertyData,
String referencedEntityName,
boolean nonInsertableFake) {
boolean nonInsertableFake,
boolean lazyMapping) {
super( delegate.getServiceRegistry(), propertyData );
this.delegate = delegate;
this.referencedEntityName = referencedEntityName;
this.nonInsertableFake = nonInsertableFake;
this.lazyMapping = lazyMapping;
}

@Override
Expand All @@ -49,10 +54,23 @@ public boolean mapToMapFromEntity(
Object oldObj) {
final HashMap<String, Object> newData = new HashMap<>();

Object oldObject = oldObj;
Object newObject = newObj;
if ( lazyMapping ) {
if ( nonInsertableFake && oldObject instanceof HibernateProxy ) {
oldObject = ( (HibernateProxy) oldObject ).getHibernateLazyInitializer().getImplementation();
}


if ( !nonInsertableFake && newObject instanceof HibernateProxy ) {
newObject = ( (HibernateProxy) newObject ).getHibernateLazyInitializer().getImplementation();
}
}

// If this property is originally non-insertable, but made insertable because it is in a many-to-one "fake"
// bi-directional relation, we always store the "old", unchaged data, to prevent storing changes made
// to this field. It is the responsibility of the collection to properly update it if it really changed.
delegate.mapToMapFromEntity( newData, nonInsertableFake ? oldObj : newObj );
delegate.mapToMapFromEntity( newData, nonInsertableFake ? oldObject : newObject );

for ( Map.Entry<String, Object> entry : newData.entrySet() ) {
data.put( entry.getKey(), entry.getValue() );
Expand Down
@@ -0,0 +1,55 @@
/*
* 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.manytoone.lazy;

import java.time.Instant;
import java.util.Collection;
import java.util.LinkedList;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
* @author Chris Cranford
*/
@Entity
@Table(name = "address")
public class Address extends BaseDomainEntity {
private static final long serialVersionUID = 7380477602657080463L;

@Column(name = "name")
private String name;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "id", cascade = CascadeType.ALL)
Collection<AddressVersion> versions = new LinkedList<>();

Address() {
}

Address(Instant when, String who, String name) {
super( when, who );
this.name = name;
}

public AddressVersion addInitialVersion(String description) {
AddressVersion version = new AddressVersion( getCreatedAt(), getCreatedBy(), this, 0, description );
versions.add( version );
return version;
}

public String getName() {
return name;
}

public Collection<AddressVersion> getVersions() {
return versions;
}
}
@@ -0,0 +1,61 @@
/*
* 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.manytoone.lazy;

import java.time.Instant;
import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
* @author Chris Cranford
*/
@Entity
@Table(name = "address_version")
public class AddressVersion extends BaseDomainEntityVersion {
private static final long serialVersionUID = 1100389518057335117L;

@Id
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "id", referencedColumnName = "id", updatable = false, nullable = false)
private Address id;

@Column(name = "description", updatable = false)
private String description;

AddressVersion() {
}

AddressVersion(Instant when, String who, Address id, long version, String description) {
setCreatedAt( when );
setCreatedBy( who );
setVersion( version );
this.id = Objects.requireNonNull(id );
this.description = description;
}

@Override
public Address getId() {
return id;
}

public String getDescription() {
return description;
}

public AddressVersion update(Instant when, String who, String description) {
AddressVersion version = new AddressVersion( when, who, id, getVersion() + 1, description );
id.versions.add( version );
return version;
}
}
@@ -0,0 +1,19 @@
/*
* 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.manytoone.lazy;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.MappedSuperclass;

/**
* @author Chris Cranford
*/
@MappedSuperclass
@Access(AccessType.FIELD)
public class Base {
}
@@ -0,0 +1,56 @@
/*
* 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.manytoone.lazy;

import java.time.Instant;
import java.util.Objects;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

/**
* @author Chris Cranford
*/
@MappedSuperclass
public abstract class BaseDomainEntity extends BaseDomainEntityMetadata {
private static final long serialVersionUID = 1023010094948580516L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected long id = 0;

BaseDomainEntity() {

}

BaseDomainEntity(Instant timestamp, String who) {
super( timestamp, who );
}

public long getId() {
return id;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BaseDomainEntity that = (BaseDomainEntity) o;
return id == that.id;
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
@@ -0,0 +1,55 @@
/*
* 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.manytoone.lazy;

import java.io.Serializable;
import java.time.Instant;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

import org.hibernate.annotations.CreationTimestamp;

/**
* @author Chris Cranford
*/
@MappedSuperclass
public abstract class BaseDomainEntityMetadata extends Base implements Serializable {
private static final long serialVersionUID = 2765056578095518489L;

@Column(name = "created_by", nullable = false, updatable = false)
private String createdBy;

@CreationTimestamp
@Column(name = "created_at", nullable = false, updatable = false)
private Instant createdAt;

BaseDomainEntityMetadata() {

}

BaseDomainEntityMetadata(Instant timestamp, String who) {
this.createdBy = who;
this.createdAt = timestamp;
}

public String getCreatedBy() {
return createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

public Instant getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}
}
@@ -0,0 +1,52 @@
/*
* 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.manytoone.lazy;

import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

/**
* @author Chris Cranford
*/
@MappedSuperclass
public abstract class BaseDomainEntityVersion extends BaseDomainEntityMetadata {
private static final long serialVersionUID = 1564895954324242368L;

@Id
@Column(name = "version", nullable = false, updatable = false)
private long version;

public long getVersion() {
return version;
}

public void setVersion(long version) {
this.version = version;
}

public abstract Object getId();

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BaseDomainEntityVersion that = (BaseDomainEntityVersion) o;
return Objects.equals(getId(), that.getId()) && version == that.version;
}

@Override
public int hashCode() {
return Objects.hash(getId(), version);
}
}

0 comments on commit 92bd6f8

Please sign in to comment.