-->
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.  [ 9 posts ] 
Author Message
 Post subject: Nothing being generated by Ant and XDoclet
PostPosted: Thu Dec 18, 2003 3:27 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
I'm a total noob who's having trouble getting off the ground with Hibernate. I'm trying to use XDoclet for the first time and I'm having a problem.

I've got Ant 1.5.4 installed on my desktop. I've added 7 JARs to my ANT_HOME/lib: commons-collections-2.0.jar, commons-logging.jar, xjavadoc-1.0.jar, xdoclet-xdoclet-module-1.2b4.jar, xdoclet-hibernate-module-1.2b4.jar, and xdoclet-1.2b4.jar. I've got a build.xml that's tried and true - I've used it many times. I added the following new task to generate *.hbm.xml for me:

Code:
    <target name="generate" depends="compile" description="run auto-generate tools">
       
        <taskdef name="hibernate-xdoclet"     
                 classname="xdoclet.modules.hibernate.HibernateDocletTask"     
                 classpath="project.class.path"/>

        <hibernate-xdoclet destdir="${output}"
                           excludedtags="@version,@author,@todo"
                           force="true"
                           mergedir="${output}"
                           verbose="false">
            <fileset dir="${src.java}">
                <include name="**/*.java"/>
            </fileset>
        </hibernate-xdoclet>
       
        <!-- Upgrade grammar from Hibernate1 to Hibernate2 -->
        <replace dir="${output}">
            <include name="**/*.hbm.xml"/>
            <replacefilter token="readonly=" value="inverse="/>
            <replacefilter token="role=" value="name="/>
            <replacefilter token="hibernate-mapping.dtd" value="hibernate-mapping-2.0.dtd"/>
        </replace>
               
    </target>


I've added Hibernate XDoclet tags to the classes from "Introduction to Hibernate" by Nick Heudecker [url]http://www.systemmobile.com/articles/IntroductionToHibernate.html#The%20Source%20Files
[/url]

Here's one of them:

Code:
package example;

import java.io.Serializable;

import java.util.Date;


/**
* Sample from TSS "Intro to Hibernate" article
* @author Michael O. Duffy Wed 17-Dec-2003
* @hibernate.class table="players"
*/
public class Player implements Comparable, Serializable
{
    /** Open bracket around instance in String representation */
    public static final String OPEN_BRACKET = "{";

    /** Close bracket around instance in String representation */
    public static final String CLOSE_BRACKET = "}";

    /** Separator between fields in String representation */
    public static final String SEPARATOR = ",";

    /** Unique database id */
    private long id;

    /** Player first name */
    private String firstName;

    /** Player family name */
    private String lastName;

    /** Draft date */
    private Date draftDate;

    /** Annual salary (ought to be a money type) */
    private float annualSalary;

    /** Jersey number (why not a String for "00"?) */
    private int jerseyNumber;

    /** Parent team instance */
    private Team team;




    /**
     * Copy constructor
     * @param Player instance to copy
     */
    public Player(Player p)
    {
        this.id         = p.id;
        this.firstName  = p.firstName;
        this.lastName   = p.lastName;
        this.draftDate  = new Date(p.draftDate.getTime());
    }



    /**
     * Read access to the annualSalary
     * @hibernate.property column="annual_salary" type="float"
     * @return the annualSalary.
     */
    public float getAnnualSalary()
    {
        return this.annualSalary;
    }




    /**
     * Write access to the annualSalary
     * @param new value for the annualSalary
     */
    public void setAnnualSalary(float annualSalary)
    {
        this.annualSalary = annualSalary;
    }




    /**
     * Read access to the draftDate
     * @hibernate.property column="draft_date" type="date"
     * @return the draftDate.
     */
    public Date getDraftDate()
    {
        return this.draftDate;
    }




    /**
     * Write access to the draftDate
     * @param new value for the draftDate
     */
    public void setDraftDate(Date draftDate)
    {
        this.draftDate = draftDate;
    }




    /**
     * Read access to the firstName
     * @hibernate.property column="first_name" type="string" length="15"
     * @return the firstName.
     */
    public String getFirstName()
    {
        return this.firstName;
    }




