-->
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.  [ 10 posts ] 
Author Message
 Post subject: Trying to generate hibernate.cfg.xml when using Annotations
PostPosted: Fri Jan 30, 2009 12:37 pm 
Newbie

Joined: Fri Jan 30, 2009 12:03 pm
Posts: 6
Hibernate version: 3.3.1 GA

Using Ant, I am attempting to auto-generate the hibernate.cfg.xml file as part of my build process. In the past, I was using Xdoclet tags in my POJO's, and Xdoclet would generate the config files and DDL. I have moved to Hibernate 3, and I implemented EJB3 Annotations in the POJO's. I am using hbm2ddl successfully to generate the schema, but I can not figure out how to generate the hibernate.cfg.xml file. I need hbm2cfgxml to generate a file with "<mapping class="my.class"> for each of my POJO's, instead of generating individual .xbm.xml files, and "<mapping resource=class.xbm.xml" in the config file.

In essence, I'm attempting the following, by pointing directly to my POJO's:
Code:
<target name="hibernate.cfg.xml" depends="init">
    <taskdef
        name="hibernatetool"
        classname="org.hibernate.tool.ant.HibernateToolTask"
        classpathref="xdoclet.class.path"
    />
    <hibernatetool destdir="${build.class.dir}">
        <property key="version"      value="3.0"                     />
        <property key="dataSource"   value="${hibernate.datasource}" />
        <property key="userName"     value="${db.user}"              />
        <property key="password"     value="${db.password}"          />
        <property key="useOuterJoin" value="${hibernate.outer.join}" />
        <property key="dialect"      value="${hibernate.dialect}"    />
        <property key="showSql"      value="${hibernate.show.sql}"   />
        <annotationconfiguration configurationfile="${build.class.dir}/hibernate.cfg.xml">
            <fileset dir="${java.dir}">
                <include name="**/domain/**/*.java" />
            </fileset>
        </annotationconfiguration>
        <hbm2cfgxml ejb3="true" />
    </hibernatetool>
</target>


This fails with a "Content is not allowed in prolog." error, attempting to parse the POJO, as it seems to only support the xbm.xml files. Also, if I attempt to do this from scratch, I get a "hibernate.cfg.xml file not found" error. I must place a template config file there first, then this will run and regenerate a new file.

Am I going about this completely wrong? I really appreciate any help on this.
Thanks!


Top
 Profile  
 
 Post subject: Re: Trying to generate hibernate.cfg.xml when using Annotati
PostPosted: Sun Feb 01, 2009 6:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
scottsmith wrote:

In essence, I'm attempting the following, by pointing directly to my POJO's:
Code:
<target name="hibernate.cfg.xml" depends="init">
    <taskdef
        name="hibernatetool"
        classname="org.hibernate.tool.ant.HibernateToolTask"
        classpathref="xdoclet.class.path"
    />
    <hibernatetool destdir="${build.class.dir}">
        <property key="version"      value="3.0"                     />
        <property key="dataSource"   value="${hibernate.datasource}" />
        <property key="userName"     value="${db.user}"              />
        <property key="password"     value="${db.password}"          />
        <property key="useOuterJoin" value="${hibernate.outer.join}" />
        <property key="dialect"      value="${hibernate.dialect}"    />
        <property key="showSql"      value="${hibernate.show.sql}"   />
        <annotationconfiguration configurationfile="${build.class.dir}/hibernate.cfg.xml">
            <fileset dir="${java.dir}">
                <include name="**/domain/**/*.java" />
            </fileset>
        </annotationconfiguration>
        <hbm2cfgxml ejb3="true" />
    </hibernatetool>
</target>


This fails with a "Content is not allowed in prolog." error, attempting to parse the POJO, as it seems to only support the xbm.xml files.


yes, the include is for hbm.xml.

Why don't you just use Hibernate entity manager which picks upthe classes automatically so you dont have to maintain a cfg.xml ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: Trying to generate hibernate.cfg.xml when using Annotati
PostPosted: Mon Feb 02, 2009 3:01 am 
Newbie

Joined: Fri Jan 30, 2009 12:03 pm
Posts: 6
max wrote:
yes, the include is for hbm.xml.

Why don't you just use Hibernate entity manager which picks up the classes automatically so you dont have to maintain a cfg.xml ?

Because I need to generate the schema DDL at build time. I used to use Xdoclet, and was able to create the hibernate config, all the mapping files, and the DB schema at build time. Now that I use annotations, I am removing Xdoclet. I am able to generate the DDL using hbm2ddl, but that requires the hibernate config to contain the "<mapping class=...". Is there a better way to do all this? I'm all ears!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 6:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes, use hibernate entitymanager/JPA

