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

This commit is contained in:
Strong Liu 2012-02-07 13:27:23 -06:00
parent 0e36f65852
commit 9c243a0c68
2 changed files with 35 additions and 5 deletions

View File

@ -36,6 +36,7 @@ import org.jboss.jandex.DotName;
import org.hibernate.AnnotationException;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.SourceType;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.spi.binding.IdGenerator;
@ -70,6 +71,9 @@ public class BasicAttribute extends MappedAttribute {
*/
private final boolean isVersioned;
private final SourceType versionSourceType;
/**
* Is this property lazy loaded (see {@link javax.persistence.Basic}).
*/
@ -110,6 +114,22 @@ public class BasicAttribute extends MappedAttribute {
AnnotationInstance versionAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.VERSION );
isVersioned = versionAnnotation != null;
if ( isVersioned ) {
AnnotationInstance sourceAnnotation = JandexHelper.getSingleAnnotation(
annotations,
HibernateDotNames.SOURCE
);
if ( sourceAnnotation != null ) {
versionSourceType = JandexHelper.getEnumValue( sourceAnnotation, "value", SourceType.class );
}
else {
versionSourceType = null;
}
}
else {
versionSourceType = null;
}
if ( isId() ) {
// an id must be unique and cannot be nullable
getColumnValues().setUnique( true );
@ -170,6 +190,11 @@ public class BasicAttribute extends MappedAttribute {
return idGenerator;
}
public SourceType getVersionSourceType() {
return versionSourceType;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();

View File

@ -32,9 +32,11 @@ import org.jboss.jandex.AnnotationInstance;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.annotations.SourceType;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.metamodel.internal.source.annotations.JPADotNames;
import org.hibernate.metamodel.internal.source.annotations.JandexHelper;
import org.hibernate.metamodel.internal.source.annotations.attribute.BasicAttribute;
import org.hibernate.metamodel.internal.source.annotations.attribute.MappedAttribute;
import org.hibernate.type.StandardBasicTypes;
@ -42,10 +44,9 @@ import org.hibernate.type.StandardBasicTypes;
* @author Strong Liu
*/
public class TemporalTypeResolver extends AbstractAttributeTypeResolver {
private final MappedAttribute mappedAttribute;
private final BasicAttribute mappedAttribute;
private final boolean isMapKey;
public TemporalTypeResolver(MappedAttribute mappedAttribute) {
public TemporalTypeResolver(BasicAttribute mappedAttribute) {
if ( mappedAttribute == null ) {
throw new AssertionFailure( "MappedAttribute is null" );
}
@ -57,12 +58,16 @@ public class TemporalTypeResolver extends AbstractAttributeTypeResolver {
public String resolveHibernateTypeName(AnnotationInstance temporalAnnotation) {
if ( isTemporalType( mappedAttribute.getAttributeType() ) ) {
if ( mappedAttribute.isVersioned() && mappedAttribute.getVersionSourceType() != null ) {
return mappedAttribute.getVersionSourceType().typeName();
}
if ( temporalAnnotation == null ) {
//SPEC 11.1.47 The Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar.
//todo actually the legacy mapping doesn't require this
throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is a Temporal type, but no @Temporal annotation found." );
}
TemporalType temporalType = JandexHelper.getEnumValue( temporalAnnotation, "value", TemporalType.class );
boolean isDate = Date.class.isAssignableFrom( mappedAttribute.getAttributeType() );
final TemporalType temporalType = JandexHelper.getEnumValue( temporalAnnotation, "value", TemporalType.class );
final boolean isDate = Date.class.isAssignableFrom( mappedAttribute.getAttributeType() );
String type;
switch ( temporalType ) {
case DATE: