Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Envers] HHH-14263 Skip audit non-insertable, non-updateable components #3598

Open
wants to merge 2 commits into
base: 5.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,10 @@ private void addProperties(
// Verifies if a mapping exists using a @JoinColumn against a @NaturalId field
// and if so, this eliminates the mapping property as it isn't needed.
if ( property instanceof SyntheticProperty ) {
if ( property.getValue().isAlternateUniqueKey() ) {
if ( property.getValue().isAlternateUniqueKey() ||
// HHH-14263
// If synthetic property is non-insertable, non-updatable, then no need to audit it
!(property.isInsertable() || property.isUpdateable()) ) {
continue;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.hibernate.envers.test.entities.collection;

import org.hibernate.envers.Audited;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Set;

@Audited
@Entity
@Table(name = "foo")
public class ElementCollectionMultipleJoinColumnEntity implements Serializable {
@Id
@Column(name = "foo_id")
private String fooId;

@ElementCollection
@Column(name = "bar_id")
@CollectionTable(
name = "foos_bars_mapping",
joinColumns = {
@JoinColumn(name = "bazId", referencedColumnName = "baz_id"),
@JoinColumn(name = "fooId", referencedColumnName = "foo_id")
}
)
private Set<String> barIds;

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

public String getFooId() {
return fooId;
}

public void setFooId(String fooId) {
this.fooId = fooId;
}

public Set<String> getBarIds() {
return barIds;
}

public void setBarIds(Set<String> barIds) {
this.barIds = barIds;
}

public String getBazId() {
return bazId;
}

public void setBazId(String bazId) {
this.bazId = bazId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.hibernate.envers.test.integration.collection;

import org.hibernate.envers.AuditReader;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.entities.collection.ElementCollectionMultipleJoinColumnEntity;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;

import javax.persistence.EntityManager;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;

@TestForIssue(jiraKey = "HHH-14263")
public class ElementCollectionMultipleJoinColumnsTest extends BaseEnversJPAFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { ElementCollectionMultipleJoinColumnEntity.class };
}

@Test
public void testAuditForElementCollectionMultipleJoinColumnsWorks() {
AuditReader reader = getAuditReader();
EntityManager em = getEntityManager();
Set<String> barIds = new HashSet<>();
barIds.add("bar1");
em.getTransaction().begin();
ElementCollectionMultipleJoinColumnEntity entity = new ElementCollectionMultipleJoinColumnEntity();
entity.setFooId("foo1");
entity.setBarIds(new HashSet<>(barIds));
entity.setBazId("baz1");
em.persist(entity);
em.getTransaction().commit();

Set<String> barIds2 = new HashSet<>();
barIds2.add("bar1");
barIds2.add("bar2");
em.getTransaction().begin();
ElementCollectionMultipleJoinColumnEntity entity2 = new ElementCollectionMultipleJoinColumnEntity();
entity2.setFooId("foo1");
entity2.setBarIds(new HashSet<>(barIds2));
entity2.setBazId("baz1");
em.merge(entity2);
em.getTransaction().commit();

assertEquals(barIds, reader.find(ElementCollectionMultipleJoinColumnEntity.class, "foo1", 1).getBarIds());
assertEquals(barIds2, reader.find(ElementCollectionMultipleJoinColumnEntity.class, "foo1", 2).getBarIds());
}
}