i use my Exporter to generate java model by pojo template which in outside directory,but only the id class for composite key cann't be generated.
my exporter:
Code:
public class ProfileExporter extends GenericExporter {
private String profile = null;//example:service,model,web
public String getProfile() {
if (profile == null) {
return "";
}
return profile;
}
public ProfileExporter() {
super();
}
public ProfileExporter(org.hibernate.cfg.Configuration cfg, File outputdir) {
super(cfg, outputdir);
}
public void setProfile(String profile) {
this.profile = profile;
}
protected void exportPOJO(Map additionalContext, POJOClass element) {try {
TemplateProducer producer = new TemplateProducer(getTemplateHelper(),getArtifactCollector());
log.debug("POJOClass Instance : " + element);
log.debug("POJOClass GetDecoratedObject Instance : " + element);
additionalContext.put("pojo", element);
additionalContext.put("clazz", element.getDecoratedObject());
//if generate java class ,the package=basePackage + profile + module,the filename = the package + name;
//else the filename = profile + module + name
String tableName = null;
Object obj = element.getDecoratedObject();
if (obj instanceof PersistentClass) {
PersistentClass pClazz = (PersistentClass) obj ;
tableName = pClazz.getTable().getName();
}
else if (obj instanceof Component) {
Component pClazz = (Component) obj ;
tableName = pClazz.getTable().getName();
}
if (tableName == null || tableName.equals("")) {
log.error("Cann't get the table name");
}
String moduleName = this.getModuleName(tableName);
additionalContext.put("profile", this.getProfile());
additionalContext.put("module", moduleName);
String filename = StringHelper.replace(this.getFilePattern(), "{class-name}", getClassNameForFile( element ));
filename = StringHelper.replace(filename, "{module-name}", moduleName.equals("")?null:moduleName);
filename = StringHelper.replace(filename, "{profile-name}", this.getProfile());
String packageLocation = StringHelper.replace(getPackageNameForFile( element ),".", "/");
if(StringHelper.isEmpty(packageLocation)) {
packageLocation = "."; // done to ensure default package classes doesn't end up in the root of the filesystem when outputdir=""
}
filename = StringHelper.replace(filename, "{package-name}", packageLocation);
if(filename.endsWith(".java") && filename.indexOf('$')>=0) {
log.warn("Filename for " + getClassNameForFile( element ) + " contains a $. Innerclass generation is not supported.");
}
producer.produce(additionalContext, getTemplateName(), new File(getOutputDirectory(),filename), this.getTemplateName());}catch(Exception e){e.printStackTrace();}
}
protected String getModuleName(String tableName) {
String module = Configuration.MODULE.getTableModule(tableName);
if (module == null) {
module = "";
}
return module;
}
protected void setupContext() {
if(!getProperties().containsKey("ejb3")) {
getProperties().put("ejb3", "false");
}
if(!getProperties().containsKey("jdk5")) {
getProperties().put("jdk5", "false");
}
super.setupContext();
}
}
my ant task:
Code:
public class ProfileExporterTask extends GenericExporterTask {
protected Log log = LogFactory.getLog(ProfileExporterTask.class);
private String profile = null;
private boolean isEjb3 = false;
public ProfileExporterTask(Gen4dbToolTask parent) {
super(parent);
}
public Exporter createExporter() {
if (exporterClass == null) {
return new ProfileExporter();
} else {
try {
Class theClass = ReflectHelper.classForName(exporterClass);
return (Exporter) theClass.newInstance();
} catch (ClassNotFoundException e) {
throw new BuildException(
"Could not find custom exporter class: "
+ exporterClass, e);
} catch (InstantiationException e) {
throw new BuildException(
"Could not create custom exporter class: "
+ exporterClass, e);
} catch (IllegalAccessException e) {
throw new BuildException(
"Could not access custom exporter class: "
+ exporterClass, e);
}
}
}
public Exporter configureExporter(Exporter exporter) {
super.configureExporter(exporter);
log.debug("begin set ProfileExporter......");
ProfileExporter exp = (ProfileExporter) exporter;
if (filePattern != null) {
exp.setFilePattern(filePattern);
}
if (templateName != null) {
exp.setTemplateName(templateName);
}
if (profile != null) {
exp.setProfile(profile);
}
if (isEjb3) {
exp.getProperties().setProperty("ejb3", "" + isEjb3);
exp.getProperties().setProperty("jdk5", "" + isEjb3);
}
return exporter;
}
/**
* @return the isEjb3
*/
public boolean isEjb3() {
return isEjb3;
}
/**
* @param isEjb3
* the isEjb3 to set
*/
public void setEjb3(boolean isEjb3) {
this.isEjb3 = isEjb3;
}
/**
* @return the profile
*/
public String getProfile() {
return profile;
}
/**
* @param profile
* the profile to set
*/
public void setProfile(String profile) {
this.profile = profile;
}
public String getName() {
return "Profile Exporter";
}
}
my ant configuration:
Code:
<hibernate templatepath=".">
<jdbcconfiguration propertyfile="gen4db_build.properties" packagename="${basePackage}" reverseStrategy="com.googlecode.gen4db.util.MyReverseEngineeringDelegator" revengfile="${revengFile}" detectmanytomany="false" preferBasicCompositeids="false"/>
<property key="hibernatetool.util.toolclass" value="com.googlecode.gen4db.util.Util" />
<profiletemplate ejb3="true" profile="model" filepattern="{package-name}/{profile-name}/{module-name}/{class-name}.java" template="${template.path.pojo}/Pojo.ftl" destdir="${gen.dir}/src/model" foreach="entity" />
</hibernate>
my model generated:
Code:
@Entity
@Table(name = "t_test_many_pk", catalog = "gen4db")
public class TestManyPk implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private TestManyPkId id;
private TestPk2 testPk2;
private TestPk1 testPk1;
private String description;
public TestManyPk() {
}
public TestManyPk(TestManyPkId id, TestPk2 testPk2, TestPk1 testPk1) {
this.id = id;
this.testPk2 = testPk2;
this.testPk1 = testPk1;
}
public TestManyPk(TestManyPkId id, TestPk2 testPk2, TestPk1 testPk1,
String description) {
this.id = id;
this.testPk2 = testPk2;
this.testPk1 = testPk1;
this.description = description;
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "testPk1Id", column = @Column(name = "test_pk_1_id", nullable = false)),
@AttributeOverride(name = "testPk2Id", column = @Column(name = "test_pk_2_id", nullable = false))})
@NotNull
public TestManyPkId getId() {
return this.id;
}
public void setId(TestManyPkId id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "test_pk_2_id", nullable = false, insertable = false, updatable = false)
@NotNull
public TestPk2 getTestPk2() {
return this.testPk2;
}
public void setTestPk2(TestPk2 testPk2) {
this.testPk2 = testPk2;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "test_pk_1_id", nullable = false, insertable = false, updatable = false)
@NotNull
public TestPk1 getTestPk1() {
return this.testPk1;
}
public void setTestPk1(TestPk1 testPk1) {
this.testPk1 = testPk1;
}
@Column(name = "description", length = 200)
@Length(max = 200)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String toString() {
StringBuffer sb = new StringBuffer(getClass().getSimpleName());
sb.append(" [");
sb.append("id").append("='").append(getId()).append("', ");
sb.append("testPk2").append("='").append(getTestPk2()).append("', ");
sb.append("testPk1").append("='").append(getTestPk1()).append("', ");
sb.append("description").append("='").append(getDescription()).append(
"'");
sb.append("]");
return sb.toString();
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
TestManyPk pojo = (TestManyPk) o;
if (testPk2 != null
? !testPk2.equals(pojo.testPk2)
: pojo.testPk2 != null)
return false;
if (testPk1 != null
? !testPk1.equals(pojo.testPk1)
: pojo.testPk1 != null)
return false;
if (description != null
? !description.equals(pojo.description)
: pojo.description != null)
return false;
return true;
}
public int hashCode() {
int result = 0;
result = (testPk2 != null ? testPk2.hashCode() : 0);
result = 31 * result + (testPk1 != null ? testPk1.hashCode() : 0);
result = 31 * result
+ (description != null ? description.hashCode() : 0);
return result;
}
}
i compare ProfileExporter to POJOExporter,i don't find the defference,but TestManyPkId class cann't be generated.
any help will be deeply appreciated!