HHH-5883: Fixing - @Lob annotated field throws MappingException when the field type is serializable

This commit is contained in:
adamw 2011-01-31 12:26:26 +01:00
parent 595f919e4c
commit d8772c04a8
3 changed files with 167 additions and 5 deletions

View File

@ -33,10 +33,7 @@ import org.hibernate.envers.configuration.metadata.reader.PropertyAuditingData;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Value;
import org.hibernate.type.BasicType;
import org.hibernate.type.CompositeCustomType;
import org.hibernate.type.CustomType;
import org.hibernate.type.Type;
import org.hibernate.type.*;
/**
* Generates metadata for basic properties: immutable types (including enums).
@ -48,7 +45,8 @@ public final class BasicMetadataGenerator {
Value value, SimpleMapperBuilder mapper, boolean insertable, boolean key) {
Type type = value.getType();
if (type instanceof BasicType || "org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName())) {
if (type instanceof BasicType || type instanceof SerializableToBlobType ||
"org.hibernate.type.PrimitiveByteArrayBlobType".equals(type.getClass().getName())) {
if (parent != null) {
boolean addNestedType = (value instanceof SimpleValue) && ((SimpleValue) value).getTypeParameters() != null;

View File

@ -0,0 +1,92 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.data;
import org.hibernate.envers.Audited;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
public class LobSerializableTestEntity {
@Id
@GeneratedValue
private Integer id;
@Audited
@Lob
private SerObject obj;
public LobSerializableTestEntity() {
}
public LobSerializableTestEntity(SerObject obj) {
this.obj = obj;
}
public LobSerializableTestEntity(Integer id, SerObject obj) {
this.obj = obj;
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public SerObject getObj() {
return obj;
}
public void setObj(SerObject obj) {
this.obj = obj;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof LobSerializableTestEntity)) return false;
LobSerializableTestEntity that = (LobSerializableTestEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (obj != null ? !obj.equals(that.obj) : that.obj != null) return false;
return true;
}
public int hashCode() {
int result;
result = (id != null ? id.hashCode() : 0);
result = 31 * result + (obj != null ? obj.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,72 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.test.integration.data;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import javax.persistence.EntityManager;
import java.util.Arrays;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class LobSerializables extends AbstractEntityTest {
private Integer id1;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(LobSerializableTestEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
EntityManager em = getEntityManager();
em.getTransaction().begin();
LobSerializableTestEntity ste = new LobSerializableTestEntity(new SerObject("d1"));
em.persist(ste);
id1 = ste.getId();
em.getTransaction().commit();
em.getTransaction().begin();
ste = em.find(LobSerializableTestEntity.class, id1);
ste.setObj(new SerObject("d2"));
em.getTransaction().commit();
}
@Test
public void testRevisionsCounts() {
assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(LobSerializableTestEntity.class, id1));
}
@Test
public void testHistoryOfId1() {
LobSerializableTestEntity ver1 = new LobSerializableTestEntity(id1, new SerObject("d1"));
LobSerializableTestEntity ver2 = new LobSerializableTestEntity(id1, new SerObject("d2"));
assert getAuditReader().find(LobSerializableTestEntity.class, id1, 1).equals(ver1);
assert getAuditReader().find(LobSerializableTestEntity.class, id1, 2).equals(ver2);
}
}