-->
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.  [ 1 post ] 
Author Message
 Post subject: Foreign Key MappingException
PostPosted: Thu Apr 12, 2007 6:37 am 
Newbie

Joined: Thu Apr 12, 2007 6:07 am
Posts: 1
Hi there,

I am new to hibernate.
Could anybody please tell me why I am getting this exception while compiling my stand alone java class? Please read the code provided.

I am using hibernate 2.1, MySql 4.1 and java 1.4 on NetBeans 5.5

*************Exception*********************************
net.sf.hibernate.MappingException: An association from the table tblInputData refers to an unmapped class: HibernateOperations.fleas
at net.sf.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:696)
at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:680)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:798)
at DatabaseOperations.PerformSearch.primarySearch(PerformSearch.java:73)
at DatabaseOperations.PerformSearch.main(PerformSearch.java:105)

*************End of Exception*********************************

*************MySql table definitions*********************************

create table tblFleas
(
pk_FleaID int unsigned not null auto_increment primary key,
Name varchar(255),
SurName varchar(255),
);

create table tblInputData
(
pk_InputDataID int unsigned not null auto_increment primary key,
fk_ReferenceID int,
fk_FleaID int,
fk_CountyID int,
fk_HostID int
);


ALTER TABLE tblInputData ADD FOREIGN KEY (fk_FleaID) REFERENCES tblFleas (pk_FleaID);


*************hibernate 2.1 mapping files*********************************

1. inputdata.hbm.xml
-------------------------
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="HibernateOperations.inputdata" table="tblInputData">
<id name="pk_InputDataID" type="int" column="pk_InputDataID">

<generator class="assigned"/> </id>
<property name="fk_ReferenceID" type="int"/>
<property name="fk_FleaID" type="int"/>
<!--many-to-one name="fleaER" class="fleas" column="fk_FleaID"/-->
<property name="fk_CountyID" type="int"/>
<property name="fk_HostID" type="int"/>

<many-to-one name="fleasER" class="HibernateOperations.fleas" column="fk_FleaID"
foreign-key=""/>

</class>
</hibernate-mapping>

2. fleas.hbm.xml
-------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="HibernateOperations.fleas" table="tblFleas">
<id name="pk_FleaID" type="int" column="pk_FleaID">

<generator class="assigned"/> </id>
<property name="name" type="string"/>
<property name="surname" type="string"/>
<!--
<set name="fleasER" cascade="all" inverse="true" lazy="true">
<key column="pk_FleaID"/>

<one-to-many class="HibernateOperations.inputdata"/>
</set>
-->

</class>
</hibernate-mapping>

*************java mapping files*********************************

1. inputdata.java

package HibernateOperations;

import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class inputdata implements Serializable {

/** identifier field */
private Integer pk_InputDataID;

/** nullable persistent field */
private Integer fk_ReferenceID;

/** nullable persistent field */
private Integer fk_FleaID;

/** nullable persistent field */
private Integer fk_CountyID;

/** nullable persistent field */
private Integer fk_HostID;

/** persistent field */
private HibernateOperations.fleas fleasER;

/** full constructor */
public inputdata(Integer pk_InputDataID, Integer fk_ReferenceID, Integer fk_FleaID,
Integer fk_CountyID, Integer fk_HostID, HibernateOperations.fleas fleasER) {
this.pk_InputDataID = pk_InputDataID;
this.fk_ReferenceID = fk_ReferenceID;
this.fk_FleaID = fk_FleaID;
this.fk_CountyID = fk_CountyID;
this.fk_HostID = fk_HostID;
this.fleasER = fleasER;
}

/** default constructor */
public inputdata() {
}

/** minimal constructor */
public inputdata(Integer pk_InputDataID, HibernateOperations.fleas fleasER) {
this.pk_InputDataID = pk_InputDataID;
this.fleasER = fleasER;
}

public Integer getPk_InputDataID() {
return this.pk_InputDataID;
}

public void setPk_InputDataID(Integer pk_InputDataID) {
this.pk_InputDataID = pk_InputDataID;
}

public Integer getFk_ReferenceID() {
return this.fk_ReferenceID;
}

public void setFk_ReferenceID(Integer fk_ReferenceID) {
this.fk_ReferenceID = fk_ReferenceID;
}

public Integer getFk_FleaID() {
return this.fk_FleaID;
}

public void setFk_FleaID(Integer fk_FleaID) {
this.fk_FleaID = fk_FleaID;
}

public Integer getFk_CountyID() {
return this.fk_CountyID;
}

public void setFk_CountyID(Integer fk_CountyID) {
this.fk_CountyID = fk_CountyID;
}

public Integer getFk_HostID() {
return this.fk_HostID;
}

public void setFk_HostID(Integer fk_HostID) {
this.fk_HostID = fk_HostID;
}

public HibernateOperations.fleas getFleasER() {
return this.fleasER;
}

public void setFleasER(HibernateOperations.fleas fleasER) {
this.fleasER = fleasER;
}

public String toString() {
return new ToStringBuilder(this)
.append("pk_InputDataID", getPk_InputDataID())
.toString();
}

}


