Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-13760 Fix ClassCastException when Envers inserts audit rows that …
…use lazy many-to-one mappings
- Loading branch information
Showing
11 changed files
with
510 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
55 changes: 55 additions & 0 deletions
55
...te-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/Address.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,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; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...rs/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/AddressVersion.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,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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...rnate-envers/src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/Base.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,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 { | ||
} |
56 changes: 56 additions & 0 deletions
56
.../src/test/java/org/hibernate/envers/test/integration/manytoone/lazy/BaseDomainEntity.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,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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...t/java/org/hibernate/envers/test/integration/manytoone/lazy/BaseDomainEntityMetadata.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,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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...st/java/org/hibernate/envers/test/integration/manytoone/lazy/BaseDomainEntityVersion.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,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); | ||
} | ||
} |
Oops, something went wrong.