We know that the DDL table creation statement can be automatically generated through the Hibernate object, and the data dictionary can be generated in reverse engineering using the PowerDesigner tool. However, Chinese comments cannot be written in the generated DDL, this makes the generated data dictionary unavailable. During this holiday, I found that the Dialect, Table, and Column ing of Hibernate has already done comment processing, but the Hibernate team thinks that this function is too important, there is no time to provide this requirement, so we can implement this function by ourselves. This is a key link for synchronizing data object code with data dictionary documents! Through tracking the Hibernate code, we found that the logic for processing ing is in the Code AnnotationBinder. We don't need to process comment during runtime, but we can only process it when using SchemaExport, therefore, this function is simply implemented: 1. add an annotation @ Comment ("this is a description of the table or a description of the field"), which is applicable to Class Name and attribute name 2. copy org. hibernate. cfg. the AnnotationBuilder code is org. hibernate. cfg. myAnnotationBuilder, process annotation @ Comment 3. copy org. hibernate. cfg. configuration is org. hibernate. cfg. myConfiguration, replace the call of AnnotationBuilder with MyAnnotationBuilder 3. implement the SchemaExportTool class and pass in the mydeleati On processes the Hibernate ing relationship between Hibernate objects and generates DDL. The above ideas are successfully tested on MySql 5.1 Based on Hibernate 4.2.3, because the Code only runs during the development phase and does not require good structure and optimization, so it's just a simple implementation. If you need this function, you can implement it yourself. The following is an example of the processing result: [java] @ Entity @ Table (name = "AuditRecord _") @ Comment ("system audit Table") @ Cache (usage = CacheConcurrencyStrategy. NONSTRICT_READ_WRITE) public class AuditRecord implements Serializable {private static final long serialVersionUID = random; @ Id @ GeneratedValue @ Comment ("ID primary key") private Long id; @ Comment ("audit type ") @ Column (length = 30) private String auditType; @ Comment ("operator") @ ManyToOne @ JoinColumn (name = "userId") private Passport operator; // operation description @ Comment ("operation description") @ Column (length = 4000) private String description; @ Comment ("Creation Time") private Date creationTime;} The generated DDL is as follows: [SQL] create table audit_record _ (id bigint not null auto_increment comment 'id primary key ', audit_type varchar (30) comment 'audit type', creation_time datetime comment 'creation time', description longtext comment 'Operation description', user_id bigin T comment 'operator ', primary key (id) comment = 'System audit table'; the main code snippet is as follows: [java] import java. io. IOException; import java. util. properties; import javax. persistence. embeddable; import javax. persistence. entity; import javax. persistence. mappedSuperclass; import org. hibernate. mappingException; import org. hibernate. cfg. myConfiguration; import org. hibernate. tool. hbm2ddl. schemaExport; import org. springframework. core. Io. resource; import org. springframework. core. io. support. pathMatchingResourcePatternResolver; import org. springframework. core. io. support. resourcePatternResolver; import org. springframework. core. io. support. resourcePatternUtils; import org. springframework. core. type. classreading. cachingMetadataReaderFactory; import org. springframework. core. type. classreading. metadataReader; import org. springframework. Core. type. classreading. metadataReaderFactory; import org. springframework. core. type. filter. annotationTypeFilter; import org. springframework. core. type. filter. typeFilter; import org. springframework. util. classUtils; public class SchemaExportTool extends MyConfiguration {private static final long serialVersionUID = 1L; private static final String RESOURCE_PATTERN = "/**/*. class "; private static final String PACKAGE_INFO_SUFFIX = ". package-info "; private static final TypeFilter [] ENTITY_TYPE_FILTERS = new TypeFilter [] {new AnnotationTypeFilter (Entity. class, false), new AnnotationTypeFilter (Embeddable. class, false), new AnnotationTypeFilter (MappedSuperclass. class, false)}; private final ResourcePatternResolver resourcePatternResolver; public SchemaExportTool () {this. resourcePatternResolver = ResourcePatternUtils. getResourcePatternResolver (new PathMatchingResourcePatternResolver ();} public static void main (String [] args) {try {Properties p = new Properties (); p. setProperty ("hibernate. dialect "," org. hibernate. dialect. mySQLDialect "); SchemaExportTool cfg = new SchemaExportTool (); cfg. addProperties (p); cfg. setNamingStrategy (new ImprovedMyNamingStrategy (); cfg. scanPackage ("com. share. Passport. domain "," com. share. authority. domain "," com. share. utils. domain "); SchemaExport se = new SchemaExport (cfg); if (null! = Args & args. length> 1) if ("-f ". equals (args [0]) se. setOutputFile (args [1]); else. setOutputFile ("create_table. SQL"); else. setOutputFile ("create_table. SQL"); se. setDelimiter (";"); // se. drop (false, false); se. create (false, false);} catch (Exception e) {e. printStackTrace () ;}} private SchemaExportTool scanPackage (String... packagesToScan) {try {for (String pkg: packagesToScan) {String Pattern = ResourcePatternResolver. CLASSPATH_ALL_URL_PREFIX + ClassUtils. convertClassNameToResourcePath (pkg) + RESOURCE_PATTERN; Resource [] resources = this. resourcePatternResolver. getResources (pattern); MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory (this. resourcePatternResolver); for (Resource resource: resources) {if (resource. isReadable () {MetadataReader reader = r EaderFactory. getMetadataReader (resource); String className = reader. getClassMetadata (). getClassName (); if (matchesEntityTypeFilter (reader, readerFactory) {addAnnotatedClass (this. resourcePatternResolver. getClassLoader (). loadClass (className);} else if (className. endsWith (PACKAGE_INFO_SUFFIX) {addPackage (className. substring (0, className. length ()-PACKAGE_INFO_SUFFIX.length () ;}}} ret Urn this;} catch (IOException ex) {throw new MappingException ("Failed to scan classpath for unlisted classes", ex);} catch (ClassNotFoundException ex) {throw new MappingException ("Failed to load annotated classes from classpath", ex );}} /*** Check whether any of the configured entity type filters matches the * current class descriptor contained in the metadata reader. */private boolean m AtchesEntityTypeFilter (MetadataReader reader, MetadataReaderFactory readerFactory) throws IOException {for (TypeFilter filter: ENTITY_TYPE_FILTERS) {if (filter. match (reader, readerFactory) {return true ;}} return false ;}} [java]/*** $ Id: $ */package com. share. utils. hibernate; import org. hibernate. annotations. common. reflection. XClass; import org. hibernate. annotations. common. reflection. XPr Operty; import org. hibernate. cfg. ejb3Column; import org. hibernate. mapping. persistentClass; import com. share. annotations. comment; public class CommentBinder {public static void bindTableComment (XClass clazzToProcess, PersistentClass persistentClass) {if (clazzToProcess. isAnnotationPresent (Comment. class) {String tableComment = clazzToProcess. getAnnotation (Comment. class ). value (); persistentClass. GetTable (). setComment (tableComment) ;}} public static void bindColumnComment (XProperty property, Ejb3Column [] columns) {if (null! = Columns) if (property. isAnnotationPresent (Comment. class) {String comment = property. getAnnotation (Comment. class ). value (); for (Ejb3Column column: columns) {column. getMappingColumn (). setComment (comment) ;}} public static void bindColumnComment (XProperty property, Ejb3Column column) {if (null! = Column) if (property. isAnnotationPresent (Comment. class) {String comment = property. getAnnotation (Comment. class ). value (); column. getMappingColumn (). setComment (comment) ;}} [java/*** $ Id: $ */package com. share. annotations; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target; @ Target ({ElementType. TYPE, ElementType. FIELD}) @ Retention (RetentionPolicy. RUNTIME) public @ interface Comment {String value () default "";}