Namingstrategy in Hibernate

xiaoxiao2021-03-06  20

In the Java object, I don't know that a good naming specification uses uppercase words, such as the order item, even the name of ORDERITEM, which is easy to see that this object is from ORDER and ITEM 2 If the word consists of words, it is easy, and the attribute is also the case, such as MaxPrice, Totalprice, and more. However, if the same naming specification is shipped to the database, because many database is not sensitive to the table name, the field name is not sensitive So the most common strategy is based on the basis of the line as a synonym: ORDERITEM -> ORDER_ITEM MAXPRICE -> MAX_PRICE When using Hibernate, you have to indicate in the mapping file: Every mapping relationship has to write this way, is it very uncomfortable? Now introduce net.sf.hibernate.cfg.namingstrategy's use, see See how it is repaired this problem, the code is as follows:

Java code:

Import Net.

sf.

Hibernate.

CFG.

Namingstrategy;

Import Net.

sf.

Hibernate.

Util.

Stringhelper;

/ ** * An improved naming statate trying trying with us on defaultnamingstrategy and improvednamingstrategy * * /

public

Class undercorenamingstrategy

Implements namingstrategy

{

public

Static

Final namingstrategy instance =

New understandcorenamingstrategy

(

);

Protected undercorenamingstrategy

(

)

{

}

public

String classtotablename

(

String classname

)

{

Return Addunderscores

Stringhelper.

UNQUALIFY

(ClassName

)

);

}

public

String propertyTytocolumnname

(

String PropertyName

)

{

Return Addunderscores

Stringhelper.

UNQUALIFY

(PropertyName)

)

);

}

public

String Tablename

(

String Tablename

)

{

Return TableName;

}

public

String columnname

(

String columnname

)

{

Return columnname;

}

public

String PropertyTotableName

(

String ClassName,

String PropertyName

)

{

Return ClasstotableName

(ClassName

)

'_' PropertyToColumnName

(PropertyName)

);

}

Private

String Addunderscores

(

String name

)

{

StringBuffer buf =

New

StringBuffer

(Name.Replace

(

'.',

'_'

)

);

for

(

INT i =

1; i

Length

(

)

1; i

)

{

IF

(

'_'! = BUF.

Charat

(i -

1

) &&

CHARACTER.

iSuppercase

(BUF.

Charat

(i

)

) &&!

CHARACTER.

iSuppercase

(BUF.

Charat

(i

1

)

)

)

{

BUF.

insert

(i ,

'_'

);

}

}

Return BUF.

Tostring

(

).

TOLOWERCASE

(

);

}

}

In the initialization configuration, add this NamingStrategy plus:

Java code:

Configuration config =

New Configuration

(

);

CONFIG.

Setnamingstrategy

Underscorenamingstrategy.

Instance

);

This mapping file has become more simple: Namingstrategy can also be used in other respects, such as some database design specification unified requests Table front plus module name (eg Uniform plus order_), such as some nausea, unified requirements, table names and field names (such as ORDERITEM -> Orde_Item, MaxPrice -> Max_Pric), these are Namingstrategy can solve the dirty dirty live.

转载请注明原文地址:https://www.9cbs.com/read-60376.html

New Post(0)