    /**
     * Write access to the firstName
     * @param new value for the firstName
     */
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }




    /**
     * Read access to the id
     * @hibernate.id column="id" type="long" unsaved-value="any" generator-class="sequence"
     * @return the id.
     */
    public long getId()
    {
        return this.id;
    }




    /**
     * Write access to the id
     * @param new value for the id
     */
    public void setId(long id)
    {
        this.id = id;
    }




    /**
     * Read access to the jerseyNumber
     * @hibernate.property column="jersey_number" type="integer" length="1" non-null="true"
     * @return the jerseyNumber.
     */
    public int getJerseyNumber()
    {
        return this.jerseyNumber;
    }




    /**
     * Write access to the jerseyNumber
     * @param new value for the jerseyNumber
     */
    public void setJerseyNumber(int jerseyNumber)
    {
        this.jerseyNumber = jerseyNumber;
    }




    /**
     * Read access to the lastName
     * @hibernate.property column="last_name" type="string" length="15" not-null="true"
     * @return the lastName.
     */
    public String getLastName()
    {
        return this.lastName;
    }




    /**
     * Write access to the lastName
     * @param new value for the lastName
     */
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }




    /**
     * Read access to the team
     * @hibernate.many-to-one name="team" class="example.Team" column="id"
     * @return the team.
     */
    public Team getTeam()
    {
        return this.team;
    }



    /**
     * "Deep equals" for a Player
     * @param instance to compare to the target
     * @return true if the parameter is equal to the target; false otherwise
     */
    public boolean equals(Object o)
    {
        if (this == o)
            return true;

        if (!(o instanceof Player))
            return false;

        Player p = (Player)o;

        boolean sameId = (this.id == p.id);

        return sameId;
    }



    /**
     * Generate a unique hash code for a Team
     * @return unique hash code for this Team
     */
    public int hashCode()
    {
        int value   = 7;

        value       = 11*value + 13*(int)(this.id ^ (this.id >>> 32));

        return value;
    }



    /**
     * String representation of a Team
     * @return string representation of a Team
     */
    public String toString()
    {
        String value
        = OPEN_BRACKET
        + this.id
        + SEPARATOR
        + this.firstName
        + SEPARATOR
        + this.lastName
        + SEPARATOR
        + this.draftDate
        + SEPARATOR
        + this.annualSalary
        + SEPARATOR
        + this.team
        + SEPARATOR
        + this.team
        + CLOSE_BRACKET;

        return value;
    }



    /**
     * Comparable for sorting Players - sorts by id.
     * @param instance of Player to compare to the target
     * @return -1, 0, +1 if the instance id is less than, equal to,
     * or treater than the target id.
     * @throws ClassCastException if the parameter is not an instance
     * of Player
     */
    public int compareTo(Object o)
    {
        Player p = (Player)o;

        return (int)(p.id - this.id);
    }
}


When I run Ant I can see the [generate] task being called, but I don't get anything in the ${output} directory. There are no .hbm.xml files to be had anywhere, and no error messages. Ant runs to completion without complaining.

What have I done wrong? Hints are most appreciated. Thanks - MOD

_________________
MOD


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2003 9:35 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
Have you tried running Ant with the verbose option, to see that it is picking up files etc?



Sherman


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 9:10 am 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
Sherman, thanks for the suggestion. Ant has been so dependable for me that I've never had to run it with the verbose option turned on. I never thought of it.

It said that I've got a CLASPATH problem:

generate:
[echo] generating Hibernate code
dropping C:\Edu\Java\Hibernate\project.class.path from path as it doesn't exist

I'll have to dig into this. But thank you for the hint. Since everything builds properly, I never would have guessed that CLASSPATH was my problem. Thanks - MOD

_________________
MOD


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 10:34 am 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
I'm not sure why Ant is unhappy with my CLASSPATH, because it's set (the verbose option tells me so).


Any hints on what Ant means when it says:

generate:
dropping C:\Edu\Java\Hibernate\converted.class.path from path as it doesn't exist

It DOES exist, and it has the JARs for XDoclet listed. What's wrong? - MOD

_________________
MOD


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 11:13 am 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
Shouldn't:

<taskdef name="hibernate-xdoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpath="project.class.path"/>

be:

<taskdef name="hibernate-xdoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="project.class.path"/>

?

Sherman


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2003 11:55 am 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
sgwood wrote:
Shouldn't:

<taskdef name="hibernate-xdoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpath="project.class.path"/>

be:

<taskdef name="hibernate-xdoclet"
classname="xdoclet.modules.hibernate.HibernateDocletTask"
classpathref="project.class.path"/>

?

Sherman


Ack!

Of course you're right, Sherman. I didn't spot that. I did manage to "fix" it by adding a <classpath> tag, but I prefer your solution.

Thank you for being my extra eyes - and brain. ;) MOD

_________________
MOD


