-->
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.  [ 3 posts ] 
Author Message
 Post subject: Xdoclet generation of xml configuration mappings
PostPosted: Tue Feb 17, 2004 10:49 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:06 pm
Posts: 26
Location: Tampa, FL
Is there an xdoclet / hibernate task to generate the "mappings" portion of a hibernate.cfg.xml config file? I'm using the <hibernatedoclet> task to generate the *.hbm.xml files and it seems like adding the mappings to the config file is the natural next step.

Using Hibernate 2.1.2

_________________
Bill Pfeiffer


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 3:08 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
No.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 10:39 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 BeanShell 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
Code:
  <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
Code:
<?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
Code:
// 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("NoSuchFile", "hereNow");
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  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.