Java uses mapping table name reflection to create entity classes and assign property values

Source: Internet
Author: User
Tags object object

The first initialization in 1.hibernate, the corresponding table name and class name as key-value pairs in the way of storing to map

Private map<string, string> mappings;//global variable

/**
* Get entity classes based on database table names
*/
public void Initmappings () {
if (mappings = = null) {
mappings = new hashmap<string, string> ();
Sessionfactory factory = This.getsessionfactory ();
Map Metamap = Factory.getallclassmetadata ();
For (String key: (set<string>) Metamap.keyset ()) {
Abstractentitypersister Classmetadata = (abstractentitypersister) metamap.get (key);
String tableName = Classmetadata.gettablename (). toLowerCase ();
int index = Tablename.indexof (".");
if (index >= 0) {
TableName = tablename.substring (index + 1);
}
String className = Classmetadata.getentitymetamodel (). GetName ();
Mappings.put (TableName, className);
}
}
}

2. Calling the method, passing in the table name to get the corresponding entity class name

public string Getentitynamebytablename (string tableName) {
Initmappings ();
Return Mappings.get (TableName);
}

3. Create an entity class based on the entity class name

/**
*listobj: The set of attribute values to be assigned, in the same order as the entity class attributes

*/

Public Object Getbyreflect (String tableName, List listobj) throws Exception {

Class<?> model = Class.forName (tableName);
Object object = new Object ();

if (model = null) {
field[] field = Model.getdeclaredfields ();
string[] modelname = new String[field.length];
string[] Modeltype = new String[field.length];

Object = Model.newinstance ();
Method m = null;

for (int i = 1; i <field.length; i++) {
String name = Field[i].getname ();
Object value = null;
Name = name.substring (0, 1). toUpperCase () + name.substring (1);
String type = Field[i].getgenerictype (). toString ();
if (Type.equals ("Class java.lang.String")) {
m = Model.getdeclaredmethod ("set" + Name, String.class);

if (Listobj.get (i-1) instanceof Double) {
Double d= (double) listobj.get (i-1);
Value=string.valueof (d);

}else{
Value = (String) listobj.get (i-1);
}
}
if (Type.equals ("Class Java.lang.Integer")) {
m = Model.getdeclaredmethod ("set" + Name, Integer.class);
Double d = (double) listobj.get (i-1);
Value = integer.valueof (D.intvalue ());
}
if (Type.equals ("Class Java.lang.Short")) {
m = Model.getdeclaredmethod ("set" + Name, Short.class);
Value = (short) listobj.get (i-1);
}
if (Type.equals ("Class Java.lang.Float")) {
m = Model.getdeclaredmethod ("set" + Name, Float.class);
Value = (Float) listobj.get (i-1);
}
if (Type.equals ("Class java.lang.Double")) {
m = Model.getdeclaredmethod ("set" + Name, Double.class);
Value = (Double) listobj.get (i-1);
}
if (Type.equals ("Class Java.lang.Boolean")) {
m = Model.getdeclaredmethod ("set" + Name, Boolean.class);
Value = (Boolean) listobj.get (i-1);
}
if (M! = null) {
M.invoke (object, value);
}
}

}
return object;
}

Java uses mapping table name reflection to create entity classes and assign property values

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.