Last active
January 1, 2016 01:43
-
-
Save qfrank/05fda8376b2c885a7e00 to your computer and use it in GitHub Desktop.
模仿grails NamingStrategy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ys.scs.persistence; | |
import org.hibernate.AssertionFailure; | |
import org.hibernate.cfg.ImprovedNamingStrategy; | |
import org.hibernate.cfg.NamingStrategy; | |
import org.hibernate.internal.util.StringHelper; | |
/** | |
* User: Frank Tang <br/> | |
* Date: 15/11/26<br/> | |
* Time: 下午6:52<br/> | |
* Email: [email protected]<br/> | |
*/ | |
public class MyImprovedNamingStrategy extends ImprovedNamingStrategy { | |
public static final NamingStrategy INSTANCE = new MyImprovedNamingStrategy(); | |
@Override | |
public String columnName(String columnName) { | |
String colName = addUnderscores(columnName); | |
return colName; | |
} | |
@Override | |
public String propertyToColumnName(String propertyName) { | |
propertyName = propertyName.replace("id.","").replace('.', '_'); | |
return addUnderscores(propertyName); | |
} | |
@Override | |
public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) { | |
String header = propertyName != null ? propertyName : propertyTableName; | |
if (header == null) throw new AssertionFailure("NamingStrategy not properly filled"); | |
header = header.replace("id.",""); | |
header = addUnderscores(header); | |
return columnName(header + "_id"); | |
} | |
@Override | |
public String logicalColumnName(String columnName, String propertyName) { | |
return StringHelper.isNotEmpty(columnName) ? columnName : propertyToColumnName(propertyName); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment