-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem setting the package name using Hibernate Tools
PostPosted: Wed May 28, 2008 2:46 am 
Beginner
Beginner

Joined: Thu Apr 17, 2008 2:03 pm
Posts: 26
Hi,

I am experiencing a problem with the package name declaration during pojo and mapping generation with Hibernate Tools without using Ant.

Code for generating pojos:

try {

cfg = new JDBCMetaDataConfiguration();
OverrideRepository or = new OverrideRepository();
InputStream xmlInputStream = new FileInputStream(FileUtil.toFile(revengFile));
xmlHelper = new XMLHelper();
entityResolver = XMLHelper.DEFAULT_DTD_RESOLVER;
List errors = new ArrayList();

SAXReader saxReader = xmlHelper.createSAXReader("XML InputStream", errors, entityResolver);
org.dom4j.Document doc = saxReader.read(new InputSource(xmlInputStream));
Configuration c = cfg.configure(confFile);
cfg.setReverseEngineeringStrategy(or.getReverseEngineeringStrategy(new DefaultReverseEngineeringStrategy()));
cfg.readFromJDBC();
} catch (Exception e) {
Exceptions.printStackTrace(e);
}

// Generating POJOs
FileObject pkg;
try {
pkg = SourceGroups.getFolderForPackage(helper.getLocation(), helper.getPackageName());
File outputDir = FileUtil.toFile(pkg);
POJOExporter exporter = new POJOExporter(cfg, outputDir);
exporter.getProperties().setProperty("jdk", new Boolean(helper.getJavaSyntax()).toString());
exporter.getProperties().setProperty("ejb3", new Boolean(helper.getEjbAnnotation()).toString());
exporter.start();
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}

// Generating Mappings
try {
pkg = SourceGroups.getFolderForPackage(helper.getLocation(), helper.getPackageName());
File outputDir = FileUtil.toFile(pkg);
HibernateMappingExporter exporter = new HibernateMappingExporter(cfg, outputDir);
exporter.start();
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}


hibernate.cfg.xml:


Located at C:\Documents and Settings\gowri\My Documents\NetBeansProjects\WebApplication1\src\java\hibernate.cfg.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="session1">
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/travel</property>
<property name="hibernate.connection.username">travel</property>
<property name="hibernate.connection.password">travel</property>
</session-factory>
</hibernate-configuration>


hibernate.reveng.xml:

located at C:\Documents and Settings\gowri\My Documents\NetBeansProjects\WebApplication1\src\java\hibernate.reveng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<table-filter exclude="false" match-catalog=".*" match-name="FLIGHT" match-schema=".*"/>
<table-filter exclude="false" match-catalog=".*" match-name="PERSON" match-schema=".*"/>
<table-filter exclude="false" match-catalog=".*" match-name="TRIP" match-schema=".*"/>
<table-filter exclude="false" match-catalog=".*" match-name="TRIPTYPE" match-schema=".*"/>
</hibernate-reverse-engineering>


in the code outputDir is
C:\Documents and Settings\gowri\MyDocuments\NetBeansProjects\WebApplication57\src\java\Travel.

But the generated pojo and mapping files don't contain package as Travel



// default package
// Generated May 27, 2008 12:45:56 AM by Hibernate Tools 3.2.1.GA


import java.util.Date;

/**
* Person generated by hbm2java
*/
public class Person implements java.io.Serializable {


private int personid;
private String name;
private String jobtitle;
private Short frequentflyer;
private Date lastupdated;

public Person() {
}


public Person(int personid) {
this.personid = personid;
}


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated May 27, 2008 12:45:57 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>

<class name="Person" table="PERSON" schema="TRAVEL">
<id name="personid" type="int">

class name should be Travel.Person.

Wondering why tools is not setting the package right ?

Secondly, even though I have defined the HibernateReverseEngineeringStrategy, why tools is generating pojos and mapping files for all the tables in the db ? (Note:I have listed only selected tables in the hibernate.reveng.xml).


Last edited by Gowri on Fri May 30, 2008 4:11 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 4:10 pm 
Beginner
Beginner

Joined: Thu Apr 17, 2008 2:03 pm
Posts: 26
Can someone help me please ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 10:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
this is the third posting....please don't double nor triple post. thanks.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 1:51 pm 
Beginner
Beginner

Joined: Thu Apr 17, 2008 2:03 pm
Posts: 26
I was able to solve the package name problem by using the ReverseEngineeringSettings and invoking the setDefaultPackageName() API.

DefaultReverseEngineeringStrategy strategy = new DefaultReverseEngineeringStrategy();
settings = new ReverseEngineeringSettings(strategy); settings.setDefaultPackageName(helper.getPackageName());
strategy.setSettings(settings);
cfg.setReverseEngineeringStrategy(strategy);


But I was not able to solve my second problem where tools is generating POJO's and mapping files for all the tables in the DB even though my reveng.xml lists only a subset of the DB tables.

I am not using a custom reverse engineering strategy. Is it possible for pojo generator/mapping generator to read all the tables in the schema and ignore the "selected" tables defined in reveng.xml. And hence creating the additional java and mapping files.

I would appreciate if you can tell me what I am missing.

I am attaching my modified code snippets:

cfg = new JDBCMetaDataConfiguration();
OverrideRepository or = new OverrideRepository();
Configuration c = cfg.configure(confFile); // hibernate.cfg.xml
or.addFile(revengFile); // hibernate.reveng.xml
DefaultReverseEngineeringStrategy strategy = new DefaultReverseEngineeringStrategy();
settings = new ReverseEngineeringSettings(strategy);
settings.setDefaultPackageName(helper.getPackageName()); // sets package name
strategy.setSettings(settings);
cfg.setReverseEngineeringStrategy(strategy);
cfg.readFromJDBC();



And the hibernate.reveng.xml is

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<table-filter exclude="false" match-catalog=".*" match-name="HOTEL" match-schema="TRAVEL"/>
<table-filter exclude="false" match-catalog=".*" match-name="PERSON" match-schema="TRAVEL"/>
<table-filter exclude="false" match-catalog=".*" match-name="TRIP" match-schema="TRAVEL"/>
<table-filter exclude="false" match-catalog=".*" match-name="TRIPTYPE" match-schema="TRAVEL"/>
</hibernate-reverse-engineering>


Only 4 tables are selected as part of the hibernate.reveng.xml. But pojos and mappings are generated for all the tables in the TRAVEL db. Also the selected 4 tables are not dependent on the other tables in the schema.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 2:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
as far as I can see you never use the OverrideRepository's reverse engineeringstrategy meaning your reveng.xml is never used.

You need something like:

cfg.setReverseEngineeringStrategy(or.getReverseEngineeringStrategy(yourRevEngStrategy));

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.