Sorry, I meant this to be a new thread.
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