1.in the project.
(author.java-->author.hbm.xml)
(person.java-->person.hbm.xml)
(publications.java-->publications.hbml.xml)
and test code
code example:
★★★★★★★★★★★★★★★★★★★★author.java
package com.one2many;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Author {
public Author() {
}
private java.lang.String AUTHORID;
public void setAUTHORID(java.lang.String AUTHORID)
{
this.AUTHORID = AUTHORID;
}
public java.lang.String getAUTHORID()
{
return this.AUTHORID;
}
private java.lang.String ALIAS = null;
public void setALIAS(java.lang.String ALIAS)
{
this.ALIAS = ALIAS;
}
public java.lang.String getALIAS()
{
return this.ALIAS;
}
private com.one2many.Person objPerson = null;
public void setobjPerson(com.one2many.Person objPerson)
{
this.objPerson = objPerson;
}
public com.one2many.Person getobjPerson()
{
return this.objPerson;
}
private java.util.Set publications = new java.util.HashSet();
public void setpublications(java.util.Set publications)
{
this.publications = publications;
}
public java.util.Set getpublications()
{
return this.publications;
}
}
★★★★★★★★★★★★★★★★★★★★author.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.one2many.Author" table="DB_AUTHOR">
<id name="AUTHORID" type="java.lang.String">
<column name="DB_AUTHORID" sql-type="VARCHAR(400)" not-null="true"/>
<generator class="foreign">
<param name="property">objPerson
</param>
</generator>
</id>
<property name="ALIAS" type="java.lang.String" update="true" insert="true">
<column name="DB_ALIAS" sql-type="VARCHAR(400)" length="400"
not-null="false"/>
</property>
<one-to-one name="objPerson" class="com.one2many.Person" cascade="all" constrained="true"/>
<set name="publications" lazy="true" inverse="true" cascade="all">
<key column="DB_AUTHORID"/>
<one-to-many class="com.one2many.Publication"/>
</set>
</class>
</hibernate-mapping>
★★★★★★★★★★★★★★★★★★★★person.java
package com.one2many;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Person {
public Person() {
}
private String PersonID;
public void setPersonID(java.lang.String PersonID)
{
this.PersonID = PersonID;
}
public java.lang.String getPersonID()
{
return this.PersonID;
}
private java.lang.String NAME = null;
public void setNAME(java.lang.String NAME)
{
this.NAME = NAME;
}
public java.lang.String getNAME()
{
return this.NAME;
}
private java.lang.String EMAIL = null;
public void setEMAIL(java.lang.String EMAIL)
{
this.EMAIL = EMAIL;
}
public java.lang.String getEMAIL()
{
return this.EMAIL;
}
}
★★★★★★★★★★★★★★★★★★★★person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.one2many.Person" table="DB_PERSON">
<id name="PersonID" type="java.lang.String" unsaved-value="any">
<column name="DB_PERSONID" sql-type="VARCHAR(41)" not-null="true"
length="41"/>
<generator class="assigned"/>
</id>
<property name="NAME" type="java.lang.String" update="true" insert="true">
<column name="DB_NAME" sql-type="VARCHAR(400)" length="400"
not-null="false"/>
</property>
<property name="EMAIL" type="java.lang.String" update="true" insert="true">
<column name="DB_EMAIL" sql-type="VARCHAR(500)" length="400"
not-null="false"/>
</property>
</class>
</hibernate-mapping>
★★★★★★★★★★★★★★★★★★★★publications.java
package com.one2many;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Publication {
public Publication() {
}
private String id = null;
public void setid(String id)
{
this.id = id;
}
public String getid()
{
return this.id;
}
private String bookName = null;
public void setbookName(String bookName)
{
this.bookName = bookName;
}
public String getbookName()
{
return this.bookName;
}
private String dataTime = null;
public void setdataTime(String dataTime)
{
this.dataTime = dataTime;
}
public String getdataTime()
{
return this.dataTime;
}
private String authorId = null;
public void setauthorId(String authorId)
{
this.authorId = authorId;
}
public String getauthorId()
{
return this.authorId;
}
//
private com.one2many.Author objAuthor = null;
public void setobjAuthor(com.one2many.Author objAuthor)
{
this.objAuthor = objAuthor;
}
public com.one2many.Author getobjAuthor()
{
return this.objAuthor;
}
}
★★★★★★★★★★★★★★★★★★★★publications.hbml.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.one2many.Publication" table="DB_PUBLICATION">
<id name="id" column="DB_PublicationID">
<generator class="uuid.hex"/>
</id>
<property name="bookName" column="DB_BOOKNAME"/>
<property name="dataTime" column="DB_DATATIME"/>
<many-to-one name="objAuthor" column="DB_AUTHORID"/>
</class>
</hibernate-mapping>
★★★★★★★★★★★★★★★★★★★★the test code
package com.one2many;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import net.sf.hibernate.MappingException;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class hibernateclienttest {
public hibernateclienttest() {
}
private static net.sf.hibernate.Transaction tx;
private static net.sf.hibernate.Session objSession;
public static void main(String [] agrs)
throws Exception
{
try
{
Configuration objConfig = new Configuration();
SessionFactory objSessionFactory = objConfig.configure().buildSessionFactory();
SchemaExport objSchema = new SchemaExport(objConfig);
objSchema.setOutputFile("SaveThePeople.SQL");
objSchema.create(true,true);
//
net.sf.hibernate.Session objSession = objSessionFactory.openSession();
tx = objSession.beginTransaction();
com.one2many.Person objPerson1 = new com.one2many.Person();
objPerson1.setPersonID("abc");
objPerson1.setNAME("aman");
objPerson1.setEMAIL("
[email protected]");
com.one2many.Person objPerson2 = new com.one2many.Person();
objPerson2.setPersonID("def");
objPerson2.setNAME("bman");
objPerson2.setEMAIL("
[email protected]");
com.one2many.Person objPerson3 = new com.one2many.Person();
objPerson3.setPersonID("ghi");
objPerson3.setNAME("cman");
objPerson3.setEMAIL("
[email protected]");
//objSession.save(objPerson1);
//objSession.save(objPerson2);
//objSession.save(objPerson3);
com.one2many.Publication objPublication1 = new com.one2many.Publication();
objPublication1.setbookName("english books");
objPublication1.setdataTime("20031010");
com.one2many.Publication objPublication2 = new com.one2many.Publication();
objPublication2.setbookName("japa books");
objPublication2.setdataTime("19991011");
com.one2many.Publication objPublication3 = new com.one2many.Publication();
objPublication3.setbookName("java books");
objPublication3.setdataTime("20040107");
com.one2many.Author objAuthor1 = new com.one2many.Author();
objAuthor1.setALIAS("name of aman");
objAuthor1.setobjPerson(objPerson1);
objAuthor1.getpublications().add(objPublication1);
objAuthor1.getpublications().add(objPublication2);
objAuthor1.getpublications().add(objPublication3);
objPublication1.setobjAuthor(objAuthor1);
objPublication2.setobjAuthor(objAuthor1);
objPublication3.setobjAuthor(objAuthor1);
objSession.save(objAuthor1);
tx.commit();
return;
}
catch(Exception e)
{
e.printStackTrace();
tx.rollback();
}
finally
{
if(objSession!=null)
objSession.close();
}
}
}
the error tells:
net.sf.hibernate.HibernateException: unmapped property: objPerson
at net.sf.hibernate.persister.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:960)
at net.sf.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:34)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:727)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:717)
at com.one2many.hibernateclienttest.main(hibernateclienttest.java:85)