-->
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.  [ 9 posts ] 
Author Message
 Post subject: Facing an error : Association references unmapped
PostPosted: Thu Apr 20, 2006 3:49 am 
Newbie

Joined: Thu Apr 20, 2006 3:36 am
Posts: 13
I m new to hibernate n m using Hibernate version : 3.1
This is my first application.I m was able to develop normal add,updat n delete module.But m facing problems whn trying to use associations.

Backend is Oracle. But facing this error,Association references unmapped class: login.UserSalary

My code is as below,


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

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@indusa-sql:1521:mbv</property>
<property name="connection.username">efttrainee</property>
<property name="connection.password">efttrainee</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<mapping resource="User.hbm.xml"/>


</session-factory>

</hibernate-configuration>



user.hbm.xml
---------------------
<?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="login.User" table="users">
<id column="USER_ID" name="id">
<generator class="increment"/>
</id>

<property column="USER_NAME"
length="100"
name="userName"
not-null="true"
type="java.lang.String"/>

<property column="USER_PASSWORD"
length="100"
name="userPassword"
not-null="true"
type="java.lang.String"/>

<bag name="usersalary" lazy="false">
<key column="USER_ID"/>
<one-to-many class="login.UserSalary"/>
</bag>


</class>

</hibernate-mapping>



UserSalary.hbm.xml
----------------------------
<?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="login.UserSalary" table="usersalary">
<id column="PK_USERSALARY_ID" name="id">
<generator class="increment"/>
</id>

<property column="SALARY"
name="salary"
not-null="false"
type="java.lang.String"/>

<many-to-one name="users" column="USER_ID"
class="login.User"
not-null="true" />

</class>

</hibernate-mapping>



User.java
------------
package login;

import java.util.List;
import java.util.ArrayList;


public class User {

/** identifier field */
private Long id;

/** persistent fields */
private String userName;
private String userPassword;

private List usersalary = new ArrayList();
private List usersalary1;


/** default constructors */
public User() {
}

public User(List usersalary1) {
this.usersalary1 = usersalary1;
}

public List getUsersalary() {
return usersalary;
}

public void setUsersalary(List usersalary) {
this.usersalary = usersalary;
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPassword() {
return this.userPassword;
}

public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

}


UserSalary.java
-------------------
package login;

import java.util.*;

public class UserSalary {

/** identifier field */
private Long id;

/** persistent fields */
private Long salary;

/** foreign fields */
private List users;

/** foreign fields */
private Long userid;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getSalary() {
return salary;
}

public void setSalary(Long salary) {
this.salary = salary;
}

public List getUsers() {
return users;
}

public void setUsers(List users) {
this.users = users;
}

public Long getUserid() {
return userid;
}

public void setUserid(Long user_id) {
this.userid = user_id;
}
}



I regret if such posts have already been posted.But had to post is i was not able to solve the problem


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 4:02 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
You forgot UserSalary in your Hibernate Configuration:

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

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@indusa-sql:1521:mbv</property>
<property name="connection.username">efttrainee</property>
<property name="connection.password">efttrainee</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<mapping resource="User.hbm.xml"/>

<!-- ADD THIS LINE -->
<mapping resource="UserSalary.hbm.xml"/>

</session-factory>

</hibernate-configuration>

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 4:05 am 
Beginner
Beginner

Joined: Thu Apr 20, 2006 3:44 am
Posts: 32
Saumya,

I presume you are trying to write few join queries using these 2 tables, I would suggest you to look at the first page of the reference documentation where he gives you similar implementation.

You can look at 3 things

1) In your HBM files remove type="java.lang.String", this is default

2) You can even remove mapping and try to execute a theta join

e.g HQL will be
from UserSalary , User

Better go through existing implementation in first chapter of reference doc.

hope this helps

Sudhir H


Top
 Profile  
 
 Post subject: Re : Facing an error : Association references unmapped
PostPosted: Thu Apr 20, 2006 5:25 am 
Newbie

Joined: Thu Apr 20, 2006 3:36 am
Posts: 13
The following suggestion did help.Thankful to both of u.. But now facing some pbms vth setter/getter properties of the mapped column i suppose.



The error is as follows :

