Query table name and field name in hibernate

Source: Internet
Author: User

/**
* Hibernate object, attribute query table name, field name tool class
*
*/

Package com. cinwa. creditnet. Common;

Import java. util. iterator;

Import org. hibernate. cfg. configuration;
Import org. hibernate. Mapping. column;
Import org. hibernate. Mapping. persistentclass;
Import org. hibernate. Mapping. Property;
Import org. hibernate. Mapping. Table;

Public class hibernateparter {

Private Static configuration hibernateconf;

Private Static configuration gethibernateconf (){
If (hibernateconf = NULL ){
// Return new configuration (). Configure ();
Return new configuration ();
}
Return hibernateconf;
}

Private Static persistentclass getpersistentclass (class clazz ){

Synchronized (hibernateparter. Class ){
Persistentclass Pc = gethibernateconf (). getclassmapping (
Clazz. getname ());

If (Pc = NULL ){
Hibernateconf = gethibernateconf (). addclass (clazz );
PC = gethibernateconf (). getclassmapping (clazz. getname ());
}
Return PC;
}
}

/**
* Obtain the table name corresponding to the object.
* @ Param clazz entity class
* @ Return table name
*/
Public static string gettablename (class clazz ){
Return getpersistentclass (clazz). gettable (). getname ();
}

/**
* obtain the primary key field name of the table corresponding to the object
* @ Param clazz entity class
* @ return primary key field name
* /
Public static string getpkcolumnname (class clazz) {
Table = getpersistentclass (clazz ). gettable ();
return table. getprimarykey (). getcolumn (0 ). getname ();
}

/**
* using entity classes and attributes, obtain the table field name corresponding to the object class attribute
* @ Param clazz entity class
* @ Param propertyname attribute name
* @ return field name
*/
Public static string getcolumnname (class clazz, string propertyname) {
persistentclass = getpersistentclass (clazz);
Property = persistentclass. getproperty (propertyname);
iterator it = property. getcolumniterator ();
If (it. hasnext () {
column = (column) it. next ();
return column. getname ();
}< br> return NULL;
}< BR >}

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.