Hibernate mapping field problem [Improvednamingstrategy]

Source: Internet
Author: User

Hibernate uses JPA for mappings there are 3 rules that can be configured: Defaultnamingstrategy,improvednamingstrategy,ejb3namingstrategy

Here only to say Improvednamingstrategy, other self-looking hibernate code, Improvednamingstrategy code as follows, is a singleton instance:


/** Hibernate, relational persistence for idiomatic java** Copyright (c), Red Hat Inc. or Third-party contributors as  * indicated by the @author tags or express copyright attribution* statements applied by the authors.  All Third-party contributions are* distributed under license by Red Hat inc.** This copyrighted material is made available To anyone wishing to use, modify,* copy, or redistribute it subject to the terms and conditions of the gnu* Lesser genera L public License, as published by the Free software foundation.** This program is distributed in the hope that it'll be useful,* but without any WARRANTY;  Without even the implied warranty of merchantability* or FITNESS for A particular PURPOSE. See the GNU Lesser general public license*-details.** you should has received a copy of the GNU Lesser General P Ublic license* along with this distribution; If not, write to:* free software Foundation, inc.* Franklin Street, fifth floor* Boston, MA 02110-1301 Usa*/pacKage Org.hibernate.cfg;import Java.io.serializable;import Org.hibernate.util.stringhelper;import org.hibernate.assertionfailure;/*** an improved naming strategy, prefers embedded* underscores to mixed case names* @s EE defaultnamingstrategy The default strategy* @author Gavin King*/public class Improvednamingstrategy implements Namings Trategy, Serializable {/** * A convenient singleton instance */public static final Namingstrategy instance = new Improvedn Amingstrategy ();/** * Return the unqualified class name, mixed case converted to * underscores */public String classtotabl Ename (String className) {return addunderscores (stringhelper.unqualify (ClassName));} /** * Return "full" path with underscore seperators, mixed * Case converted to underscores */public String prop Ertytocolumnname (String PropertyName) {return addunderscores (stringhelper.unqualify (PropertyName));} /** * Convert mixed case to underscores */public string TableName (String tableName) {return addUnderscores (tableName);} /** * Convert mixed case to underscores */public string ColumnName (String columnName) {return addunderscores (Columnnam e);}    protected static string Addunderscores (string name) {StringBuffer buf = new StringBuffer (Name.replace ('. ', ' _ '));            for (int i=1; I<buf.length ()-1; i++) {if (Character.islowercase (Buf.charat)) &&            Character.isuppercase (Buf.charat (i)) && character.islowercase (Buf.charat (i+1))) {        Buf.insert (i++, ' _ '); }} return Buf.tostring (). toLowerCase ();} public string Collectiontablename (string ownerentity, String ownerentitytable, String associatedentity, String Ociatedentitytable, String PropertyName) {return tableName (ownerentitytable + ' _ ' + propertytocolumnname (prope Rtyname));} /** * Return the argument */public string Joinkeycolumnname (String joinedcolumn, String joinedtable) {return Columnnam E (joinedcolumn);} /** * Return the property name or Propertytablename */public string Foreignkeycolumnname (String propertyname, String Propertyentityname, String propertytablename, String referencedcolumnname) {string Header = PropertyName! = null?    Stringhelper.unqualify (PropertyName): Propertytablename;    if (header = = null) throw new Assertionfailure ("Namingstrategy not properly filled"); Return ColumnName (header); + "_" + referencedcolumnname not used for backward compatibility}/** * Return the column name or the unqualified propert Y name */public string logicalcolumnname (String columnName, String propertyname) {return Stringhelper.isnotempty (Colu Mnname)? ColumnName:StringHelper.unqualify (propertyname);} /** * Returns Either the table name if explicit or * if there is an associated table, the concatenation of owner entity Ta BLE and associated table * Otherwise the concatenation of Owner entity table and the unqualified property name */public St Ring LogicalcollectiontabLename (String tableName, String ownerentitytable, String associatedentitytable, St    Ring PropertyName) {if (tableName! = null) {return tableName; } else {//use of a stringbuffer to workaround a JDK bug return new StringBuffer (ownerentitytable). Append                    ("_"). Append (associatedentitytable! = null?)    AssociatedEntityTable:StringHelper.unqualify (PropertyName)). ToString (); }}/** * Return The column name if explicit or the concatenation of the property name and the referenced column */public St Ring Logicalcollectioncolumnname (String columnName, String propertyname, String referencedcolumn) {return stringhelper            . Isnotempty (ColumnName)? ColumnName:StringHelper.unqualify (PropertyName) + "_" + Referencedcolumn;}}

addunderscores is used for processing when table names and column names in Java rules conform to usernametable (table) and Usernamecolumn (columns) and are parsed into user_name_table and user_name_ column, the specific return is handled by Propertytocolumnname. But, if this rule is configured, Spring +JPA is configured as follows:
-

<prop key= "Hibernate.ejb.naming_strategy" >org.hibernate.cfg.ImprovedNamingStrategy</prop>

will ignore the comment in the @column name, in fact, this name is to map the database field, the result is configured this is not care. I'll go. Look at the hibernate explanation
https://hibernate.atlassian.net/browse/HHH-8616
Think is this is a bug, do not know why this fix bug.

Now there are 7-8 table rules that do not fit this, rewrite the following.

/** Copyright 2012-2014 sencloud.com.cn. All rights reserved.* support:http://www.sencloud.com.cn* License:http://www.sencloud.com.cn/license*/package Com.sencloud.hibernate.cfg;import Java.io.serializable;import Org.hibernate.cfg.improvednamingstrategy;import Org.hibernate.cfg.namingstrategy;import org.hibernate.util.stringhelper;/*** improvednamingstrategy- Hibernate mapping Rules * * @author xutianlong* @version 3.0*/public class Moshopimprovednamingstrategy extends Improvednamingstrategy    implements Namingstrategy, Serializable {/** *  */private static final long Serialversionuid = 3088474161734101900l;public string Propertytocolumnname (String propertyname) {    if ( Propertyname.endswith ("__")) {        return Propertyname.replace ("__", "" "). toLowerCase ();    }    Return Addunderscores (Stringhelper.unqualify (PropertyName));}}

Change the configuration:


<prop key= "Hibernate.ejb.naming_strategy" >com.sencloud.hibernate.cfg.moshopimprovednamingstrategy</ Prop>


So how do you use this? PropertyName must end with "__" and then unify toLowerCase in the rules. A little bit of that.
@Column (nullable = False, length=100) private String username__;


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.