-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Wrong package after customized reverse engineering
PostPosted: Mon Jan 04, 2010 11:26 am 
Newbie

Joined: Mon Jul 20, 2009 5:09 pm
Posts: 16
Hello,

I use Hibernate Code Generation from Eclipse 3.5 with a customized reveng.strategy. Here is the example code:

package com.modules.database.implementation.hibernate.util;

import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.TableIdentifier;

public class MyReverseEngineering extends DelegatingReverseEngineeringStrategy {

public MyReverseEngineering(ReverseEngineeringStrategy delegate) {
super(delegate);
}



/**
* {@inheritDoc}
*/
@Override
public String tableToClassName(TableIdentifier tableIdentifier) {

if (tableIdentifier != null) {
System.out.println(tableIdentifier.getName());
final boolean startsWithT = tableIdentifier.getName().startsWith(
"T_");
final boolean startsWitht = tableIdentifier.getName().startsWith(
"t_");
if (startsWithT || startsWitht) {
final String firstLetter = tableIdentifier.getName().substring(
2,
3).toUpperCase();
final String substring = tableIdentifier.getName().substring(3);
final String tableName = firstLetter.concat(substring);
return tableName;
} else {
return super.tableToClassName(tableIdentifier);
}
} else {
System.out.println("tableIdentifier is null!");
return "";
}
}

}

The java files are named as desired but the package is wrong. All files are created in the default package. The given packge is ignored. If I remove my strategy the java files are created in the desired package.

What do I wrong?

Thanks for your help.

Best regards,
Till


Top
 Profile  
 
 Post subject: Re: Wrong package after customized reverse engineering
PostPosted: Mon Jan 04, 2010 12:40 pm 
Newbie

Joined: Mon Jul 20, 2009 5:09 pm
Posts: 16
I got it!

The mistake I made was that the package name must be part of the returned String:

import org.apache.log4j.Logger;
import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.TableIdentifier;

public class MyReverseEngineering extends
DelegatingReverseEngineeringStrategy {

private static Logger LOGGER = Logger.getLogger(MyReverseEngineering.class);

/**
* A constructor.
*
* @param delegate
* The fall back strategy.
*/
public MyReverseEngineering(ReverseEngineeringStrategy delegated) {
super(delegated);
}

/**
* {@inheritDoc}
*/
@Override
public String tableToClassName(TableIdentifier tableIdentifier) {

final String defaultClassName = super.tableToClassName(tableIdentifier);
LOGGER.warn(defaultClassName);
final String packageName = this.extractPackageName(defaultClassName);
LOGGER.warn(packageName);
final String className = this.defineClassName(tableIdentifier);
LOGGER.warn(className);
final String concatedString = packageName.concat(className);
LOGGER.warn(concatedString);
return concatedString;
}

private String extractPackageName(String defaultClassName) {
int lastIndex = defaultClassName.lastIndexOf(".");
final String packageName = defaultClassName.substring(0, lastIndex + 1);
return packageName;
}

private String defineClassName(TableIdentifier tableIdentifier) {
final boolean startsWithT = tableIdentifier.getName().startsWith("T_");
final boolean startsWitht = tableIdentifier.getName().startsWith("t_");
if (startsWithT || startsWitht) {
final String firstLetter = tableIdentifier.getName().substring(2, 3).toUpperCase();
final String substring = tableIdentifier.getName().substring(3);
final String tableName = firstLetter.concat(substring);
return tableName;
} else {
return super.tableToClassName(tableIdentifier);
}
}
}

This solutions seems to be a little bit ugly. But how can reach ReverseStrategySettings from my strategy?
If somebody knows it, please tell me. Otherwise I will use my solution.

Thanks,
Till


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.