HHH-4899:

- fixing mutable property types in composite ids
- test

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18791 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Adam Warski 2010-02-12 12:47:15 +00:00
parent d88f65ff0c
commit ecf4f4fa66
4 changed files with 263 additions and 6 deletions

View File

@ -61,12 +61,12 @@ public final class IdMetadataGenerator {
Property property = properties.next();
Type propertyType = property.getType();
if (!"_identifierMapper".equals(property.getName())) {
if (!propertyType.isMutable()) {
// Last but one parameter: ids are always insertable
mainGenerator.getBasicMetadataGenerator().addBasic(parent,
getIdPersistentPropertyAuditingData(property),
property.getValue(), mapper, true, key);
} else {
// Last but one parameter: ids are always insertable
boolean added = mainGenerator.getBasicMetadataGenerator().addBasic(parent,
getIdPersistentPropertyAuditingData(property),
property.getValue(), mapper, true, key);
if (!added) {
throw new MappingException("Type not supported: " + propertyType.getClass().getName());
}
}

View File

@ -0,0 +1,89 @@
/*
* 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.entities.ids;
import org.hibernate.envers.Audited;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Entity
public class CompositeDateIdTestEntity {
@EmbeddedId
private DateEmbId id;
@Audited
private String str1;
public CompositeDateIdTestEntity() {
}
public CompositeDateIdTestEntity(String str1) {
this.str1 = str1;
}
public CompositeDateIdTestEntity(DateEmbId id, String str1) {
this.id = id;
this.str1 = str1;
}
public DateEmbId getId() {
return id;
}
public void setId(DateEmbId id) {
this.id = id;
}
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CompositeDateIdTestEntity that = (CompositeDateIdTestEntity) o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (str1 != null ? !str1.equals(that.str1) : that.str1 != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (str1 != null ? str1.hashCode() : 0);
return result;
}
}

View File

@ -0,0 +1,84 @@
/*
* 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.entities.ids;
import javax.persistence.Embeddable;
import java.io.Serializable;
import java.util.Date;
/**
* @author Adam Warski (adam at warski dot org)
*/
@Embeddable
public class DateEmbId implements Serializable {
private Date x;
private Date y;
public DateEmbId() {
}
public DateEmbId(Date x, Date y) {
this.x = x;
this.y = y;
}
public Date getX() {
return x;
}
public void setX(Date x) {
this.x = x;
}
public Date getY() {
return y;
}
public void setY(Date y) {
this.y = y;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DateEmbId)) return false;
DateEmbId embId = (DateEmbId) o;
if (x != null ? !x.equals(embId.x) : embId.x != null) return false;
if (y != null ? !y.equals(embId.y) : embId.y != null) return false;
return true;
}
public int hashCode() {
int result;
result = (x != null ? x.hashCode() : 0);
result = 31 * result + (y != null ? y.hashCode() : 0);
return result;
}
public String toString() {
return "DateEmbId(" + x + ", " + y + ")";
}
}

View File

@ -0,0 +1,84 @@
/*
* 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.ids;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.entities.ids.CompositeDateIdTestEntity;
import org.hibernate.envers.test.entities.ids.DateEmbId;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import javax.persistence.EntityManager;
import java.util.Arrays;
import java.util.Date;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class CompositeDateId extends AbstractEntityTest {
private DateEmbId id1;
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(CompositeDateIdTestEntity.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
CompositeDateIdTestEntity dite = new CompositeDateIdTestEntity(new DateEmbId(new Date(), new Date()), "x");
em.persist(dite);
id1 = dite.getId();
em.getTransaction().commit();
// Revision 2
em = getEntityManager();
em.getTransaction().begin();
dite = em.find(CompositeDateIdTestEntity.class, id1);
dite.setStr1("y");
em.getTransaction().commit();
}
@Test
public void testRevisionsCounts() {
assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(CompositeDateIdTestEntity.class, id1));
}
@Test
public void testHistoryOfId1() {
CompositeDateIdTestEntity ver1 = new CompositeDateIdTestEntity(id1, "x");
CompositeDateIdTestEntity ver2 = new CompositeDateIdTestEntity(id1, "y");
assert getAuditReader().find(CompositeDateIdTestEntity.class, id1, 1).getStr1().equals("x");
assert getAuditReader().find(CompositeDateIdTestEntity.class, id1, 2).getStr1().equals("y");
}
}