It has autodiscovery based on the classpath.

then you just use <jpaconfiguration ..> instead.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2009 6:00 pm 
Newbie

Joined: Fri Jan 30, 2009 12:03 pm
Posts: 6
Thanks, that worked!

I have converted to <jpaconfiguration ..>.
Unfortunately, I now require a "persistence.xml" file. This information used to be contained in the hibernate.cfg.xml file, which I auto-generated using hbm2cfgxml (please see ant example in first post). Do you know of an easy way to auto generate the persistence.xml file?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 5:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
take any example of basic persistence.xml from hibernate entitymanager docs

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 6:02 pm 
Newbie

Joined: Fri Jan 30, 2009 12:03 pm
Posts: 6
max wrote:
take any example of basic persistence.xml from hibernate entitymanager docs

Yes, I did that, and it works. I only ask if you know of an easy way to auto generate this file. Before, I used:

Code:
<hibernatetool destdir="${build.class.dir}">
    <property key="version"      value="3.0"                     />
    <property key="dataSource"   value="${hibernate.datasource}" />
    <property key="userName"     value="${db.user}"              />
    <property key="password"     value="${db.password}"          />
    <property key="useOuterJoin" value="${hibernate.outer.join}" />
    <property key="dialect"      value="${hibernate.dialect}"    />
    <property key="showSql"      value="${hibernate.show.sql}"   />
    <annotationconfiguration configurationfile="${build.class.dir}/hibernate.cfg.xml">
        <fileset dir="${java.dir}">
            <include name="**/domain/**/*.java" />
        </fileset>
    </annotationconfiguration>
    <hbm2cfgxml ejb3="true" />
</hibernatetool>


The values for these properties currently come from different properties files in my project. For example, the showSql is in my generic "database.properties" file, and the userName and password values come from my specific "postgres.properties" file, etc. I suppose I could just hack it with a giant echo task... :(

Code:
<!-- Hack to build the persistence configuration file -->
<mkdir dir="${build.class.dir}/META-INF"   />
<echo file="${build.class.dir}/META-INF/persistence.xml" append="false">
    &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
        version="1.0"&gt;
        &lt;persistence-unit name="rss.domains"&gt;
                &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
            &lt;jta-data-source&gt;${hibernate.datasource}&lt;/jta-data-source&gt;
            &lt;properties&gt;
                &lt;property name="hibernate.transaction.factory_class"
                              value="org.hibernate.transaction.JTATransactionFactory" /&gt;
                &lt;property name="hibernate.connection.datasource" value="${hibernate.datasource}" /&gt;
                &lt;property name="hibernate.connection.username" value="${db.user}" /&gt;
                &lt;property name="hibernate.connection.password" value="${db.password}" /&gt;
                &lt;property name="hibernate.dialect" value="${hibernate.dialect}" /&gt;
                &lt;property name="hibernate.show_sql" value="${hibernate.show.sql}" /&gt;
                &lt;property name="hibernate.use_outer_join" value="${hibernate.outer.join}" /&gt;
                &lt;property name="hibernate.jdbc.use_scrollable_resultset" value="${hibernate.scrollable_recordset}" /&gt;
                &lt;property name="hibernate.bytecode.use_reflection_optimizer" value="${hibernate.bytecode.use_reflection_optimizer}" /&gt;
            &lt;/properties&gt;
        &lt;/persistence-unit&gt;
    &lt;/persistence&gt;
</echo>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 7:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
why don't you just create a persistence.xml.template and use ant property expression to replace them ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2009 8:14 pm 
Newbie

Joined: Fri Jan 30, 2009 12:03 pm
Posts: 6
That sounds great, can you give me an example?

I assume you mean that the template file will look like:
Code:
...
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>${hibernate.datasource}</jta-data-source>
<properties>
    <property name="hibernate.transaction.factory_class"
              value="org.hibernate.transaction.JTATransactionFactory" />
    <property name="hibernate.connection.datasource" value="${hibernate.datasource}" />
    <property name="hibernate.connection.username" value="${db.user}" />
    <property name="hibernate.connection.password" value="${db.password}" />
...
</properties>
...

And that I will do something in ant to parse the file, and replace the variables somehow?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 3:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
http://ant.apache.org/manual/CoreTasks/filter.html

_________________
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.  [ 10 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.