-->
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 in Hibernate many-to-one Foreign key relation
PostPosted: Wed Mar 12, 2008 11:30 am 
Newbie

Joined: Wed Mar 12, 2008 11:16 am
Posts: 15
Hi to all

I am trying Hibernate many-to-one association for foreign key relation for two tables.but i am getting the following error

org.hibernate.MappingException: An association from the table contact_bh refers to an unmapped class: Contact_rvBean

can any one solve this

My code--
First table: contact_rv

PID NOT NULL NUMBER(38)
PNAME VARCHAR2(50)


Second table: contact_bh

EMPID NOT NULL NUMBER(38)
PID NUMBER(38)


I want to have foreign key relation for pid in contact_bh to pid in contact_rv



My mapping files are--
For contact_rv
-----------------------------------------------
<?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>
<class name="com.mss.Contact_rvBean" table="contact_rv">
<id name="pid" type="int" column="pid">
<generator class="increment"/>
</id>

<property name="pname" column="pname" />
</class>
</hibernate-mapping>

For contact_bh

<?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>
<class name="com.mss.Contact_bhBean" table="contact_bh">
<id name="empid" type="int" column="empid">
<generator class="increment"/>
</id>

<many-to-one name="pid" class="Contact_rvBean" column="pid"/>
</class>
</hibernate-mapping>


beans are
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.mss;

/**
*
* @author miracle
*/
public class Contact_bhBean {
private int empid;
private int pid;
private Contact_rvBean rvbean;

public int getEmpid() {
return empid;
}

public void setEmpid(int empid) {
this.empid = empid;
}

public int getPid() {
return pid;
}

public void setPid(int pid) {
this.pid = pid;
}

public Contact_rvBean getRvbean() {
return rvbean;
}

public void setRvbean(Contact_rvBean rvbean) {
this.rvbean = rvbean;
}

}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.mss;

/**
*
* @author miracle
*/
public class Contact_rvBean {

private int pid;
private String pname;

public

int getPid() {
return pid;
}

public void setPid(int pid) {
this.pid = pid;
}

public String getPname() {
return pname;
}

public void setPname(String pname) {
this.pname = pname;
}
}

Can any one PLz PLz PLz PLzPLzPLzPLz PLzPLz................Help me to come out of this problem

or

Please give complete code for one mapping example.
thanks in advance

raghu


Top
 Profile  
 
 Post subject: Re: Problem in Hibernate many-to-one Foreign key relation
PostPosted: Wed Mar 12, 2008 2:38 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Code:
<many-to-one name="pid" class="Contact_rvBean" column="pid"/>




pid here should be a com.mss.Contact_rvBean not an int. Second I believe you need to specify the full class name not only the class name. so the following should be better:


Code:
<many-to-one name="rvBean" class="com.mss.Contact_rvBean" column="pid"/>


and for the com.mss.Contact_bhBean class:

Code:
class Contact_bhBean {
....
Contact_rvBean rvBean;

...
public void setRvBean(..) {...}
public Contact_rvBean getRvBean() {..}

}




Farzad-


Top
 Profile  
 
 Post subject: Thanx for that but still i m facing trouble
PostPosted: Thu Mar 13, 2008 4:32 am 
Newbie

Joined: Wed Mar 12, 2008 11:16 am
Posts: 15
Hi

Zayad

Thanks for ur corrections i tried wat u have said.so i am not getting any error,but when i insert pid.i am not able to see the inserted value in table contact_bh.but the primary key is incremented for every transaction.

it looks like this
Table:contact_bh

EMPID PID
1
3
5
6
7
2
4

7 rows selected.

the code i have given is--
hbm file for contact_bh----------------------------------------

<?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>
<class name="com.mss.Contact_bhBean" table="contact_bh">
<id name="empid" type="int" column="empid">
<generator class="increment"/>
</id>

<many-to-one name="rvbean" class="com.mss.Contact_rvBean" column="pid" />
</class>
</hibernate-mapping>

------------------------------------------------------------------------------
hbm file for contact_rv is
-------------
<?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>
<class name="com.mss.Contact_rvBean" table="contact_rv">
<id name="pid" type="int" column="pid">
<generator class="increment"/>
</id>
<property name="pname" column="pname" />
</class>
</hibernate-mapping>
-----------------------------------------------------------------------------------

bean for contac_bh
-----------_________________________
package com.mss;

/**
*
* @author miracle
*/
public class Contact_bhBean {
private int empid;
private int pid;
private Contact_rvBean rvbean;

public int getEmpid() {
return empid;
}

public void setEmpid(int empid) {
this.empid = empid;
}

public int getPid() {
return pid;
}

public void setPid(int pid) {
this.pid = pid;
}

public Contact_rvBean getRvbean() {
return rvbean;
}

public void setRvbean(Contact_rvBean rvbean) {
this.rvbean = rvbean;
}

}
_____________________________
bean for contact_rv

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.mss;

