-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: mapping exception
PostPosted: Mon Mar 08, 2010 2:24 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
i am new to hibernate i try the example for foreign key mapping but it show the exception mentioned below.
i send the xml files and pojo classes. please reply me soon it is very urgent.
i have to finish this task.

advance thanks for those u reply to me.

contact.hbm.xml mapping.
<?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="join.Contact" table="emp">
<id name="id" type="int" column="ID" >
<generator class="increment"/>
</id>

<property name="firstName" column ="firstname"/>
<property name="lastName" column ="lastname"/>
<property name="insid" column ="InsID"/>
<bag name="insurance" inverse="
true" cascade="all,delete-orphan">
<key column="InsID"/>
<one-to-many class="
join.Insurance"/>
</bag>
</class>
</hibernate-mapping>

insurance.xml files
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="join.Insurance" table="Insurance">
<id name="id" column ="InsID" type="int">
<generator class="increment"/>
</id>
<property name="insuranceName" column="InsuranceName" type="String"/>
<property name="investementAmount" column="InvestmentAmount" type="double"/>
<many-to-one name="Contact" class="
join.Contact" column="InsID"
insert="false" update="false"/>

</class>

</hibernate-mapping>
hibernate .cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">

jdbc:mysql://localhost/sakthi</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="show_sql">true</property>
<property name="connection.autocommit">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
<mapping resource="insurance.hbm.xml"/>
</session-factory>
</hibernate-configuration>

EmpDetail.java
package sample;

public class EmpDetail{

private String firstname;
private String city;
private int empid;


public String getfirstname() {
return firstname;
}

public void setfirstname(String firstname) {
this.firstname =firstname;
}

public String getcity() {
return city;
}

public void setcity(String city) {
this.city = city;
}


public int getempid() {
return empid;
}

public void setempid(int Empid) {
this.empid =empid;
}
}
Employee.java
package sample;

public class Employee {
private String firstname;

private String email;
private int empid;

/**
* @return Email
*/


/**
* @return First Name
*/
public String getfirstname() {
return firstname;
}

public void setfirstname(String string) {
firstname = string;
}

public String getemail() {
return email;
}


/**
* @param string Sets the Email
*/
public void setemail(String string) {
email = string;
}

/**
* @param string Sets the First Name
*/


/**
* @param string sets the Last Name
*/

public int getempid() {
return empid;
}

/**
* @param l Sets the ID
*/
public void setempid(int i) {
empid = i;
}

}

my main class

package sample;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.*;
/*import java.util.List;
import java.util.ListIterator;*/
import sample.Employee;
import sample.EmpDetail;
public class Inner {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
try{
SessionFactory fact = new
Configuration().configure().buildSessionFactory();
sess = fact.openSession();
String sql_query = " select empdet.firstname,empdet.city,empmail.firstname,empmail.email FROM sample.EmpDetail empdet left outer join fetch sample.Employee empmail where empdet.Empid = empmail.Empid";
Query query = sess.createQuery(sql_query);
Iterator ite = query.list().iterator();

System.out.println("FirstName\t"+"Email\t"+"FirstName\t"+"City");
while ( ite.hasNext() ) {
Object[] pair = (Object[]) ite.next();
Employee mail = (Employee) pair[0];
EmpDetail det = (EmpDetail) pair[1];
System.out.print(mail.getfirstname());
System.out.print(mail.getemail());
System.out.print("\t"+det.getfirstname());
System.out.print("\t\t"+det.getcity());
System.out.println();
}
sess.close();
}
catch(Exception e ){
System.out.println(e.getMessage());
e.printStackTrace();
}

}

}


11:43:53,250 INFO Environment:456 -11:43:53,250 INFO Environment:469 -11:43:53,250 INFO Environment:502 -11:43:53,250 INFO Environment:532 -11:43:53,250 INFO Configuration:1228 -11:43:53,265 INFO Configuration:1199 -11:43:53,375 INFO Configuration:439 -11:43:53,484 INFO HbmBinder:256 -11:43:53,500 INFO Configuration:439 -11:43:53,546 INFO HbmBinder:256 -11:43:53,609 INFO Configuration:1340 -11:43:53,609 INFO Configuration:844 -11:43:53,609 INFO Configuration:848 -Association references unmapped class: join.Insurance
org.hibernate.MappingException: Association references unmapped class: join.Insurance
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:1946)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2295)
at org.hibernate.cfg.HbmBinder$SecondPass.doSecondPass(HbmBinder.java:2266)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:853)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1036)
at join.Inner.main(Inner.java:16)


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 3:07 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
<one-to-many class="
join.Insurance"/>


