HHH-6558 Bind @org.hibernate.annotations.Source

This commit is contained in:
Strong Liu 2012-02-07 15:56:02 -06:00
parent 9c243a0c68
commit ab578c78a5
2 changed files with 136 additions and 5 deletions

View File

@ -35,6 +35,8 @@ import org.hibernate.metamodel.internal.source.annotations.JandexHelper;
import org.hibernate.metamodel.internal.source.annotations.attribute.MappedAttribute;
/**
* Type Resolver which checks {@link org.hibernate.annotations.Type} to find the type info.
*
* @author Strong Liu
*/
public class AttributeTypeResolverImpl extends AbstractAttributeTypeResolver {
@ -46,11 +48,7 @@ public class AttributeTypeResolverImpl extends AbstractAttributeTypeResolver {
@Override
protected String resolveHibernateTypeName(AnnotationInstance typeAnnotation) {
String typeName = null;
if ( typeAnnotation != null ) {
typeName = JandexHelper.getValue( typeAnnotation, "type", String.class );
}
return typeName;
return typeAnnotation != null ? JandexHelper.getValue( typeAnnotation, "type", String.class ) : null;
}
@Override

View File

@ -0,0 +1,133 @@
/*
* 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.metamodel.internal.source.annotations.entity;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import org.junit.Test;
import org.hibernate.AnnotationException;
import org.hibernate.annotations.Source;
import org.hibernate.annotations.SourceType;
import org.hibernate.metamodel.spi.binding.AttributeBinding;
import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.metamodel.spi.binding.HibernateTypeDescriptor;
import org.hibernate.type.DbTimestampType;
import org.hibernate.type.LongType;
import org.hibernate.type.TimestampType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Strong Liu
*/
public class VersionBindingTests extends BaseAnnotationBindingTestCase {
@Entity
class Item1 {
@Id
long id;
long version;
}
@Test
@Resources(annotatedClasses = VersionBindingTests.Item1.class)
public void testNoVersionAnnotation() {
assertFalse( getEntityBinding( Item1.class ).isVersioned() );
}
@Entity
class Item2 {
@Id
private long id;
@Version
private Long version;
//we need add getters / setters due to HHH-6561
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
@Test
@Resources(annotatedClasses = VersionBindingTests.Item2.class)
public void testVersionTypeAttribute() {
EntityBinding binding = getEntityBinding( Item2.class );
assertTrue( binding.isVersioned() );
HibernateTypeDescriptor descriptor = binding.getHierarchyDetails()
.getVersioningAttributeBinding()
.getHibernateTypeDescriptor();
// assertEquals( "Long", descriptor.getExplicitTypeName() );
assertEquals( Long.class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( LongType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
}
@Entity
class Item3 {
@Id
Long id;
@Version
@Source(SourceType.DB)
Date version;
}
@Test
@Resources(annotatedClasses = VersionBindingTests.Item3.class)
public void testVersionAttributeWithSource() {
EntityBinding binding = getEntityBinding( Item3.class );
assertTrue( binding.isVersioned() );
HibernateTypeDescriptor descriptor = binding.getHierarchyDetails()
.getVersioningAttributeBinding()
.getHibernateTypeDescriptor();
assertEquals( "dbtimestamp", descriptor.getExplicitTypeName() );
assertEquals( Date.class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( DbTimestampType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
}
}