-->
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.  [ 8 posts ] 
Author Message
 Post subject: listing mappings in hibernate.cfg.xml file
PostPosted: Mon Feb 23, 2004 6:37 pm 
Beginner
Beginner

Joined: Thu Feb 05, 2004 10:39 pm
Posts: 44
When I list the hibernate class mappings in my hibernate.cfg.xml file, must I list each file individually or can I specify a pattern like:

**/hibernate/**/*.hbm.xml?


Top
 Profile  
 
 Post subject: also...
PostPosted: Mon Feb 23, 2004 6:38 pm 
Beginner
Beginner

Joined: Thu Feb 05, 2004 10:39 pm
Posts: 44
If I have to list each file individually, is there an Ant task that can generate the hibernate.cfg.xml file for me then?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 6:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can't use wildcards, sorry. You either have to list them all, or package them in a jar and use <mapping jar="your.jar">


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 6:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No, there is no ant task - probably would be a good opportunity to get into xdoclet and write something that autogenerates the hibernate.cfg.xml :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 7:26 pm 
Beginner
Beginner

Joined: Thu Feb 05, 2004 10:39 pm
Posts: 44
actually, done some searching and it seems this guy wrote an ant task he wants to donate to the project:


Top
 Profile  
 
 Post subject: Ant/BeanShell script to create configuration file list
PostPosted: Mon Mar 01, 2004 7:53 pm 
Newbie

Joined: Mon Mar 01, 2004 6:09 pm
Posts: 3
Location: Kensington, NH, USA
Here is an Ant task I developed to create the mapping list. It uses Bean Shell to generate the file list (named hbnfiles.xml), which is included into a simple hibernate.cfg.xml file.

I had some trouble getting BeanShell to run at first, until I installed the latest Ant version.

-Phil

-------- File Snippet: build.xml
<target name="file-list"
description="Create hibernate mapping file list">
<property name="generated.dir" location="target/classes"/>
<property name="hibernate.list.file" location="hbnfiles.xml"/>
<script language="beanshell" src="create_hbnfiles.bsh"/>
</target>


-------- File: hibernate.cfg.xml
<?xml version="1.0"?>

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd" [
<!ENTITY fileList SYSTEM "file:hbnfiles.xml">
]>

<hibernate-configuration>

<session-factory
name="java:comp/env/hibernate/SessionFactory">

<!-- This is a list of all the mapping files to
be used for this configuration.
-->
&fileList;

</session-factory>

</hibernate-configuration>

---------- File: create_hbnfiles.bsh

// This file contains a BeanShell script that
// works together with the Ant project file.
// This script searches for hibernate mapping
// files and writes the filenames out to a
// hibernate configuration file.

// Read properties configured in the Ant
// project file.
//
// This property tells us where to look for
// hibernate mapping files.
String basedirName = project.getProperty("generated.dir");
print("Search for hibernate files in " + basedirName);

// This property the filename to use when writing
// out the hibernate file list.
String outFilename = project.getProperty("hibernate.list.file");
print("Output to " + outFilename);

/**********************************************/

/*
* Write a line to the configuration file.
*/
void dumpFilename(String s) {
out.println("<mapping resource=\"" + s + "\"/>");
}

/**********************************************/
/*
* Combine the relative paths together to
* form a complete path. If either is empty,
* then there is no separator since they are
* not really being combined.
*/
String relativeName(String base, String name) {
if (base.length() == 0 || name.length() == 0)
return base + name;
else
return base + "/" + name;
}

/**********************************************/
/*
* This method reads the directory specified
* and looks for file names to output to the
* configuration file. It invokes itself
* recursively to find all files.
*/
void readDir(String relativePath) {
// Combine relativePath with basedirName
// to get get full path
String fullName = relativeName(basedirName, relativePath);
File dir = new File(fullName);

// Get a listing of all directories
// and all files ending in .hbm.xml
//
String[] list = dir.list(new FilenameFilter() {
boolean accept(File dir, String name) {
File f = new File(dir, name);
if (f.isDirectory())
return true;
else if (f.isFile() && name.endsWith(".hbm.xml"))
return true;
else
return false;
}
});

count = 0;
for (i=0; i<list.length; i++) {
f = new File(fullName + "/" + list[i]);
if (f.isFile()) {
dumpFilename(relativeName(relativePath, f.getName()));
count++;
} else if (f.isDirectory()) {
readDir(relativeName(relativePath, f.getName()));
}
}

// Print out summary of what we found
if (count > 0) {
print("readDir: " + relativePath);
print("Found Files: " + count);
}
}

/**********************************************/
// MAIN

// Keep a backup copy of the current file.
mv(outFilename, outFilename+".bak");

File configFile = new File(outFilename);
PrintWriter out = new PrintWriter(new FileWriter(configFile));

// Kick things off by passing in an empty String.
//
readDir("");

// Close the config file we just created.
out.close();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 4:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You could put it on the wiki community page if you want.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 10:48 am 
Newbie

Joined: Mon Mar 01, 2004 6:09 pm
Posts: 3
Location: Kensington, NH, USA
Thanks. I updated the Wiki at...

http://www.hibernate.org/72.html


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