-->
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.  [ 5 posts ] 
Author Message
 Post subject: Trouble from book Java Persistence With Hibernate
PostPosted: Thu Jun 26, 2008 6:47 pm 
Newbie

Joined: Fri Jun 06, 2008 2:03 pm
Posts: 9
Hi,

I am a hibernate newbie and I hope this is not a stupid question. I have been trying to follow the examples as given in the book listing 2.1 and 2.2 to learn Hibernate and I am getting this error that I don't understand. Please help. This is the sql query I used to set up the table.

CREATE TABLE MESSAGES(
MESSAGE_ID INT(4) NOT NULL AUTO_INCREMENT,
MESSAGE_TEXT VARCHAR(50),
NEXT_MESSAGE_ID INT(4),
PRIMARY KEY (MESSAGE_ID),
FOREIGN KEY(NEXT_MESSAGE_ID) REFERENCES MESSAGES(MESSAGE_ID));


Hibernate version: 3.2

Mapping documents:


<class name="com.jbe.test.hibernate.model.Message" table="MESSAGES">

<id name="id" column="MESSAGE_ID">
<generator class="increment"></generator>
</id>

<property name = "text" column="MESSAGE_TEXT" type="string"/>

<many-to-one name="nextMessage" cascade="all"
column="NEXT_MESSAGE_ID" foreign-key="NEXT_MESSAGE_ID" />
</class>


Code between sessionFactory.openSession() and session.close():

session.beginTransaction();
session.save(message);
session.getTransaction().commit();



Full stack trace of any exception that occurs:

org.hibernate.MappingException: An association from the table MESSAGES refers to an unmapped class: java.lang.Integer
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1262)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1180)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1296)
at com.jbe.test.hibernate.dao.impl.MessageDaoImpl.addMessage(MessageDaoImpl.java:24)
at com.jbe.test.hibernate.test.MessageTest.testAddMessage(MessageTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:478)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:617)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:885)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
at org.testng.TestRunner.runWorkers(TestRunner.java:712)
at org.testng.TestRunner.privateRun(TestRunner.java:582)
at org.testng.TestRunner.run(TestRunner.java:477)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:324)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:319)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
at org.testng.SuiteRunner.run(SuiteRunner.java:198)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:823)
at org.testng.TestNG.runSuitesLocally(TestNG.java:790)
at org.testng.TestNG.run(TestNG.java:708)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)




Name and version of the database you are using: mysql 5.0.22

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Jun 26, 2008 3:37:07 PM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.3.0.CR1
Jun 26, 2008 3:37:07 PM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Jun 26, 2008 3:37:07 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Jun 26, 2008 3:37:07 PM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Jun 26, 2008 3:37:07 PM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Jun 26, 2008 3:37:07 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Jun 26, 2008 3:37:07 PM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : com/google/test/hibernate/Messages.hbm.xml
Jun 26, 2008 3:37:07 PM org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: com.google.test.hibernate.model.Message -> MESSAGES
Jun 26, 2008 3:37:07 PM org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
FAILED: testAddMessage(Message [
Id = null
Text = Hello World])
Add Message
org.hibernate.MappingException: An association from the table MESSAGES refers to an unmapped class: java.lang.Integer
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1262)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1180)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1296)
at com.google.test.hibernate.dao.impl.MessageDaoImpl.addMessage(MessageDaoImpl.java:24)
at com.google.test.hibernate.test.MessageTest.testAddMessage(MessageTest.java:58)
... Removed 22 stack frames






Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject: class needs mapping
PostPosted: Thu Jun 26, 2008 7:10 pm 
Newbie

Joined: Tue Feb 06, 2007 10:41 am
Posts: 1
Location: Baltimore, MD
You need to provide the mapping for the "nextMessage" class that you are refering to in the many-to-one association. Make sure your include it in the .config file.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 27, 2008 10:02 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
It does seem peculiar that you're getting this on the Integer class.

Again, ensure both sides of the relationship have mapped entities. If the problem persists, post your mapping files.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 2:05 pm 
Newbie

Joined: Fri Jun 06, 2008 2:03 pm
Posts: 9
Mapping File:


<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="com.jbe.test.hibernate.model.MessageWithAnnotations"/>
</session-factory>
</hibernate-configuration>



Entity Model:

package com.jbe.test.hibernate.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
* Transfer object to leverage hibernate annotationss to represent database to
* object mapping relationships.
*/
@Entity
@Table(name = "MESSAGES")
public class MessageWithAnnotations {

@Column(name = "MESSAGE_ID")
@GeneratedValue
@Id
private Integer id;

@Column(name = "MESSAGE_TEXT")
private String text;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "NEXT_MESSAGE_ID")
private MessageWithAnnotations nextMessage;

public Integer getId() {
return id;
}

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

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public MessageWithAnnotations getNextMessage() {
return nextMessage;
}

public void setNextMessage(MessageWithAnnotations nextMessage) {
this.nextMessage = nextMessage;
}

private MessageWithAnnotations() {}

public MessageWithAnnotations(String text) {
this.text = text;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 2:49 pm 
Newbie

Joined: Fri Jun 06, 2008 2:03 pm
Posts: 9
I did some little debugging of my own. I found that this method in the SessionFactoryImpl throws a MappingException.

public EntityPersister getEntityPersister(String entityName) throws MappingException {
EntityPersister result = (EntityPersister) entityPersisters.get(entityName);
if (result==null) {
throw new MappingException( "Unknown entity: " + entityName );
}
return result;
}

The entity persisters HashMap is always empty. Do you know what causes this to empty?


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