-->
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.  [ 5 posts ] 
Author Message
 Post subject: I write the one-to-many code,but it tells me some error,help
PostPosted: Tue Jan 06, 2004 10:12 am 
Regular
Regular

Joined: Tue Dec 30, 2003 2:35 am
Posts: 85
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("aman@163.com");
com.one2many.Person objPerson2 = new com.one2many.Person();
objPerson2.setPersonID("def");
objPerson2.setNAME("bman");
objPerson2.setEMAIL("bman@163.com");
com.one2many.Person objPerson3 = new com.one2many.Person();
objPerson3.setPersonID("ghi");
objPerson3.setNAME("cman");
objPerson3.setEMAIL("cman@163.com");


//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)


Top
 Profile  
 
 Post subject: in the test code,table had been created
PostPosted: Tue Jan 06, 2004 10:14 am 
Regular
Regular

Joined: Tue Dec 30, 2003 2:35 am
Posts: 85
alter table DB_AUTHOR drop constraint FK3F6B206C1124BE87

alter table DB_PUBLICATION drop constraint FK97ACCFCB1124BE87

drop table DB_AUTHOR cascade constraints

drop table DB_PUBLICATION cascade constraints

drop table DB_PERSON cascade constraints

create table DB_AUTHOR (
DB_AUTHORID VARCHAR(400) not null,
DB_ALIAS VARCHAR(400),
primary key (DB_AUTHORID)
)

create table DB_PUBLICATION (
DB_PublicationID VARCHAR2(255) not null,
DB_BOOKNAME VARCHAR2(255),
DB_DATATIME VARCHAR2(255),
DB_AUTHORID VARCHAR2(41),
primary key (DB_PublicationID)
)

create table DB_PERSON (
DB_PERSONID VARCHAR(41) not null,
DB_NAME VARCHAR(400),
DB_EMAIL VARCHAR(500),
primary key (DB_PERSONID)
)

alter table DB_AUTHOR add constraint FK3F6B206C1124BE87 foreign key (DB_AUTHORID) references DB_PERSON

alter table DB_PUBLICATION add constraint FK97ACCFCB1124BE87 foreign key (DB_AUTHORID) references DB_AUTHOR

but,finally,code tells the error
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)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 12:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Replace
Code:
<param name="property">objPerson
</param>

to
Code:
<param name="property">objPerson</param>

_________________
Emmanuel


Top
 Profile  
 
 Post subject: thanks,yes,it work,but i can't understand.......
PostPosted: Tue Jan 06, 2004 8:10 pm 
Regular
Regular

Joined: Tue Dec 30, 2003 2:35 am
Posts: 85
<param name="property">objPerson
</param>
also a valid xml string,if i treat
<param name="property">objPerson
</param>
and
<param name="property">objPerson</param>
as a xml string, i think there is no different betwen them?

i can't understand!
i can't image!

thanks could you tell me the true reason!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 3:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Did you *actually* you try my advice ?
objPerson is a valid property objPerson<return> isn't

_________________
Emmanuel


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