-->
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.  [ 7 posts ] 
Author Message
 Post subject: composite-element question
PostPosted: Thu Dec 11, 2003 2:37 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 4:07 pm
Posts: 22
The class that you specify in the class attribute of the <composite-element> tag in a many-to-many association, should it be defined in the mapping file outside of the composite-element tag?

Consider the following example:

<set name="Spaces" table="spacepersonrel" lazy="true" >
<key column="person_ID"/>
<composite-element class="net.sf.hibernate.examples.quickstart.SpaceMembership">
<property name="priority" type="string">
column name="priority" sql-type="VARCHAR2(100)"/>
</property>
<many-to-one name="spacedata" class="net.sf.hibernate.examples.quickstart.Space1" column="SpaceId"/>
</composite-element>
</set>

In the above example, should the class net.sf.hibernate.examples.quickstart.SpaceMembership be declared separately in the hibernate mapping file?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 2:39 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
no


Top
 Profile  
 
 Post subject: composite-element question
PostPosted: Thu Dec 11, 2003 2:47 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 4:07 pm
Posts: 22
If so, in the class net.sf.hibernate.examples.quickstart.SpaceMembership, when I call the setPriority("High"), I get the following exception:

java.lang.reflect.InvocationTargetException: java.lang.reflect.InvocationTargetException: java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:375)
at net.sf.hibernate.examples.quickstart.Entity.setProperty(Unknown Source)
at net.sf.hibernate.examples.quickstart.SpaceMembership.setPriority(Unknown Source)
at java.lang.reflect.Method.invoke(Native Method)
at net.sf.hibernate.util.ReflectHelper$Setter.set(ReflectHelper.java:45)
at net.sf.hibernate.type.ComponentType.setPropertyValues(ComponentType.java:225)
at net.sf.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:147)
at net.sf.hibernate.collection.CollectionPersister.readElement(CollectionPersister.java:377)
at net.sf.hibernate.collection.Set.readFrom(Set.java:242)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:175)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:602)
at net.sf.hibernate.loader.CollectionLoader.initialize(CollectionLoader.java:82)
at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2897)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:151)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:64)
at net.sf.hibernate.collection.Set.iterator(Set.java:138)
at net.sf.hibernate.examples.quickstart.Person.setSpaces(Unknown Source)
at java.lang.reflect.Method.invoke(Native Method)
at net.sf.hibernate.util.ReflectHelper$Setter.set(ReflectHelper.java:45)
at net.sf.hibernate.persister.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:170)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1961)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:196)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:602)
at net.sf.hibernate.loader.CollectionLoader.initialize(CollectionLoader.java:82)
at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2897)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:151)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:64)
at net.sf.hibernate.collection.Set.size(Set.java:114)
at net.sf.hibernate.examples.quickstart.SampleServlet.addUserToProject(Unknown Source)
at net.sf.hibernate.examples.quickstart.SampleServlet.createUsers(Unknown Source)
at net.sf.hibernate.examples.quickstart.SampleServlet.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:265)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:2546)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2260)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)

Any clue?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 3:05 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Can you show source for

net.sf.hibernate.examples.quickstart.SpaceMembership

and

net.sf.hibernate.examples.quickstart.Entity
?

If it is some well-known Hibernate example, where can I obtain it?


Top
 Profile  
 
 Post subject: composite-element question
PostPosted: Thu Dec 11, 2003 4:45 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 4:07 pm
Posts: 22
Here is the code for SpaceMembership:

package net.sf.hibernate.examples.quickstart;

import java.util.*;

public class SpaceMembership extends Entity {

public SpaceMembership() {
}

public Space1 getSpacedata()
{
return (Space1) getProperty((Object)"spacedata");

}

public void setSpacedata(Space1 spacedata)
{
if(spacedata != null)
setProperty((Object)"spacedata", (Object)spacedata);
}

public void setPriority(String priority)
{
setProperty((Object)"priority", priority);
}

public String getPriority()
{
return (String)getProperty((Object)"priority");
}


}




Here is the code for Entity:

package net.sf.hibernate.examples.quickstart;

import java.util.*;

public class Entity {

//private String id;
//private String title;
//private Date createDate;
private Hashtable ht;

public Entity() {
ht = new Hashtable();
}

public String getId() {
//return id;
return (String)getProperty((Object)"Id");
}

public void setId(String id) {
//this.id = id;
setProperty((Object)"Id", id);
}

public String getTitle() {
//return title;f
return (String)getProperty((Object)"title");
}

public void setTitle(String title) {
//this.title = title;
setProperty((Object)"title", title);
}

public Date getCreateDate() {
//return createDate;
return (Date)getProperty((Object)"createDate");
}

public void setCreateDate(Date createDate) {
//this.createDate = createDate;
setProperty((Object)"createDate", createDate);
}

public void setProperty(Object Name, String Value)
{
//System.out.println("Name : " + Name);
//System.out.println("Value : " + Value);
ht.put(Name, Value);
}

public Object getProperty(Object Name)
{
return (Object) ht.get(Name);
}

public void setProperty(Object Name, Date Value)
{
ht.put(Name, Value);
}

public Date getPropertyAsDate(String Name)
{
return (Date) getProperty(Name);
}

public String getPropertyAsString(String Name)
{
return (String) getProperty(Name);
}

public void setProperty(Object Name, Object Value)
{
ht.put(Name, Value);
}

public void setProperty(Object name, char value)
{
ht.put(name, new Character(value));
}

public char getPropertyAsChar(Object name)
{
if (ht.get(name) == null)
return ' ';
else
return ((Character) ht.get(name)).charValue();
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 8:06 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I do not think this exception is caused by setPriority("High"). According to stack trace, in the addUserToProject method you check size of Persons collection. Hibernate starts loading this collection and initializing its elements.
Each Person has Spaces collection. In the setSpaces method (which is called by Hibernate) you begin to iterate spaces (why? this supposed to be a plain setter) and this causes loading of Spaces collection. And when Hibernate initializes one of SpaceMembership classes it calls setPriority on it.
To me it looks like HashMap.put thrown NullPointerException. Have no idea why - I checked source of two JDKs and none of them should crash even if key is null. Not even saying about null value which is absolutely valid situation.

What JDK are you using?

Try adding some logging into setPriority method (or use a debugger) to see what parameters arrive there.

Btw, I'm just curious why do you need this complex and inefficient stuff with HashMaps? Why don't you implement all the properties as normal properties with private member variables?


Top
 Profile  
 
 Post subject: composite-element question
PostPosted: Thu Dec 11, 2003 10:04 pm 
Beginner
Beginner

Joined: Wed Sep 17, 2003 4:07 pm
Posts: 22
Thanks for your reply. It was my mistake that I put in the Iterator at the wrong place. Actually, the problem was due to existing records in the spacepersonrel table with null priority column. I fixed that.

Thank you.


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