-->
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.  [ 4 posts ] 
Author Message
 Post subject: Simple koreign key
PostPosted: Mon Jan 30, 2006 10:50 am 
Newbie

Joined: Thu Jan 26, 2006 7:26 am
Posts: 6
Location: Bucharest
Hibernate version:3.1
Name and version of the database you are using:POSTGRESQL 8.1
I have 2 tables:
CREATE TABLE h(
id serial NOT NULL,
nume varchar(30),
prenume varchar(20),
CONSTRAINT h_pkey PRIMARY KEY (id)
)

CREATE TABLE honey(
id serial NOT NULL,
name text,
taste text,
id_h int4 NOT NULL,
CONSTRAINT honey_pkey PRIMARY KEY (id),
CONSTRAINT "FK_h" FOREIGN KEY (id_h)
REFERENCES h (id)
ON UPDATE RESTRICT ON DELETE CASCADE
)


and Honey.hbm.xml :
<?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 package="util">
<class name="Honey" table="honey">
<id name="id" column="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">honey_id_seq</param>
</generator>
</id>
<property name="name" column="name" type="java.lang.String" />
<property name="taste" column="taste" type="java.lang.String" />
<many-to-one name="id_h" column="id_h" not-null="true" />
</class>

<class name="H" table="h">
<id name="id" column="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">h_id_seq</param>
</generator>
</id>
<property name="nume" column="nume" type="java.lang.String" />
<property name="prenume" column="prenume" type="java.lang.String" />
</class>
</hibernate-mapping>


and I obtained an error :
org.hibernate.MappingException: An association from the table honey refers to an unmapped class: java.lang.Integer
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1122)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1072)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1156)
at util.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:19)
at util.TestClient.createHoney(TestClient.java:37)
at util.TestClient.main(TestClient.java:19)
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:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86)
java.lang.NullPointerException
at util.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:25)
at util.TestClient.createHoney(TestClient.java:37)
at util.TestClient.main(TestClient.java:19)
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:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86)
Exception in thread "main"
Process finished with exit code 1


Why?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 11:06 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Code:
<many-to-one name="id_h" column="id_h" not-null="true" />

should be
Code:
<many-to-one name="id_h" column="id_h" class="H"   not-null="true" />


Since you dint mention the class, it looks like it picked it from the Database(int) and thus the java.lang.Integer.


Top
 Profile  
 
 Post subject: Re: Simple Foreign Key
PostPosted: Mon Jan 30, 2006 11:13 am 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
You should post the code for your Honey class as well. Make sure the many-to-one property of your class is treated as an H class. It seems that you are treating the FK as an Integer in your class, which is why Hibernate is complaining about an unmapped "java.lang.Integer" type. You try something like:

[In Honey.java]
Code:
private H hItem;

public H getH() {
    return hItem;
}

public void setH(H hItem) {
    this.hItem = hItem;
}


[In your Honey.hbm.xml]
Code:
<many-to-one name="h" column="id_h" not-null="true" />

_________________
-Chris


Top
 Profile  
 
 Post subject: Re: Simple foreign key
PostPosted: Mon Jan 30, 2006 11:17 am 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
As far as what rajasaur said, he's not quite right. Here's a quote on the "class" property from the hibernate manual:

Quote:
class (optional - defaults to the property type determined by reflection): The name of the associated class.


So you must've had the FK property stored as an Integer in your Honey class.

_________________
-Chris


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