2006-04-20 14:54:26 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of login.UserSalary.setSalary
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:215)
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:185)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3232)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1676)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:755)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:229)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at org.hibernate.impl.CriteriaImpl.uniqueResult(CriteriaImpl.java:305)
at login.UserInsertHibernate.findByAll(UserInsertHibernate.java:98)
at org.apache.jsp.listUsers_jsp._jspService(listUsers_jsp.java:71)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Thread.java:595)
Caused by: net.sf.cglib.beans.BulkBeanException: java.lang.String
at login.UserSalary$$BulkBeanByCGLIB$$7b889bf.setPropertyValues(<generated>)
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:212)
... 53 more
Caused by: java.lang.ClassCastException: java.lang.String
... 55 more


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 5:31 am 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
You mapped the property salary as a string but in the java class it is a long. set the type of salary to long (in your mapping file)


UserSalary.hbm.xml
----------------------------
<?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="login.UserSalary" table="usersalary">
<id column="PK_USERSALARY_ID" name="id">
<generator class="increment"/>
</id>

<property column="SALARY"
name="salary"
not-null="false"
type="long"/>

<many-to-one name="users" column="USER_ID"
class="login.User"
not-null="true" />

</class>

</hibernate-mapping>

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 5:58 am 
Newbie

Joined: Thu Apr 20, 2006 3:36 am
Posts: 13
I tried vth "long" as well as "java.lang.Long" but no difference :(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 6:09 am 
Newbie

Joined: Thu Apr 20, 2006 3:36 am
Posts: 13
I m sorry.. It did work.. Thank u :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 7:06 am 
Newbie

Joined: Thu Apr 20, 2006 3:36 am
Posts: 13
I made small changes to the code

User.java
------------

package login;

/*import java.util.Set;
import java.util.HashSet;*/
import java.util.List;
import java.util.ArrayList;


public class User {

/** identifier field */
private Long id;

/** persistent fields */
private String userName;
private String userPassword;

private List usersalary = new ArrayList();


/** default constructors */
public User() {
}

public List getUsersalary() {
return usersalary;
}

public void setUsersalary(List usersalary) {
this.usersalary = usersalary;
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getUserName() {
return this.userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPassword() {
return this.userPassword;
}

public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public void addChild(UserSalary child) throws IllegalArgumentException {

if (child == null) {
throw new IllegalArgumentException("Child may not be null");
}

child.setUsers(this);
usersalary.add(child);
}

}



UserSalary.java :
----------------------

package login;

import java.util.*;

public class UserSalary {

/** identifier field */
private Long id;

/** persistent fields */
private Long salary;

/** foreign fields */
private User users;

/** foreign fields */
private Long userid;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getSalary() {
return salary;
}

public void setSalary(Long salary) {
this.salary = salary;
}

public User getUsers() {
return users;
}

public void setUsers(User users) {
this.users = users;
}

public Long getUserid() {
return userid;
}

public void setUserid(Long user_id) {
this.userid = user_id;
}
}


I m able to list the users vth multiple salaries... But while inserting a user i need to insert users salary into the salary table too.. But while inserting the insert statement to usersalary table does not execute givin the error below :

Insert function :
---------------------


public void insert (User user,UserSalary userSalBean)throws Exception {

// Fire up Hibernate
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

// Open Session
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

try {

user.addChild(userSalBean);
session.saveOrUpdate(user);
tx.commit();
session.flush();
session.close();

}catch (RuntimeException e) {
if (tx != null)
tx.rollback();
throw e; // or display error message
}

//session.getTransaction().commit();

}


Error :
-------------

Hibernate: select max(USER_ID) from users
Hibernate: insert into users (USER_NAME, USER_PASSWORD, USER_ID) values (?, ?, ?)
Hibernate: update usersalary set USER_ID=? where PK_USERSALARY_ID=?
Apr 20, 2006 4:30:05 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.TransientObjectException: login.UserSalary
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:71)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:
697)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1037
)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:29
7)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at login.UserInsertHibernate.insert(UserInsertHibernate.java:28)
at org.apache.jsp.addEditUserController_jsp._jspService(addEditUserController_jsp.java:89)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Thread.java:595)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 10:01 am 
Beginner
Beginner

Joined: Thu Apr 20, 2006 3:44 am
Posts: 32
Can any one tell me one thing, to get this many to one or one to many mapping working, in the table on the many side, do you have to define the Foreign Key column exclusiely.

i mean to say I have 2 tables, both share a column name id. But in one table that id is not defined as a foreign key , and that other table has diffrt PK....do u think with this we can define one to many of many to one relnship

i am facing while working on the HBM which shall have defn of this foreign key

any clue ?

sudhir


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.