[
http://opensource.atlassian.com/projects/hibernate/browse/HBX-911?page=all ]
Sorin Silaghi updated HBX-911:
------------------------------
Attachment: default-values.clean.patch
The first patch was a bit messy because I had some code formating options active when I
committed to my SVN. I started fresh and added only the changes I made to the classes...
It looks allot better this way. I just hope I haven't missed anything. If this one
doesn't work please try the first one.
Get Default Values From DB
---------------------------
Key: HBX-911
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HBX-911
Project: Hibernate Tools
Type: Patch
Components: reverse-engineer
Versions: 3.2beta9
Environment: I worked on this patch from the TOOLS_3_2_0_BETA9 branch in SVN. I
didn't actually compile the whole of hibernate-tools, I just compiled the classes I
wanted to change and got the rest of the binaries from the .jar in the maven repository.
If necessary I could provide a maven project that compiles the whole thing into a jar.
Reporter: Sorin Silaghi
Attachments: default-values.clean.patch, default-values.patch
I needed this for the project I am working on. The "client" requested this
"feature"... that's for motivation. I don't think it's a great idea
but it's useful.
First of all sorry for not being able to provide test cases.. I didn't had the time
so I used the project I'm working on for tests.
This is how it works:
I modified JDBCReader and JDBCMetaDataDialect so that the default value is retrieved
from the DB.
In JDBCBinder in the bindMeta the default value is added as a meta tag. To give user
control over the process I added a couple of methods to the reveng strategy interface:
public boolean useDefaultValueOnHibernateType(String hibernateType);
- this will return true for the hibernateTypes that should have a default value(if
available).
public String convertDefaultValueForColumn(TableIdentifier table, String columnName,
String sqlTypeName, String hibernateTypeName, boolean isForeignKey, String value);
- and this one is used to convert the data to a form better suited for the
persistence classes
I also modified the BasicPOJOClass class so that if the default value saved in the
hbm is "" the initialization in the entity class will be the default
constructor, for example: "new Date();".
Below is an example of how to implement a custom convertDefaultValueForColumn. Part
of this could be moved to the default reveng strategy. We decided to threat fields that
are foreign keys by creating an empty object with the id specified in the database and
dealing with the retrieval of the rest of the data in the service layer. It actually looks
better than it sounds in our case.
public String convertDefaultValueForColumn(TableIdentifier table, String columnName,
String sqlTypeName, String hibernateTypeName, boolean isForeignKey, String value) {
if (isForeignKey){
if (hibernateTypeName.contains("org.kobit.ofam.service.model")){
String id;
try{
id = Long.valueOf(value).toString() + "L";
}catch(Exception e){
return null;
}
return "new " +
hibernateTypeName.substring(hibernateTypeName.lastIndexOf(".") +
1)+"("+ id +")";
}
}
Class hibernateTypeClass = null;
try {
hibernateTypeClass = Class.forName(hibernateTypeName);
} catch (ClassNotFoundException e){
log.error("type not found: " + hibernateTypeName);
}
try{
if (hibernateTypeClass!=null){
if (hibernateTypeClass.equals(String.class)){
value = "\"" + value.substring(1,value.length()-1) +
"\"";
return value;
}
if (hibernateTypeClass.equals(Long.class))
return Long.valueOf(value).toString() + "L";
if (hibernateTypeClass.equals(Double.class))
return Double.valueOf(value) + "D";
if (hibernateTypeClass.equals(Boolean.class))
return (Boolean.valueOf(Long.valueOf(value).longValue() >
0L)).toString();
if (hibernateTypeClass.equals(Date.class)) {
DateFormat df = DateFormat.getInstance();
return df.format(df.parse(value));
}
}
}catch(Exception e){
log.error("error converting type " + hibernateTypeName + "
:" + e.getMessage() + " for value " + value);
return "";
}
return "";
}
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira