From: <hib...@li...> - 2006-04-25 00:49:36
|
Author: epbernard Date: 2006-04-24 20:49:30 -0400 (Mon, 24 Apr 2006) New Revision: 9786 Added: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Availability.java Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/BusTrip.java trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/metadata-complete.xml Log: XML overriding: support for <basic> Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java =================================================================== --- trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java 2006-04-24 23:35:11 UTC (rev 9785) +++ trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java 2006-04-25 00:49:30 UTC (rev 9786) @@ -55,6 +55,8 @@ import javax.persistence.Lob; import javax.persistence.Enumerated; import javax.persistence.Basic; +import javax.persistence.FetchType; +import javax.persistence.EnumType; import org.dom4j.Attribute; import org.dom4j.Element; @@ -301,34 +303,15 @@ private void getBasic(List<Annotation> annotationList, XMLContext.Default defaults) { for (Element element : elementsForProperty) { if ( "basic".equals( element.getName() ) ) { - boolean properAccessOnMetadataComplete = computeProperAccessOnMetadataComplete( defaults ); - boolean properOverridingOnMetadataNonComplete = computeProperOverridingOnMetadataComplete( defaults ); - if ( properAccessOnMetadataComplete || properOverridingOnMetadataNonComplete ) { - Annotation annotation = buildColumns( element ); - if (annotation != null) annotationList.add(annotation); - annotation = buildGeneratedValue(element); - if (annotation != null) annotationList.add(annotation); - annotation = buildTemporal(element); - if (annotation != null) annotationList.add(annotation); - //FIXME: fix the priority of xml over java for generator names - annotation = getTableGenerator( element, defaults ); - if (annotation != null) annotationList.add(annotation); - annotation = getSequenceGenerator( element, defaults ); - if (annotation != null) annotationList.add(annotation); - AnnotationDescriptor id = new AnnotationDescriptor( Id.class ); - annotationList.add( AnnotationFactory.create( id ) ); - } - else { - if ( defaults.canUseJavaAnnotations() ) { - if (! properOverridingOnMetadataNonComplete) { - //check that id exists on the other attribute - //TODO EmbeddedId too? - if (mirroredAttribute == null || ! mirroredAttribute.isAnnotationPresent( Id.class ) ) { - throw new AnnotationException("Cannot override an property with <id> it does not have an @Id already"); - } - } - } - } + Annotation annotation = buildColumns( element ); + if (annotation != null) annotationList.add(annotation); + getTemporal(annotationList, element); + getLob(annotationList, element); + getEnumerated(annotationList, element); + AnnotationDescriptor basic = new AnnotationDescriptor( Basic.class ); + getFetchType( basic, element.attributeValue( "fetch" ) ); + copyBooleanAttribute( basic, element, "optional" ); + annotationList.add( AnnotationFactory.create( basic ) ); } } if ( elementsForProperty.size() == 0 ) { @@ -350,6 +333,43 @@ } } + private void getEnumerated(List<Annotation> annotationList, Element element) { + Element subElement = element != null ? element.element( "enumerated" ) : null; + if (subElement != null) { + AnnotationDescriptor ad = new AnnotationDescriptor( Enumerated.class ); + String enumerated = subElement.getTextTrim(); + if ( "ORDINAL".equalsIgnoreCase( enumerated ) ) { + ad.setValue( "value", EnumType.ORDINAL ); + } + else if ( "STRING".equalsIgnoreCase( enumerated ) ) { + ad.setValue( "value", EnumType.STRING ); + } + else if ( StringHelper.isNotEmpty( enumerated ) ) { + throw new AnnotationException( "Unknown EnumType: " + enumerated + ". " + SCHEMA_VALIDATION); + } + annotationList.add( AnnotationFactory.create( ad ) ); + } + } + + private void getLob(List<Annotation> annotationList, Element element) { + Element subElement = element != null ? element.element( "lob" ) : null; + if (subElement != null) { + annotationList.add( AnnotationFactory.create( new AnnotationDescriptor( Lob.class ) ) ); + } + } + + private void getFetchType(AnnotationDescriptor descriptor, String fetchString) { + if (fetchString != null) { + if ( "eager".equalsIgnoreCase( fetchString ) ) { + descriptor.setValue( "fetch", FetchType.EAGER); + } + else if ( "lazy".equalsIgnoreCase( fetchString ) ) { + descriptor.setValue( "fetch", FetchType.LAZY); + } + } + + } + private void getEmbeddedId(List<Annotation> annotationList, XMLContext.Default defaults) { for (Element element : elementsForProperty) { if ( "embedded-id".equals( element.getName() ) ) { @@ -398,8 +418,7 @@ if (annotation != null) annotationList.add(annotation); annotation = buildGeneratedValue(element); if (annotation != null) annotationList.add(annotation); - annotation = buildTemporal(element); - if (annotation != null) annotationList.add(annotation); + getTemporal(annotationList, element); //FIXME: fix the priority of xml over java for generator names annotation = getTableGenerator( element, defaults ); if (annotation != null) annotationList.add(annotation); @@ -478,28 +497,25 @@ } } - private Temporal buildTemporal(Element element) { + private void getTemporal(List<Annotation> annotationList, Element element) { Element subElement = element != null ? element.element( "temporal" ) : null; if (subElement != null) { - AnnotationDescriptor ad = new AnnotationDescriptor( Temporal.class ); - String temporal = subElement.getTextTrim(); - if ( "DATE".equalsIgnoreCase( temporal ) ) { - ad.setValue( "value", TemporalType.DATE ); - } - else if ( "TIME".equalsIgnoreCase( temporal ) ) { - ad.setValue( "value", TemporalType.TIME ); - } - else if ( "TIMESTAMP".equalsIgnoreCase( temporal ) ) { - ad.setValue( "value", TemporalType.TIMESTAMP ); - } - else if ( StringHelper.isNotEmpty( temporal ) ) { - throw new AnnotationException( "Unknown TemporalType: " + temporal+ ". " + SCHEMA_VALIDATION); - } - return AnnotationFactory.create( ad ); + AnnotationDescriptor ad = new AnnotationDescriptor( Temporal.class ); + String temporal = subElement.getTextTrim(); + if ( "DATE".equalsIgnoreCase( temporal ) ) { + ad.setValue( "value", TemporalType.DATE ); + } + else if ( "TIME".equalsIgnoreCase( temporal ) ) { + ad.setValue( "value", TemporalType.TIME ); + } + else if ( "TIMESTAMP".equalsIgnoreCase( temporal ) ) { + ad.setValue( "value", TemporalType.TIMESTAMP ); + } + else if ( StringHelper.isNotEmpty( temporal ) ) { + throw new AnnotationException( "Unknown TemporalType: " + temporal+ ". " + SCHEMA_VALIDATION); + } + annotationList.add( AnnotationFactory.create( ad ) ); } - else { - return null; - } } private AssociationOverrides getAssociationOverrides(Element tree, XMLContext.Default defaults) { Added: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Availability.java =================================================================== --- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Availability.java 2006-04-24 23:35:11 UTC (rev 9785) +++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/Availability.java 2006-04-25 00:49:30 UTC (rev 9786) @@ -0,0 +1,10 @@ +//$Id: $ +package org.hibernate.test.reflection.java.xml; + +/** + * @author Emmanuel Bernard + */ +public enum Availability { + ON_DUTY, + NO_SERVICE +} Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/BusTrip.java =================================================================== --- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/BusTrip.java 2006-04-24 23:35:11 UTC (rev 9785) +++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/BusTrip.java 2006-04-25 00:49:30 UTC (rev 9786) @@ -1,6 +1,7 @@ //$Id: $ package org.hibernate.test.reflection.java.xml; +import java.util.Date; import javax.persistence.Entity; import javax.persistence.EmbeddedId; @@ -10,6 +11,9 @@ @Entity public class BusTrip { private BusTripPk id; + private Availability status; + private byte[] serial; + private Date terminusTime; @EmbeddedId public BusTripPk getId() { @@ -19,4 +23,28 @@ public void setId(BusTripPk id) { this.id = id; } + + public Availability getStatus() { + return status; + } + + public void setStatus(Availability status) { + this.status = status; + } + + public byte[] getSerial() { + return serial; + } + + public void setSerial(byte[] serial) { + this.serial = serial; + } + + public Date getTerminusTime() { + return terminusTime; + } + + public void setTerminusTime(Date terminusTime) { + this.terminusTime = terminusTime; + } } Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java =================================================================== --- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java 2006-04-24 23:35:11 UTC (rev 9785) +++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java 2006-04-25 00:49:30 UTC (rev 9786) @@ -36,6 +36,11 @@ import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.EmbeddedId; +import javax.persistence.Enumerated; +import javax.persistence.EnumType; +import javax.persistence.Lob; +import javax.persistence.Basic; +import javax.persistence.FetchType; import junit.framework.TestCase; import org.dom4j.DocumentException; @@ -206,6 +211,28 @@ assertEquals( 1, reader.getAnnotation( AttributeOverrides.class ).value().length ); } + public void testBasicRelatedAnnotations() throws Exception { + XMLContext context = buildContext("org/hibernate/test/reflection/java/xml/metadata-complete.xml"); + Field field = BusTrip.class.getDeclaredField( "status" ); + EJB3OverridenAnnotationReader reader = new EJB3OverridenAnnotationReader( field, context ); + assertNotNull( reader.getAnnotation( Enumerated.class ) ); + assertEquals( EnumType.STRING, reader.getAnnotation( Enumerated.class ).value() ); + assertEquals( false, reader.getAnnotation( Basic.class ).optional() ); + field = BusTrip.class.getDeclaredField( "serial" ); + reader = new EJB3OverridenAnnotationReader( field, context ); + assertNotNull( reader.getAnnotation( Lob.class ) ); + assertEquals( "serialbytes", reader.getAnnotation( Columns.class ).columns()[0].name() ); + field = BusTrip.class.getDeclaredField( "terminusTime" ); + reader = new EJB3OverridenAnnotationReader( field, context ); + assertNotNull( reader.getAnnotation( Temporal.class ) ); + assertEquals( TemporalType.TIMESTAMP, reader.getAnnotation( Temporal.class ).value() ); + assertEquals( FetchType.LAZY, reader.getAnnotation( Basic.class ).fetch() ); + + field = BusTripPk.class.getDeclaredField( "busDriver" ); + reader = new EJB3OverridenAnnotationReader( field, context ); + assertNotNull( reader.isAnnotationPresent( Basic.class ) ); + } + private XMLContext buildContext(String ormfile) throws SAXException, DocumentException, IOException { XMLHelper xmlHelper = new XMLHelper(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/metadata-complete.xml =================================================================== --- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/metadata-complete.xml 2006-04-24 23:35:11 UTC (rev 9785) +++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/metadata-complete.xml 2006-04-25 00:49:30 UTC (rev 9786) @@ -29,6 +29,16 @@ <column name="fld_busdriver"/> </attribute-override> </embedded-id> + <basic name="status" optional="false"> + <enumerated>STRING</enumerated> + </basic> + <basic name="serial" optional="true"> + <column name="serialbytes"/> + <lob/> + </basic> + <basic name="terminusTime" fetch="LAZY"> + <temporal>TIMESTAMP</temporal> + </basic> </attributes> </entity> <embeddable class="BusTripPk" access="FIELD"> |