-->
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: Error in outer join: association or configuration error
PostPosted: Tue Nov 30, 2004 3:50 am 
Newbie

Joined: Wed Nov 24, 2004 8:38 am
Posts: 19
Location: India
I have two tables CAT and MICE
CAT table
(cat_id int primary key,
name varchar(16),
sex char(1),
weight double)

MICE
(mice_id int primary key,
name varchar(16),
eatenby int)

I want to perform an outer join using CAT, MICE.
Quote:
eatenby
field in MICE table is a foreign key that refernces CAT.cat_id. I don't want to use MiddleGen and XDoclet. I would like to
Quote:
create classes and hbm.xml files
corresponding to the given tables. Once this is done I would like to formulate the outer join query using CAT and MICE tables.

I am using the following code:

Cat.java

package net.sf.hibernate.examples.quickstart;

public class Cat {

private int id;
private String name;
private char sex;
private float weight;

public Cat() {
}
public int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}

}

Mice.java

package net.sf.hibernate.examples.quickstart;

public class Mice {

private int id;
private String name;
private Cat cat;

public Mice() {
}
public int getId() {
return id;
}
private void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}

}

Mice.hbm.xml

<hibernate-mapping>

<class name="net.sf.hibernate.examples.quickstart.Mice" table="MICE">

<id name="id" type="integer" unsaved-value="null" >
<column name="MICE_ID" sql-type="int" not-null="true"/>
<generator class="increment"/>
</id>

<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>

<many-to-one name="cat" class="net.sf.hibernate.examples.quickstart.Cat" column="cat_id"/>

</class>

</hibernate-mapping>

Cat.hbm.xml


<hibernate-mapping>

<class name="net.sf.hibernate.examples.quickstart.Cat" table="CAT">

<id name="id" type="integer" unsaved-value="null" >
<column name="CAT_ID" sql-type="int" not-null="true"/>
<generator class="increment"/>
</id>

<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>

<property name="sex"/>

<property name="weight"/>

</class>

</hibernate-mapping>

After this in a file i am trying to call this query:

Query q1 = sess.createQuery(
"select cat.name " +
"from Cat as cat, Mice as mice " +
"outer join mice.cat as cat"
);

The code is comiling successfully. When i run the main file it gives
Quote:
Unable to execute query: net.sf.hibernate.JDBCException
and then the stack trace.[/quote]

I am unable to solve this problem. Plz suggest code changes required.....

Thanks......


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 4:59 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
Why didn't you add the fllowing code snippet in the Cat.hbm.xml and Cat.java?

public class Cat {

private HashSet mices=new HashSet();

....

public Set getMices(){
return mices;
}

public void setMices(Set mices){
this.mices=mices;
}

....
}


Cat.hbm.xml

<hibernate-mapping>
<class name="net.sf.hibernate.examples.quickstart.Cat" table="CAT">
<id name="id" type="integer" unsaved-value="null" >
<column name="CAT_ID" sql-type="int" not-null="true"/>
<generator class="increment"/>
</id>
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
<set name="mices" inverse="true" lazy="true" cascade="all" outer-join="true">
<key column="eatenby"/>
<one-to-many class="net.sf.hibernate.examples.quickstart.Mice"/>
</set>


</class>

your hql code shoud be:

select cat from Cat cat

I don't known if i really understand your meaning.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 5:11 am 
Newbie

Joined: Wed Nov 24, 2004 8:38 am
Posts: 19
Location: India
bcoz i need a join on CAT and MICE tables....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 5:20 am 
Newbie

Joined: Wed Nov 24, 2004 8:38 am
Posts: 19
Location: India
amjn.

don't follow the design of my classes. i am not sure if i am in the right direction...

i need to execute a outer join on CAT and MICE table and eatenby is a foreign key, as shown in the table layout.

could u suggest chnages now.... why do we need Set class????


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 9:33 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
Hi gs_1481:

In fact,it is standard one-to-many relationship between Cat table and Mice table.So you must represent the bidirectional relationship in Cat.hbm.xml and Mice.hbm.xml at first,separately initialize a Set object containing many Mice objects in the Cat class and a cat object reference in the Mice class.

I strongly suggest you look through the hibernate reference or hibernate in action before go about write code with hibernate.I am beginner with hibernate too,and only known a little about hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 10:04 am 
Newbie

Joined: Wed Nov 24, 2004 8:38 am
Posts: 19
Location: India
Thanks...
I have solved the problem.... i got the lead from ur first reply on Set interface... basically i was not able to get the point that CAT maps MICE as 1 : m relationship...
for that we need to have a collection of mice in Cat.java and a refence to cat in Mice.java... this is the way i visualize it....

Then on i formulated the outer join query and was able to execute it...

Thanks frnd... if possible lets meet tomorrow on yahoo chat


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 01, 2004 2:44 am 
Newbie

Joined: Wed Nov 24, 2004 8:38 am
Posts: 19
Location: India
I am facing a new problem now.

My joins are working fine, i am able to insert data in the CAT table but i am unable to insert data in the MICE table.

Thanks....


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.