Top
 Profile  
 
 Post subject: Explain Hibernate MappingException: Repeated column
PostPosted: Mon Dec 22, 2003 4:14 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
Thanks to sgwood, I've managed to get hbm.xml files out of XDoclet and Ant. Now I'm having another problem.

I'm trying to get the example from "Introduction to Hibernate" by Nick Heudecker to work:

http://www.systemmobile.com/articles/In ... ce%20Files

I've added a simple main() class to example.Players so I can exercise Hibernate and see a record inserted into my database:

Code:
package example;

import java.io.Serializable;

import java.text.DateFormat;
import java.util.Date;

import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;


/**
* Sample from TSS "Intro to Hibernate" article
* @author Michael O. Duffy Wed 17-Dec-2003
* @hibernate.class table="players"
*/
public class Player implements Comparable, Serializable
{
    /** Open bracket around instance in String representation */
    public static final String OPEN_BRACKET = "{";

    /** Close bracket around instance in String representation */
    public static final String CLOSE_BRACKET = "}";

    /** Separator between fields in String representation */
    public static final String SEPARATOR = ",";

    /** Unique database id */
    private long id;

    /** Player first name */
    private String firstName;

    /** Player family name */
    private String lastName;

    /** Draft date */
    private Date draftDate;

    /** Annual salary (ought to be a money type) */
    private float annualSalary;

    /** Jersey number (why not a String for "00"?) */
    private int jerseyNumber;

    /** Parent team instance */
    private Team team;

   
   
   
    /**
     * Command line tester
     * @param command line arguments
     * <ol start="0">
     * <li>first name</li>
     * <li>last name</li>
     * <li>draft date</li>
     * <li>annual salary</li>
     * <li>jersey number</li>
     * <li>team city</li>
     * <li>team name</li>
     * </ol>
     */
    public static void main(String [] args)
    {
        try
        {
            if (args.length > 0)
            {
                // configure Hibernate session
                Configuration config = new Configuration();
               
                config.addClass(example.Player.class);             
                config.addClass(example.Team.class);
               
                SessionFactory factory = config.buildSessionFactory();
                Session session = factory.openSession();
               
                // create a new player
                Player p = new Player();
                p.setFirstName((args.length > 0) ? args[0] : "");
                p.setLastName((args.length > 1) ? args[1] : "");
                p.setDraftDate((args.length > 2) ? DateFormat.getDateInstance().parse(args[2]) : new Date());
                p.setAnnualSalary((args.length > 3) ? Float.parseFloat(args[3]) : 0.0f);
                p.setJerseyNumber((args.length > 4) ? Integer.parseInt(args[4]) : 0);
                System.out.println("player: " + p);

                session.saveOrUpdate(p);
               
                Team team = new Team();
                team.setCity((args.length > 5) ? args[5] : "Boston");
                team.setName((args.length > 6) ? args[6] : "Celtics");               
                team.addPlayer(p);
               
                System.out.println("team: " + team);
               
                session.saveOrUpdate(team);                                             
               
                session.close().close();
                factory.close();               
            }
        }
        catch (Exception e)
        {
            e.printStackTrace(System.err);
        }
    }

   
   
   
    /** Create a new Player */
    public Player() {}
   

   
   
    /**
     * Copy constructor
     * @param Player instance to copy
     */
    public Player(Player p)
    {
        this.id         = p.id;
        this.firstName  = p.firstName;
        this.lastName   = p.lastName;
        this.draftDate  = new Date(p.draftDate.getTime());
    }



    /**
     * Read access to the annualSalary
     * @hibernate.property column="annual_salary" type="float"
     * @return the annualSalary.
     */
    public float getAnnualSalary()
    {
        return this.annualSalary;
    }




    /**
     * Write access to the annualSalary
     * @param new value for the annualSalary
     */
    public void setAnnualSalary(float annualSalary)
    {
        this.annualSalary = annualSalary;
    }




    /**
     * Read access to the draftDate
     * @hibernate.property column="draft_date" type="date"
     * @return the draftDate.
     */
    public Date getDraftDate()
    {
        return this.draftDate;
    }




    /**
     * Write access to the draftDate
     * @param new value for the draftDate
     */
    public void setDraftDate(Date draftDate)
    {
        this.draftDate = draftDate;
    }




    /**
     * Read access to the firstName
     * @hibernate.property column="first_name" type="string" length="15"
     * @return the firstName.
     */
    public String getFirstName()
    {
        return this.firstName;
    }




