-->
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: problem accessing parent's collection
PostPosted: Wed Dec 13, 2006 12:05 pm 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
Hi there. I an newbie and i need some help.
Concerning the Cat-DomesticCat example of the reference, i face this problem:
Uppon trying to insert a new cat with a mother, i get the following error:
"could not initialize a collection: [hibernate.Cat.kittens#1]"

the error is generated when i try to add the new cat into the mother's kitten collection, by this line:
mother.getKittens().add(c);

This is stuff i've used quite a few times before and i don't understand the origin of the problem.

The overall code i use for the insertion is pretty much the usual:

> motherid=new Long(Long.parseLong(request.getParameter("motherid")));
> mother=(Cat)ses.load(Cat.class,motherid);
> c.setMother(mother);
> mother.getKittens().add(c);
> c.setLitterId(mother.getKittens().size());

The mapping is the same as inside the reference, but i really don't think there is something to it.
Has anyone seen sth similar before? Please ask for any other details if the are required.
thnx


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 13, 2006 12:57 pm 
Beginner
Beginner

Joined: Wed Sep 13, 2006 7:24 am
Posts: 26
Are you using POJOs ?


I have seen this before, but it tends to happen when the Saving isn't done in the right order, or the Set,List,Bag has not been created in your POJO.

_________________
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 5:28 am 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
yes i am. without a constructor though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 6:29 am 
Beginner
Beginner

Joined: Wed Sep 13, 2006 7:24 am
Posts: 26
gtriant wrote:
yes i am. without a constructor though.


at a guess this might be the issue.

i looked at all my classes and all the contstructors create a new ArrayList, or new Map(), etc for all properties that are Bags, List or Map.

Maybe try that, and see if its happy.

_________________
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 8:09 am 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
I had already tried using constructor. It makes no difference.
Anyway i initialize the list at my class.
Any other ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 8:31 am 
Beginner
Beginner

Joined: Wed Sep 13, 2006 7:24 am
Posts: 26
gtriant wrote:
I had already tried using constructor. It makes no difference.
Anyway i initialize the list at my class.
Any other ideas?


the "getKitten" function will look to pull what data you have about the kittens.

check to see that you are loading this information up as well (Lazy Setting in mapping file ????).

Also are you using the Hibernate Session and not the StatelessSession, as i'm sure StatelessSession do not load List,Set, etc

_________________
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 8:12 am 
Newbie

Joined: Wed Nov 15, 2006 4:47 am
Posts: 11
I include all the involving code. if anyone can see a problem, please help out.

Java class:

package hibernate;

import java.util.Set;
import java.util.HashSet;
import java.util.Date;

public class Cat {
private Long id; // identifier
private Date birthdate;
private char sex;
private float weight;
private int litterId;
private Cat mother;
private Set kittens = new HashSet();

Cat(){}

private void setId(Long id) {
this.id=id;
}
public Long getId() {
return id;
}
public void setBirthdate(Date date) {
birthdate = date;
}
public Date getBirthdate() {
return birthdate;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getWeight() {
return weight;
}
public void setSex(char sex) {
this.sex=sex;
}
public char getSex() {
return sex;
}
public void setLitterId(int id) {
this.litterId = id;
}
public int getLitterId() {
return litterId;
}
public void setMother(Cat mother) {
this.mother = mother;
}
public Cat getMother() {
return mother;
}
public void setKittens(Set kittens) {
this.kittens = kittens;
}
public Set getKittens() {
return kittens;
}
}

###############################################

Mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate">

<class name="hibernate.Cat" table="cats" discriminator-value="C">
<id name="id">
<generator class="sequence">
<param name="sequence">cat_seq</param>
</generator>
</id>

<discriminator column="subclass" type="character"/>

<property name="weight"/>
<property name="birthdate" type="date" not-null="true" update="false"/>
<!-- <property name="color" type="eg.types.ColorUserType" not-null="true" update="false"/>-->
<property name="sex" not-null="true" update="false"/>
<property name="litterId" column="litterId" update="false"/>

<many-to-one name="mother" column="mother_id" update="false"/>
<set name="kittens" inverse="true" order-by="litter_id">
<key column="mother_id"/>
<one-to-many class="hibernate.Cat"/>
</set>

<subclass name="DomesticCat" discriminator-value="D">
<property name="name" type="string"/>
</subclass>
</class>

</hibernate-mapping>

#################################################
java code for insertion:

try{
String birthdate=request.getParameter("birthdate");
char sex=request.getParameter("sex").charAt(0);
float weight=Float.parseFloat(request.getParameter("weight"));;
Long motherid;
Date bd=null;
try{
SimpleDateFormat sdf=new SimpleDateFormat("dd/mm/yyyy");
sdf.setLenient(true);
bd=sdf.parse(birthdate);
}
catch (ParseException e){
System.out.println(e.getMessage());
}
catch (IllegalArgumentException e) {
System.out.println("Invalid date");
}
Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
ses.beginTransaction();
Cat mother=null;
if(request.getParameter("subclass").equals("D")){
System.out.println("subclass d");
DomesticCat c=new DomesticCat();
c.setName(request.getParameter("name"));
c.setBirthdate(bd);
c.setSex(sex);
c.setWeight(weight);
if(!(request.getParameter("motherid")==null)&&!(request.getParameter("motherid").equals(""))){
motherid=new Long(Long.parseLong(request.getParameter("motherid")));
mother=(Cat)ses.load(Cat.class,motherid);
c.setMother(mother);
System.out.println("Trying to insert a cat with the mother: "+motherid);
mother.getKittens().add(c);
c.setLitterId(mother.getKittens().size());
}
else{
c.setLitterId(0);
}
ses.save(c);
}
else if(request.getParameter("subclass").equals("C")){
System.out.println("subclass c");
Cat c=new Cat();
c.setBirthdate(bd);
c.setSex(sex);
c.setWeight(weight);
if(!(request.getParameter("motherid")==null)&&!(request.getParameter("motherid").equals(""))){
motherid=new Long(Long.parseLong(request.getParameter("motherid")));
mother=(Cat)ses.load(Cat.class,motherid);
c.setMother(mother);
mother.getKittens().add(c);
c.setLitterId(mother.getKittens().size());
}
else{
c.setLitterId(0);
}
ses.save(c);
}
ses.getTransaction().commit();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}


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.