Hibernate version:2.1.6
Mapping documents: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping> <class name="de.some.db.hibtest.data.Person" table="persons"> <id name="pid" column="pid" type="long" unsaved-value="null"> <generator class="hilo"/> </id> <property name="m_strName" column="name" type="string" length="30" not-null="true"/> <set name="m_Invoices" cascade="all" inverse="true" lazy="false"> <key column="id"/> <one-to-many class="de.audi.db.hibtest.data.Invoice"/> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping> <class name="de.audi.db.hibtest.data.Invoice" table="invoices"> <id name="id" column="id" type="long" unsaved-value="null"> <generator class="hilo"/> </id> <property name="m_nInvoiceNumber" column="invNo" type="int" not-null="true"/> <property name="m_strArticle" column="article" type="string" length="50" not-null="true"/> <property name="m_fPrice" column="price" type="float" not-null="true"/> <many-to-one name="m_Customer" class="de.audi.db.hibtest.data.Person" column="pid"/> </class> </hibernate-mapping>
Java data classes public class Person { private Long pid; private String m_strName; private Set m_Invoices = new HashSet();
public Person() { }
public Person(String strName) { this.m_strName = strName; } public String toString() { return "\nName: " + m_strName + "\nPId: " + pid + "\n"; }
/** * @return */ public Set getM_Invoices() { return m_Invoices; }
/** * @return */ public String getM_strName() { return m_strName; }
/** * @return */ public Long getPid() { return pid; }
/** * @param set */ public void setM_Invoices(Set set) { m_Invoices = set; }
/** * @param string */ public void setM_strName(String string) { m_strName = string; }
/** * @param long1 */ public void setPid(Long long1) { pid = long1; } public void addInvoice(Invoice inv) { this.m_Invoices.add(inv); } }
public class Invoice { private int m_nInvoiceNumber; private String m_strArticle; private float m_fPrice; private Person m_Customer;
private Long id;
public Invoice() { }
public Invoice(String strArticle, float fPrice, Person customer) { this(-1, strArticle, fPrice, customer); } public Invoice(int nINumber, String strArticle, float fPrice, Person customer) { this.m_nInvoiceNumber = nINumber; this.m_strArticle = strArticle; this.m_fPrice = fPrice; this.m_Customer = customer; }
public String toString() { return "\nInvoiceNumber: " + m_nInvoiceNumber + "\nArticle: " + m_strArticle + "\nPrice: " + m_fPrice + "\nPerson: "+ m_Customer.getM_strName() + " (" + m_Customer.getPid() + ")\n"; }
/** * @return */ public Person getM_Customer() { return m_Customer; }
/** * @return */ public float getM_fPrice() { return m_fPrice; }
/** * @return */ public int getM_nInvoiceNumber() { return m_nInvoiceNumber; }
/** * @return */ public String getM_strArticle() { return m_strArticle; }
/** * @param person */ public void setM_Customer(Person person) { m_Customer = person; }
/** * @param f */ public void setM_fPrice(float f) { m_fPrice = f; }
/** * @param i */ public void setM_nInvoiceNumber(int i) { m_nInvoiceNumber = i; }
/** * @param string */ public void setM_strArticle(String string) { m_strArticle = string; }
/** * @return */ public Long getId() { return id; }
/** * @param long1 */ public void setId(Long long1) { id = long1; }
}
Name and version of the database you are using:hsqldb
The generated SQL (show_sql=true): Hibernate: select person0_.pid as pid, person0_.name as name from persons person0_ where (person0_.name like '%Gutz%' ) Hibernate: select m_invoices0_.id as id__, m_invoices0_.id as id1_, m_invoices0_.invNo as invNo1_, m_invoices0_.article as article1_, m_invoices0_.price as price1_, m_invoices0_.pid as pid1_, person1_.pid as pid0_, person1_.name as name0_ from invoices m_invoices0_ left outer join persons person1_ on m_invoices0_.pid=person1_.pid where m_invoices0_.id=? Hibernate: select m_invoices0_.id as id__, m_invoices0_.id as id1_, m_invoices0_.invNo as invNo1_, m_invoices0_.article as article1_, m_invoices0_.price as price1_, m_invoices0_.pid as pid1_, person1_.pid as pid0_, person1_.name as name0_ from invoices m_invoices0_ left outer join persons person1_ on m_invoices0_.pid=person1_.pid where m_invoices0_.id=? Hibernate: select m_invoices0_.id as id__, m_invoices0_.id as id1_, m_invoices0_.invNo as invNo1_, m_invoices0_.article as article1_, m_invoices0_.price as price1_, m_invoices0_.pid as pid1_, person1_.pid as pid0_, person1_.name as name0_ from invoices m_invoices0_ left outer join persons person1_ on m_invoices0_.pid=person1_.pid where m_invoices0_.id=? Hibernate: select m_invoices0_.id as id__, m_invoices0_.id as id1_, m_invoices0_.invNo as invNo1_, m_invoices0_.article as article1_, m_invoices0_.price as price1_, m_invoices0_.pid as pid1_, person1_.pid as pid0_, person1_.name as name0_ from invoices m_invoices0_ left outer join persons person1_ on m_invoices0_.pid=person1_.pid where m_invoices0_.id=?
Hello out there,
I'm having trouble. I have a Person and an Invoice class, with a one-to-many association between Person and Invoice. Inserting data makes no problem, using sql I found out that the invoices belong to the right person. But when I use the find(String) method to search for a person by its name, I get the right persons, but their Set is empty, the invoices are not retrieved. Did I do the mapping wrong? Can anyone help me? I'm stuck on this for hours :-(
Thanks in advance,
Jan
|