    /**
     * Write access to the firstName
     * @param new value for the firstName
     */
    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }




    /**
     * Read access to the id
     * @hibernate.id column="id" insert="false" update="false" type="long" unsaved-value="any" generator-class="sequence"
     * @return the id.
     */
    public long getId()
    {
        return this.id;
    }




    /**
     * Write access to the id
     * @param new value for the id
     */
    public void setId(long id)
    {
        this.id = id;
    }




    /**
     * Read access to the jerseyNumber
     * @hibernate.property column="jersey_number" type="integer" length="1" non-null="true"
     * @return the jerseyNumber.
     */
    public int getJerseyNumber()
    {
        return this.jerseyNumber;
    }




    /**
     * Write access to the jerseyNumber
     * @param new value for the jerseyNumber
     */
    public void setJerseyNumber(int jerseyNumber)
    {
        this.jerseyNumber = jerseyNumber;
    }




    /**
     * Read access to the lastName
     * @hibernate.property column="last_name" type="string" length="15" not-null="true"
     * @return the lastName.
     */
    public String getLastName()
    {
        return this.lastName;
    }




    /**
     * Write access to the lastName
     * @param new value for the lastName
     */
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }




    /**
     * Read access to the team
     * @hibernate.many-to-one name="team" class="example.Team" column="id"
     * @return the team.
     */
    public Team getTeam()
    {
        return this.team;
    }

   
   
   
    /**
     * Write access to the team
     * @param new team value
     */
    public void setTeam(final Team newTeam)
    {
        this.team = new Team(newTeam);
    }

   
   
   
    /**
     * "Deep equals" for a Player
     * @param instance to compare to the target
     * @return true if the parameter is equal to the target; false otherwise
     */
    public boolean equals(Object o)
    {
        if (this == o)
            return true;

        if (!(o instanceof Player))
            return false;

        Player p = (Player)o;

        boolean sameId = (this.id == p.id);

        return sameId;
    }



    /**
     * Generate a unique hash code for a Team
     * @return unique hash code for this Team
     */
    public int hashCode()
    {
        int value   = 7;

        value       = 11*value + 13*(int)(this.id ^ (this.id >>> 32));

        return value;
    }



    /**
     * String representation of a Team
     * @return string representation of a Team
     */
    public String toString()
    {
        String value
        = OPEN_BRACKET
        + this.id
        + SEPARATOR
        + this.firstName
        + SEPARATOR
        + this.lastName
        + SEPARATOR
        + this.draftDate
        + SEPARATOR
        + this.annualSalary
        + SEPARATOR
        + this.team
        + SEPARATOR
        + this.team
        + CLOSE_BRACKET;

        return value;
    }



    /**
     * Comparable for sorting Players - sorts by id.
     * @param instance of Player to compare to the target
     * @return -1, 0, +1 if the instance id is less than, equal to,
     * or treater than the target id.
     * @throws ClassCastException if the parameter is not an instance
     * of Player
     */
    public int compareTo(Object o)
    {
        Player p = (Player)o;

        return (int)(p.id - this.id);
    }
}


Everything compiles fine, and Java finds the hibernate.properties in the CLASSPATH without trouble.

But I get this exception:

Code:
INFO: reflection optimizer disabled for: example.Player, BulkBeanException: null (property setTeam)
net.sf.hibernate.MappingException: Repeated column in mapping for class example.Player should be mapped with insert="false" update="false": id
   at net.sf.hibernate.persister.AbstractEntityPersister.checkColumnDuplication(AbstractEntityPersister.java:978)
   at net.sf.hibernate.persister.EntityPersister.<init>(EntityPersister.java:844)
   at net.sf.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:41)
   at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:136)
   at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
   at example.Player.main(Player.java:78)


I don't understand what the MappingException means. If anyone can explain I'd appreciate it. Thanks - MOD

_________________
MOD


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 4:49 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
You have a duplicate column name, as the error message indicates:


Code:
    /**
     * Read access to the id
     * @hibernate.id column="id" type="long" unsaved-value="any" generator-class="sequence"
     * @return the id.
     */
    public long getId()
    {
        return this.id;
    }

    /**
     * Read access to the team
     * @hibernate.many-to-one name="team" class="example.Team" column="id"
     * @return the team.
     */
    public Team getTeam()
    {
        return this.team;
    }


I'd change the second one to teamId


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2003 4:58 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2003 10:13 am
Posts: 33
Thank you, sgwood, I'll try it. Once again you've saved me from my own errors. - MOD

_________________
MOD


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