HHH-5821 - JPA EntityType's (or ManagedType's) getSingularAttributes() returns the version attribute with isVersion set to false.

This commit is contained in:
Steve Ebersole 2011-01-21 14:05:16 -06:00 committed by JPAV
parent 4aa9cbe5b7
commit 1be9fc80c9
5 changed files with 88 additions and 2 deletions

View File

@ -259,6 +259,7 @@ public abstract class AbstractIdentifiableType<X>
public void applyVersionAttribute(SingularAttributeImpl<X, ?> versionAttribute) {
AbstractIdentifiableType.this.version = versionAttribute;
managedBuilder.addAttribute( versionAttribute );
}
public void addAttribute(Attribute<X, ?> attribute) {

View File

@ -178,6 +178,10 @@ class MetadataContext {
// #buildIdClassAttributes
continue;
}
if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
// skip the version property, it was already handled previously.
continue;
}
final Attribute attribute = attributeFactory.buildAttribute( jpa2Mapping, property );
if ( attribute != null ) {
jpa2Mapping.getBuilder().addAttribute( attribute );
@ -203,6 +207,10 @@ class MetadataContext {
Iterator<Property> properties = safeMapping.getDeclaredPropertyIterator();
while ( properties.hasNext() ) {
final Property property = properties.next();
if ( safeMapping.isVersioned() && property == safeMapping.getVersion() ) {
// skip the version property, it was already handled previously.
continue;
}
final Attribute attribute = attributeFactory.buildAttribute( jpa2Mapping, property );
if ( attribute != null ) {
jpa2Mapping.getBuilder().addAttribute( attribute );

View File

@ -35,7 +35,7 @@ public abstract class AbstractMetamodelSpecificTest extends TestCase {
return new Class[] {
Address.class, Alias.class, Country.class, CreditCard.class, Customer.class,
Info.class, LineItem.class, Order.class, Phone.class, Product.class,
ShelfLife.class, Spouse.class
ShelfLife.class, Spouse.class, VersionedEntity.class
};
}
}

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.ejb.metamodel;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.SingularAttribute;
import org.hibernate.ejb.test.TestCase;
@ -35,7 +36,7 @@ public class EmbeddedTypeTest extends TestCase {
@Override
public Class[] getAnnotatedClasses() {
return new Class[] {
Product.class, ShelfLife.class
Product.class, ShelfLife.class, VersionedEntity.class
};
}
@ -53,4 +54,16 @@ public class EmbeddedTypeTest extends TestCase {
em.getTransaction().commit();
em.close();
}
public void testVersionAttributeMetadata() {
// HHH-5821
EntityManager em = getOrCreateEntityManager();
EntityType<VersionedEntity> metadata = em.getMetamodel().entity( VersionedEntity.class );
assertNotNull( metadata.getDeclaredVersion( int.class ) );
assertTrue( metadata.getDeclaredVersion( int.class ).isVersion() );
assertEquals( 3, metadata.getDeclaredSingularAttributes().size() );
assertTrue( metadata.getDeclaredSingularAttributes().contains( metadata.getDeclaredVersion( int.class ) ) );
em.close();
}
}

View File

@ -0,0 +1,64 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. 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 Inc.
*
* 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.ejb.metamodel;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
/**
* @author Steve Ebersole
*/
@Entity
public class VersionedEntity {
private String id;
private String name;
private int version;
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Version
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}