|
From: <hib...@li...> - 2006-04-16 14:52:59
|
Author: epbernard
Date: 2006-04-16 10:51:51 -0400 (Sun, 16 Apr 2006)
New Revision: 9752
Modified:
trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.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/SocialSecurityMoralAccount.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml
Log:
Support for sequence generator xml overriding
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-16 06:02:33 UTC (rev 9751)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/reflection/java/EJB3OverridenAnnotationReader.java 2006-04-16 14:51:51 UTC (rev 9752)
@@ -25,6 +25,7 @@
import javax.persistence.DiscriminatorValue;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
+import javax.persistence.SequenceGenerator;
import org.dom4j.Attribute;
import org.dom4j.Element;
@@ -69,6 +70,7 @@
annotationToXml.put( Inheritance.class, "inheritance" );
annotationToXml.put( DiscriminatorValue.class, "discriminator-value" );
annotationToXml.put( DiscriminatorColumn.class, "discriminator-column" );
+ annotationToXml.put( SequenceGenerator.class, "sequence-generator" );
}
private XMLContext xmlContext;
@@ -76,6 +78,7 @@
private String propertyName;
private boolean isFieldAccess;
private transient Annotation[] annotations;
+ private static final String WORD_SEPARATOR = "-";
public EJB3OverridenAnnotationReader(AnnotatedElement el, XMLContext xmlContext) {
super( el );
@@ -166,6 +169,8 @@
if ( current != null ) annotationList.add( current );
current = getDiscriminatorColumn( tree, defaults );
if ( current != null ) annotationList.add( current );
+ current = getSequenceGenerator( tree, defaults );
+ if ( current != null ) annotationList.add( current );
this.annotations = annotationList.toArray( new Annotation[ annotationList.size() ] );
}
else if ( propertyName != null ) {
@@ -178,22 +183,33 @@
}
}
+ private SequenceGenerator getSequenceGenerator(Element tree, XMLContext.Default defaults) {
+ Element element = tree != null ? tree.element( annotationToXml.get(SequenceGenerator.class) ) : null;
+ if ( element != null ) {
+ AnnotationDescriptor ad = new AnnotationDescriptor( SequenceGenerator.class );
+ copyStringAttribute( ad, element, "name" );
+ copyStringAttribute( ad, element, "sequence-name" );
+ copyIntegerAttribute( ad, element, "initial-value" );
+ copyIntegerAttribute( ad, element, "allocation-size" );
+ return AnnotationFactory.create( ad );
+ }
+ else if ( defaults.canUseJavaAnnotations() ) {
+ return super.getAnnotation( SequenceGenerator.class );
+ }
+ else {
+ return null;
+ }
+ }
+
private DiscriminatorColumn getDiscriminatorColumn(Element tree, XMLContext.Default defaults) {
Element element = tree != null ? tree.element( "discriminator-column" ) : null;
if ( element != null ) {
AnnotationDescriptor ad = new AnnotationDescriptor( DiscriminatorColumn.class );
- Attribute attr = element.attribute( "name" );
- if (attr != null) {
- ad.setValue( "name", attr.getValue() );
- }
- attr = element.attribute( "column-definition" );
- if (attr != null) {
- ad.setValue( "columnDefinition", attr.getValue() );
- }
- attr = element.attribute( "discriminator-type" );
+ copyStringAttribute( ad, element, "name" );
+ copyStringAttribute( ad, element, "column-definition" );
+ String value = element.attributeValue( "discriminator-type" );
DiscriminatorType type = DiscriminatorType.STRING;
- if (attr != null) {
- String value = attr.getValue();
+ if (value != null) {
if ( "STRING".equals( value ) ) {
type = DiscriminatorType.STRING;
}
@@ -208,16 +224,7 @@
}
}
ad.setValue( "discriminatorType", type );
- attr = element.attribute( "length" );
- if (attr != null) {
- try {
- int length = Integer.parseInt( attr.getValue() );
- ad.setValue( "length", length );
- }
- catch (NumberFormatException e) {
- throw new AnnotationException( "column length not parsable, activate schema validation: " + attr.getValue() );
- }
- }
+ copyIntegerAttribute( ad, element, "length" );
return AnnotationFactory.create( ad );
}
else if ( defaults.canUseJavaAnnotations() ) {
@@ -232,8 +239,7 @@
Element element = tree != null ? tree.element( "discriminator-value" ) : null;
if ( element != null ) {
AnnotationDescriptor ad = new AnnotationDescriptor( DiscriminatorValue.class );
- String discr = element.getTextTrim();
- ad.setValue( "value", discr );
+ copyStringElement( element, ad, "value" );
return AnnotationFactory.create( ad );
}
else if ( defaults.canUseJavaAnnotations() ) {
@@ -333,7 +339,7 @@
else {
if ( "entity".equals( tree.getName() ) ) {
AnnotationDescriptor entity = new AnnotationDescriptor( Entity.class );
- copyAttribute( entity, tree, "name" );
+ copyStringAttribute( entity, tree, "name" );
if ( defaults.canUseJavaAnnotations()
&& StringHelper.isEmpty( (String) entity.valueOf( "name" ) ) ) {
Entity javaAnn = super.getAnnotation( Entity.class );
@@ -413,13 +419,13 @@
else {
//ignore java annotation, an element is defined
AnnotationDescriptor annotation = new AnnotationDescriptor( Table.class );
- copyAttribute( annotation, subelement, "name" );
- copyAttribute( annotation, subelement, "catalog" );
+ copyStringAttribute( annotation, subelement, "name" );
+ copyStringAttribute( annotation, subelement, "catalog" );
if ( StringHelper.isNotEmpty( defaults.getCatalog() )
&& StringHelper.isEmpty( (String) annotation.valueOf( "catalog" ) ) ) {
annotation.setValue( "catalog", defaults.getCatalog() );
}
- copyAttribute( annotation, subelement, "schema" );
+ copyStringAttribute( annotation, subelement, "schema" );
if ( StringHelper.isNotEmpty( defaults.getSchema() )
&& StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) ) ) {
annotation.setValue( "schema", defaults.getSchema() );
@@ -436,13 +442,13 @@
List<SecondaryTable> secondaryTables = new ArrayList<SecondaryTable>( 3 );
for ( Element element : elements ) {
AnnotationDescriptor annotation = new AnnotationDescriptor( SecondaryTable.class );
- copyAttribute( annotation, element, "name" );
- copyAttribute( annotation, element, "catalog" );
+ copyStringAttribute( annotation, element, "name" );
+ copyStringAttribute( annotation, element, "catalog" );
if ( StringHelper.isNotEmpty( defaults.getCatalog() )
&& StringHelper.isEmpty( (String) annotation.valueOf( "catalog" ) ) ) {
annotation.setValue( "catalog", defaults.getCatalog() );
}
- copyAttribute( annotation, element, "schema" );
+ copyStringAttribute( annotation, element, "schema" );
if ( StringHelper.isNotEmpty( defaults.getSchema() )
&& StringHelper.isEmpty( (String) annotation.valueOf( "schema" ) ) ) {
annotation.setValue( "schema", defaults.getSchema() );
@@ -535,24 +541,51 @@
while ( pkIt.hasNext() ) {
Element subelement = (Element) pkIt.next();
AnnotationDescriptor pkAnn = new AnnotationDescriptor( PrimaryKeyJoinColumn.class );
- Attribute attribute = subelement.attribute( "name" );
- if ( attribute != null ) {
- pkAnn.setValue( "name", attribute.getValue() );
+ copyStringAttribute( pkAnn, subelement, "name");
+ copyStringAttribute( pkAnn, subelement, "referenced-column-name");
+ copyStringAttribute( pkAnn, subelement, "column-definition");
+ pkJoinColumns[index++] = AnnotationFactory.create( pkAnn );
+ }
+ return pkJoinColumns;
+ }
+
+ private void copyStringAttribute(AnnotationDescriptor annotation, Element element, String attributeName) {
+ String attribute = element.attributeValue( attributeName );
+ if (attribute != null) {
+ StringBuilder annotationAttributeName = new StringBuilder( attributeName );
+ int index = annotationAttributeName.indexOf( WORD_SEPARATOR );
+ while ( index != -1 ) {
+ annotationAttributeName.deleteCharAt( index );
+ annotationAttributeName.setCharAt( index, Character.toUpperCase( annotationAttributeName.charAt( index ) ));
+ index = annotationAttributeName.indexOf( WORD_SEPARATOR );
}
- attribute = subelement.attribute( "referenced-column-name" );
- if ( attribute != null ) {
- pkAnn.setValue( "referencedColumnName", attribute.getValue() );
+ annotation.setValue( annotationAttributeName.toString(), attribute );
+ }
+ }
+
+ private void copyIntegerAttribute(AnnotationDescriptor annotation, Element element, String attributeName) {
+ String attribute = element.attributeValue( attributeName );
+ if (attribute != null) {
+ StringBuilder annotationAttributeName = new StringBuilder( attributeName );
+ int index = annotationAttributeName.indexOf( WORD_SEPARATOR );
+ while ( index != -1 ) {
+ annotationAttributeName.deleteCharAt( index );
+ annotationAttributeName.setCharAt( index, Character.toUpperCase( annotationAttributeName.charAt( index ) ));
+ index = annotationAttributeName.indexOf( WORD_SEPARATOR );
}
- attribute = subelement.attribute( "column-definition" );
- if ( attribute != null ) {
- pkAnn.setValue( "columnDefinition", attribute.getValue() );
+ annotation.setValue( annotationAttributeName.toString(), attribute );
+ try {
+ int length = Integer.parseInt( attribute );
+ annotation.setValue( annotationAttributeName.toString(), length );
}
- pkJoinColumns[index++] = AnnotationFactory.create( pkAnn );
+ catch (NumberFormatException e) {
+ throw new AnnotationException( attributeName + " not parseable, activate schema validation: " + attribute );
+ }
}
- return pkJoinColumns;
}
- private void copyAttribute(AnnotationDescriptor annotation, Element element, String attributeName) {
- annotation.setValue( attributeName, element.attributeValue( attributeName, "" ) );
+ private void copyStringElement(Element element, AnnotationDescriptor ad, String annotationAttribute) {
+ String discr = element.getTextTrim();
+ ad.setValue( annotationAttribute, discr );
}
}
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-16 06:02:33 UTC (rev 9751)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/EJB3OverridenAnnotationReaderTest.java 2006-04-16 14:51:51 UTC (rev 9752)
@@ -19,6 +19,7 @@
import javax.persistence.InheritanceType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.DiscriminatorColumn;
+import javax.persistence.SequenceGenerator;
import junit.framework.TestCase;
import org.hibernate.util.XMLHelper;
@@ -55,6 +56,9 @@
assertEquals( "unique constraints ignored", 1, tables[0].uniqueConstraints().length );
assertEquals( "pk join column ignored", 1, tables[0].pkJoinColumns().length );
assertEquals( "pk join column ignored", "admin_id", tables[0].pkJoinColumns()[0].name() );
+ assertNotNull( "Sequence Overriding not working", reader.getAnnotation( SequenceGenerator.class ) );
+ assertEquals( "wrong sequence name", "seqhilo", reader.getAnnotation( SequenceGenerator.class ).sequenceName() );
+ assertEquals( "default fails", 50, reader.getAnnotation( SequenceGenerator.class ).allocationSize() );
reader = new EJB3OverridenAnnotationReader(Match.class, context);
assertNotNull( reader.getAnnotation( Table.class) );
assertEquals( "Java annotation not taken into account", "matchtable", reader.getAnnotation( Table.class ).name() );
@@ -106,6 +110,7 @@
assertNull( reader.getAnnotation( IdClass.class ) );
assertNull( reader.getAnnotation( DiscriminatorValue.class ) );
assertNull( reader.getAnnotation( DiscriminatorColumn.class ) );
+ assertNull( reader.getAnnotation( SequenceGenerator.class ) );
}
private XMLContext buildContext(String ormfile) throws SAXException, DocumentException, IOException {
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/SocialSecurityMoralAccount.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/SocialSecurityMoralAccount.java 2006-04-16 06:02:33 UTC (rev 9751)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/SocialSecurityMoralAccount.java 2006-04-16 14:51:51 UTC (rev 9752)
@@ -4,6 +4,7 @@
import javax.persistence.Entity;
import javax.persistence.IdClass;
import javax.persistence.DiscriminatorValue;
+import javax.persistence.SequenceGenerator;
/**
* @author Emmanuel Bernard
@@ -11,6 +12,7 @@
@Entity
@IdClass(SocialSecurityNumber.class)
@DiscriminatorValue("Moral")
+@SequenceGenerator(name="seq")
public class SocialSecurityMoralAccount {
public String number;
public String countryCode;
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml 2006-04-16 06:02:33 UTC (rev 9751)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/reflection/java/xml/orm.xml 2006-04-16 14:51:51 UTC (rev 9752)
@@ -26,6 +26,7 @@
<column-name>address</column-name>
</unique-constraint>
</secondary-table>
+ <sequence-generator name="seqhilo" sequence-name="seqhilo"/>
</entity>
<entity class="Match">
<inheritance strategy="JOINED"/>
|