2. fleas.java

package HibernateOperations;

import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class fleas implements Serializable {

/** identifier field */
private Integer pk_FleaID;

/** nullable persistent field */
private String Name;

/** nullable persistent field */
private String Surname;


/** full constructor */
public fleas(Integer pk_FleaID, String Name, String SurName) {
this.pk_FleaID = pk_FleaID;
this.Name = Name;
this.SurName = SurName;
}

/** default constructor */
public fleas() {
}

/** minimal constructor */
public fleas(Integer pk_FleaID) {
this.pk_FleaID = pk_FleaID;
}

public Integer getPk_FleaID() {
return this.pk_FleaID;
}

public void setPk_FleaID(Integer pk_FleaID) {
this.pk_FleaID = pk_FleaID;
}

public String getName() {
return this.Name;
}

public void setName(String Name) {
this.Name = Name;
}

public String getSurName() {
return this.SurName;
}

public void setSurName(String SurName) {
this.SurName = SurName;
}


public String toString() {
return new ToStringBuilder(this)
.append("pk_FleaID", getPk_FleaID())
.toString();
}

}


*************java main class*********************************

/*
* PerformSearch.java
*
*/

package DatabaseOperations;

/**
* @author mand
*/

import HibernateOperations.fleas;
import HibernateOperations.inputdata;
import java.util.ArrayList;

import java.util.List;
import java.util.Properties;
import net.sf.hibernate.MappingException;

import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.dialect.Dialect;
import net.sf.hibernate.id.Configurable;
import net.sf.hibernate.type.Type;



public class PerformSearch {

private Configuration config;
private SessionFactory sessionFactory;
private Session session;


/** Creates a new instance of PerformSearch */

public PerformSearch() {

}

public String[] primarySearch(){


try{

String findQry = "from inputdata i inner join fleas f on " +
"i.fk_FleaID=f.pk_FleaID";

config = new Configuration();
config.addClass(HibernateOperations.inputdata.class);
sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();

List list = session.find(findQry);

/*
String[] tempFleasSpecies = new String[list.size()];

for(int i=0 ; i < list.size() ; i++){
String fs = (String) list.get(i);
tempFleasSpecies[i] = fs.toString().trim();
}

fleasSpeciesAry = tempFleasSpecies;
*/

}catch(HibernateException he){
he.printStackTrace();
}finally {
try {
if (session != null) {
session.close();
}
} catch (net.sf.hibernate.HibernateException excp) {
System.err.println(excp);
}
}

return fleasSpeciesAry;
}

public static void main(String[] args){

PerformSearch ps = new PerformSearch();
String[] tempFleaseSpecies = ps.primarySearch();

/*
for(int i = 0 ; i < tempFleaseSpecies.length ; i++){
System.out.println("No ["+i+"] :" + tempFleaseSpecies[i]);
}*/
}

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.