Cownew open-source-write a plug-in for hibernateTools

Source: Internet
Author: User

Recently, we are preparing to activate the invoicing project. On the one hand, this will allow more people to participate in open-source development. On the other hand, we will also apply the SQL translator, SQL optimizer, and JDBMonitor to this project, in this way, the three basic modules can be verified and enhanced in actual project applications.
I am going to use hibernate to implement the persistence layer, So I downloaded hibernate3 ON THE hibernate website and looked at a hibernatetools. It is a helper tool of hibernate in eclipse and it is also down, it feels good to use it. POJO and hbm can be generated directly from the database table. xml (in fact, I do not like this development method very much. I prefer to use modeling tools to write POJO, and then use tools to generate hbm. xml and DDL, but it seems that hibernate3 does not yet have such a tool. If any of you know this tool, please kindly advise ).
In the United States, the field names of the javabean generated by the javabean are exactly the same as those of the database Field names. The class names of the generated javabean are exactly the same as those of the database table names. For the sake of clarity and portability (which is also the habit of developing the company's development specifications), all the table names I designed start with "T _", with the sub-system name added in the middle, the last is the ideographic table name. For example, "T_PS_BOM" is used to represent the Material List table in the production management system. All field names start with "F", such as FId and FName.
As a result, the following javabean is generated:

Public class TPSBOM
{
.......
Public String getFID ()
...
Public String getFNumber ()
}


It looks unintuitive. I just wanted to give up this tool and thought about it. "It's not the spirit of being an open source. Study it.
I checked that the Hibernate Code Generation tab has a "reveng Strategy". What does it mean? "Reverse engineering policy "?? Click "Browse" to bring up a class selection dialog box. The default "DefaultReverseEngineeringStrategy" class is displayed. I found it in the installation directory of hibernatgatels and finally found it in pluginsorg. hibernate. eclipse_3.2.0.beta6libools under the hibernate-tools.jar to find the shadow of this class, decompile with The Decompilation tool (too lazy to go online under the source code, huh, huh ). Each method name is displayed in front of me:
TableToClassName
ColumnToPropertyName
ColumnToHibernateTypeName
...
Isn't this just ing the corresponding database items into corresponding java items?
Starting work!
Create a new class CowNewReverseEngineeringStrategy, inherited from DefaultReverseEngineeringStrategy, override tableToClassName,
ColumnToPropertyName: write your own conversion logic in the two methods.
Then package it into a jar package and put it in pluginsorg. hibernate. eclipse_3.2.0.beta6libools, and then in pluginsorg. hibernate. in eclipse_3.2.0.beta6liboolsMANIFEST.MF, add the content of the new package, close eclipse, add the-clean parameter to start eclipse, click "Hibernate Code Generation", and fill "reveng Strategy" into "com. cownew. devTools. hibtools. revEng. cowNewReverseEngineeringStrategy "," Run "!!!
The system prompts "com. cownew. DevTools. hibtools. RevEng. CowNewReverseEngineeringStrategy" error.
Exception while generating code
Reason org. hibernate. console. HibernateConsoleRunTimeException: cocould not create or find com. with one argument deleate constructor"

It seems that there is a problem in the reflection call, re-open the hibernate-tools.jar, careful observation, actually found a DelegatingReverseEngineeringStrategy, it has a parameter for the ReverseEngineeringStrategy delegate constructor, other calls are forwarded to ReverseEngineeringStrategy, fainting, failing to understand what it is doing, and having no time to study it. To CowNewReverseEngineeringStrategy, there is also a constructor named "ReverseEngineeringStrategy delegate, package again, restart eclipse, haha, everything is done, and finally generate my cute,
Public class PersonInfo
{
Public String getNumber ()...
Public String getId ()...
}
.
Complete code:

Package com. cownew. DevTools. hibtools. RevEng;

Import java. beans. Introspector;

Import org. hibernate. cfg. reveng. DefaultReverseEngineeringStrategy;
Import org. hibernate. cfg. reveng. ReverseEngineeringSettings;
Import org. hibernate. cfg. reveng. ReverseEngineeringStrategy;
Import org. hibernate. cfg. reveng. ReverseEngineeringStrategyUtil;
Import org. hibernate. cfg. reveng. TableIdentifier;
Import org. hibernate. util. StringHelper;

Public class CowNewReverseEngineeringStrategy extends
DefaultReverseEngineeringStrategy
{

Public CowNewReverseEngineeringStrategy (ReverseEngineeringStrategy delegate)
{
Super ();
}

Private ReverseEngineeringSettings settings = new ReverseEngineeringSettings ();

Public String tableToClassName (TableIdentifier table)
{
String tableName = table. getName ();
If (tableName! = Null & tableName. toUpperCase (). startsWith ("T _"))
{
String pkgName = settings. getDefaultPackageName ();
Int lastIndex = tableName. lastIndexOf (_);
TableName = tableName. substring (lastIndex + 1, tableName. length ())
+ "Info ";

String className = toUpperCamelCase (tableName );

If (pkgName. length ()> 0)
Return StringHelper. qualify (pkgName, className );
Return className;

} Else
{
Return super. tableToClassName (table );
}
};

Public String columnToPropertyName (TableIdentifier table, String column)
{
If (column! = Null & column. toUpperCase (). startsWith ("F "))
{
String cownewColName = column. substring (1, column. length ());

String decapitalize = Introspector
. Decapitalize (toUpperCamelCase (cownewColName ));
Return keywordCheck (decapitalize );
} Else
{
Return super. columnToPropertyName (table, column );
}
}

Private String keywordCheck (String possibleKeyword)
{
If (ReverseEngineeringStrategyUtil
. IsReservedJavaKeyword (possibleKeyword ))
PossibleKeyword + = "_";
Return possibleKeyword;
}

Public void setSettings (ReverseEngineeringSettings settings)
{
Super. setSettings (settings );
This. settings = settings;
}

Public static void main (String [] args)
{
TableIdentifier table = new TableIdentifier ("T_BD_Person ");
// TableIdentifier table = new TableIdentifier ("T_Person ");
// TableIdentifier table = new TableIdentifier ("Person ");
CowNewReverseEngineeringStrategy revEng = new CowNewReverseEngineeringStrategy (null );
String className = revEng. tableToClassName (table );
System. out. println (className );
System. out. println (revEng. columnToPropertyName (table, "FId "));
System. out. println (revEng. columnToPropertyName (table, "Id "));
Recently, we are preparing to activate the invoicing project. On the one hand, this will allow more people to participate in open-source development. On the other hand, we will also apply the SQL translator, SQL optimizer, and JDBMonitor to this project, in this way, the three basic modules can be

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.