Hibernate-annotation3.30 创建自定义注解,向oracle数据库写列注释

这是去年项目中遇到的.想起来这个问题了.

因为当时这版本没有一个注解可以在根据Model在逆向生成数据库时自动生成列注释,不方便,在网上找着了解决办法,不知道现在版本有没有实现这功能

https://hibernate.onjira.com/browse/HHH-4369

1. Index: src/java/org/hibernate/cfg/Ejb3Column.java

--- src/java/org/hibernate/cfg/Ejb3Column.java	(Revision 14468)
+++ src/java/org/hibernate/cfg/Ejb3Column.java	(Arbeitskopie)
@@ -144,6 +144,10 @@
 	public boolean isNullable() {
 		return mappingColumn.isNullable();
 	}
+	
+	public void setComment(String comment) {
+	    this.mappingColumn.setComment(comment);
+	}
 
 	public Ejb3Column() {
 	}

 

2. Index: src/java/org/hibernate/cfg/annotations/EntityBinder.java

--- src/java/org/hibernate/cfg/annotations/EntityBinder.java	(Revision 14468)
+++ src/java/org/hibernate/cfg/annotations/EntityBinder.java	(Arbeitskopie)
@@ -402,7 +402,8 @@
 	public void bindTable(
 			String schema, String catalog,
 			String tableName, List uniqueConstraints,
-			String constraints, Table denormalizedSuperclassTable
+			String constraints, Table denormalizedSuperclassTable,
+			String comment
 	) {
 		String logicalName = StringHelper.isNotEmpty( tableName ) ?
 				tableName :
@@ -414,6 +415,10 @@
 				persistentClass.isAbstract(), uniqueConstraints, constraints,
 				denormalizedSuperclassTable, mappings
 		);
+		
+		if ( (comment != null) && (comment.length() > 0) ) {
+		    table.setComment(comment);
+		}
 
 		if ( persistentClass instanceof TableOwner ) {
 			if ( log.isInfoEnabled() ) {

3. Index: src/java/org/hibernate/cfg/AnnotationBinder.java

--- src/java/org/hibernate/cfg/AnnotationBinder.java	(Revision 14468)
+++ src/java/org/hibernate/cfg/AnnotationBinder.java	(Arbeitskopie)
@@ -65,6 +65,7 @@
 import org.hibernate.annotations.CollectionId;
 import org.hibernate.annotations.CollectionOfElements;
 import org.hibernate.annotations.Columns;
+import org.hibernate.annotations.Comment;
 import org.hibernate.annotations.Fetch;
 import org.hibernate.annotations.Filter;
 import org.hibernate.annotations.FilterDef;
@@ -421,6 +422,7 @@
 		String schema = "";
 		String table = ""; //might be no @Table  annotation on the annotated class
 		String catalog = "";
+		String comment = "";
 		String discrimValue = null;
 		List<String[]> uniqueConstraints = new ArrayList<String[]>();
 		Ejb3DiscriminatorColumn discriminatorColumn = null;
@@ -433,6 +435,11 @@
 			catalog = tabAnn.catalog();
 			uniqueConstraints = TableBinder.buildUniqueConstraints( tabAnn.uniqueConstraints() );
 		}
+		if ( annotatedClass.isAnnotationPresent( org.hibernate.annotations.Comment.class ) ) {
+		    org.hibernate.annotations.Comment commentAnn = annotatedClass.getAnnotation( org.hibernate.annotations.Comment.class );
+		    comment = commentAnn.value();
+		}     
+        
 		final boolean hasJoinedColumns = inheritanceState.hasParents
 				&& InheritanceType.JOINED.equals( inheritanceState.type );
 		if ( hasJoinedColumns ) {
@@ -554,7 +561,8 @@
 					schema, catalog, table, uniqueConstraints,
 					constraints, inheritanceState.hasDenormalizedTable() ?
 					superEntity.getTable() :
-					null
+					null,
+					comment
 			);
 		}
 		else {
@@ -1087,7 +1095,7 @@
 					"Processing annotations of " + propertyHolder.getEntityName() + "." + inferredData.getPropertyName()
 			);
 		}
-
+		
 		if ( property.isAnnotationPresent( Parent.class ) ) {
 			if ( propertyHolder.isComponent() ) {
 				propertyHolder.setParentProperty( property.getName() );
@@ -1596,6 +1604,23 @@
 				}
 			}
 		}
+		
+		/*
+		 * Now process column comments
+		 */
+		Comment comment = property.getAnnotation( Comment.class );
+		if ( comment != null ) {
+            if ( joinColumns != null ) {
+                for ( Ejb3Column column : joinColumns ) {
+                    column.setComment( comment.value() );
+                }
+            }
+            else {
+                for ( Ejb3Column column : columns ) {
+                    column.setComment( comment.value() );
+                }
+            }
+		}
 	}
 
 	//TODO move that to collection binder?

4. Index: build.xml

--- build.xml	(Revision 14468)
+++ build.xml	(Arbeitskopie)
@@ -16,7 +16,7 @@
 	<!-- Name of project and version, used to create filenames -->
 	<property name="Name" value="Hibernate Annotations"/>
 	<property name="name" value="hibernate-annotations"/>
-	<property name="version" value="3.3.0.GA"/>
+	<property name="version" value="3.3.0.GA.comments"/>
 	<property name="javadoc.packagenames" value="org.hibernate.*"/>
 	<property name="jdbc.dir" value="jdbc"/>
 	<property name="copy.test" value="true"/>


你可能感兴趣的:(Hibernate-annotation3.30 创建自定义注解,向oracle数据库写列注释)