/**
*
* @author miracle
*/
public class Contact_rvBean {

private int pid;
private String pname;

public

int getPid() {
return pid;
}

public void setPid(int pid) {
this.pid = pid;
}

public String getPname() {
return pname;
}

public void setPname(String pname) {
this.pname = pname;
}
}
_______________________________________________


DynaValidatorForm dForm=(DynaValidatorForm)form;
SessionFactory sFactory = new Configuration().configure("com/mss/hibernate.cfg.xml").buildSessionFactory();
Session hsession = sFactory.openSession();
Transaction trans = hsession.beginTransaction();

String patientid=dForm.getString("pid").toString();

int pid=Integer.parseInt(patientid);
Contact_bhBean cbean=new Contact_bhBean();
System.out.println("********************"+patientid);
cbean.setPid(pid);

hsession.save(cbean);
System.out.println("********************"+patientid);
trans.commit();


Can you PLZ PLZ PLZ PLZ tell me where i went wrong

Thanks in advance

Raghu Varma Bhupathiraju


Top
 Profile  
 
 Post subject: Thanx for that but still i m facing trouble
PostPosted: Thu Mar 13, 2008 4:32 am 
Newbie

Joined: Wed Mar 12, 2008 11:16 am
Posts: 15
Hi

farzad

Thanks for ur corrections i tried wat u have said.so i am not getting any error,but when i insert pid.i am not able to see the inserted value in table contact_bh.but the primary key is incremented for every transaction.

it looks like this
Table:contact_bh

EMPID PID
1
3
5
6
7
2
4

7 rows selected.

the code i have given is--
hbm file for contact_bh----------------------------------------

<?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>
<class name="com.mss.Contact_bhBean" table="contact_bh">
<id name="empid" type="int" column="empid">
<generator class="increment"/>
</id>

<many-to-one name="rvbean" class="com.mss.Contact_rvBean" column="pid" />
</class>
</hibernate-mapping>

------------------------------------------------------------------------------
hbm file for contact_rv is
-------------
<?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>
<class name="com.mss.Contact_rvBean" table="contact_rv">
<id name="pid" type="int" column="pid">
<generator class="increment"/>
</id>
<property name="pname" column="pname" />
</class>
</hibernate-mapping>
-----------------------------------------------------------------------------------

bean for contac_bh
-----------_________________________
package com.mss;

/**
*
* @author miracle
*/
public class Contact_bhBean {
private int empid;
private int pid;
private Contact_rvBean rvbean;

public int getEmpid() {
return empid;
}

public void setEmpid(int empid) {
this.empid = empid;
}

public int getPid() {
return pid;
}

public void setPid(int pid) {
this.pid = pid;
}

public Contact_rvBean getRvbean() {
return rvbean;
}

public void setRvbean(Contact_rvBean rvbean) {
this.rvbean = rvbean;
}

}
_____________________________
bean for contact_rv

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.mss;

/**
*
* @author miracle
*/
public class Contact_rvBean {

private int pid;
private String pname;

public

int getPid() {
return pid;
}

public void setPid(int pid) {
this.pid = pid;
}

public String getPname() {
return pname;
}

public void setPname(String pname) {
this.pname = pname;
}
}
_______________________________________________


DynaValidatorForm dForm=(DynaValidatorForm)form;
SessionFactory sFactory = new Configuration().configure("com/mss/hibernate.cfg.xml").buildSessionFactory();
Session hsession = sFactory.openSession();
Transaction trans = hsession.beginTransaction();

String patientid=dForm.getString("pid").toString();

int pid=Integer.parseInt(patientid);
Contact_bhBean cbean=new Contact_bhBean();
System.out.println("********************"+patientid);
cbean.setPid(pid);

hsession.save(cbean);
System.out.println("********************"+patientid);
trans.commit();


Can you PLZ PLZ PLZ PLZ tell me where i went wrong

Thanks in advance

Raghu Varma Bhupathiraju


Top
 Profile  
 
 Post subject: Re: Thanx for that but still i m facing trouble
PostPosted: Thu Mar 13, 2008 10:56 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
raghu_bh wrote:
Can you PLZ PLZ PLZ PLZ tell me where i went wrong


Yes we can :) There is your problem:

Code:
cbean.setPid(pid);



Hibernate is not looking at this field. You need to set the object. So your example will be corrected like this:


Code:
int pid=Integer.parseInt(patientid);
Contact_bhBean cbean=new Contact_bhBean();
System.out.println("********************"+patientid);

Contact_rvBean rv = (
Contact_rvBean) hsession.find(Contact_rvBean.class, pid);

cbean.setRvbean(rv);

hsession.save(cbean);



and you should be happy.

Farzad-


Top
 Profile  
 
 Post subject: i am getting error find not defined for session
PostPosted: Tue May 27, 2008 3:02 am 
Newbie

Joined: Wed Mar 12, 2008 11:16 am
Posts: 15
i am getting error find not defined for session


Top
 Profile  
 
 Post subject: Re: i am getting error find not defined for session
PostPosted: Tue May 27, 2008 6:41 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
raghu_bh wrote:
i am getting error find not defined for session


You are most likely importing the wrong session class.



Farzad-


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.