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