I don't know if it is just a copy-and-past issue but the line-break in the above mapping can't be inside the class attribute value.


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 5:31 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
i can't able to understand .
any body say it clearly where i have made the mistake it is very urgent

reply me soon i have to clear this exception immediately.


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 5:36 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Remove the line-break:

Code:
<one-to-many class="join.Insurance"/>


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 5:48 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
if i remove that line it is throwing sax parse exception showing many exception and also bag is incomplete.

what is can i do now


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 5:58 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Do not remove the line... only the line break. Instead of:

Code:
<one-to-many class="
join.Insurance"/>


It should be:

Code:
<one-to-many class="join.Insurance"/>


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 6:04 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
thanks alot for ur help i suffered alot to correct this error. but now it is showing org.hibernate.MappingException: Could not determine type for: String, for columns: [org.hibernate.mapping.Column(InsuranceName)]

can u please tell me where i am going wrong.


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 6:23 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Case is important, use "string", not "String".


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 6:25 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
hai i corrected the previous exception but now i am getting
org.hibernate.PropertyNotFoundException: Could not find a getter for insurance in class join.Contact
i have checked thoroughly Contact.java pojo class .

see where i have made the mistake

advance thanks for ur reply i am waiting for ur reply it is very urgent.

once again i send the Contact.java POJO class to u.


package join;

public class Contact {
private String firstName;
private String lastName;
private int insid;
private int id;

/**
* @return insid
*/
public int getinsid() {
return insid;
}

/**
* @return First Name
*/

public String getFirstName() {
return firstName;
}

/**
* @return Last name
*/
public String getLastName() {
return lastName;
}

/**
* @param string Sets the Insid
*/

public void setinsid(int i) {
id = i;
}

/**
* @param string Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param string sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}
public int getId() {
return id;
}

/**
* @param l Sets the ID
*/
public void setId(int l) {
id = l;
}

}


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 6:46 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
hai have u check my pojo class Contact.java

whether i have made any mistake

reply soon i am waiting for ur reply i have to complete today itself.it is very urgent.


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 6:56 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Every property/association needs corresponding getter/setter methods. So the <bag name="insurance" ...> mapping requires that your Contact class has getInsurance() and setInsurance() methods. Since bags are implemented as lists in Hibernate you'll need something like this:

Code:
private List insurance;
public List getInsurance()
{
  return insurance;
}
public void setInsurance(List insurance)
{
   this.insurance = insurance;
}


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 7:09 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
thanks alot i corrected but still it is throwing exception as mentioned below.

org.hibernate.QueryException: could not resolve property: emp of: join.Insurance [from join.Insurance i inner join i.emp as e]
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.BasicEntityPersister.toType(BasicEntityPersister.java:1108)
at org.hibernate.hql.ast.FromElementType.getPropertyType(FromElementType.java:223)
at org.hibernate.hql.ast.FromElement.getPropertyType(FromElement.java:333)
at org.hibernate.hql.ast.DotNode.getDataType(DotNode.java:439)
at org.hibernate.hql.ast.DotNode.prepareLhs(DotNode.java:205)
at org.hibernate.hql.ast.DotNode.resolve(DotNode.java:165)
at org.hibernate.hql.ast.FromReferenceNode.resolve(FromReferenceNode.java:87)
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:266)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:2475)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2323)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2232)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:498)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:356)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:184)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:140)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:814)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:773)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at join.Inner.main(Inner.java:21)
reply me soon where i done a mistake.


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 7:26 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
hai can u tell me where i have done the mistake.

it is very urgent i am waiting for ur reply.


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Mon Mar 08, 2010 7:46 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The mapping files first post show that the Insurance class has two properties (insuranceName and investementAmount) and one association (Contact). You are trying to join to 'emp' which is not defined in the mapping. I guess you need to add the 'emp' association to the mapping.


Top
 Profile  
 
 Post subject: Re: mapping exception
PostPosted: Wed Mar 10, 2010 7:16 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
sorry for the late reply now only i saw ur reply

u are saying to add emp to the association but it is table name how i can add as association in the coding u please mention where i have to give it.

i am waiting for ur reply.

advance thanks those who reply me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.