-->
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.  [ 11 posts ] 
Author Message
 Post subject: MappingException: Unknown entity class: java.lang.String
PostPosted: Sun Dec 26, 2004 1:33 pm 
Newbie

Joined: Sun Dec 26, 2004 1:06 pm
Posts: 7
Location: TX, USA
Hi everyone.
I'm a beginner, and am trying out a very simple program to understand the concepts. There's Users class, which has "username" and "pswd" columns. A UserRights table has a username column which references username in Users and a userrights column which is an integer holding a numeric value. The UserRights table also has a surrogate primary key column. I am able to retreive records from the UserRights table, but I am not able to insert any records into the UserRights table.

I keep getting a
net.sf.hibernate.MappingException: Unknown entity class: java.lang.String
error.

Any help is greatly appreciated.

Hibernate version: 2.1.7

Mapping documents:
Users.hbm.xml:
Code:
<hibernate-mapping>
    <class name="okay.hbvo.Users" table="users">
   <id name="username" type="java.lang.String">
           <generator class="assigned"/>
        </id>
        <property name="pswd" type="java.lang.String"/>
    </class>
</hibernate-mapping>


UserRight.hbm.xml:
Code:
<hibernate-mapping>
    <class name="okay.hbvo.UserRight" table="userright">
        <id name="surrpk" type="java.lang.Integer" unsaved-value="0">
           <generator class="increment"/>
        </id>
        <many-to-one name="username" class="okay.hbvo.Users" column="username" unique="true"/>
        <property name="userRights" type="java.lang.Long"/>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
getting the DAO:
UserRightDAO urdao = DAOFactory.getUserRightDAO();
Long uright = new Long((long)12345);
String urname = new String("karan"); // this user exists in Users table
UserRight uro = new UserRight(urname, uright);
urdao.insertUR(uro);
MainUserRightDAO's insertUR function (takes a UserRight object as parameter):
HibernateUtil.beginTransaction();
Session session = HibernateUtil.getSession();
session.save(URo);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();

Full stack trace of any exception that occurs:
2004-12-26 10:57:33,548 [main] DEBUG okay.dao.HibernateUtil - ****************** SESSIONFACTORY MADE ******************
2004-12-26 10:57:33,549 [main] DEBUG okay.dao.HibernateUtil - Starting new database transaction in this thread.
2004-12-26 10:57:33,550 [main] DEBUG okay.dao.HibernateUtil - Opening new Session for this thread.
2004-12-26 10:57:33,934 [main] DEBUG net.sf.hibernate.impl.SessionImpl - opened session
2004-12-26 10:57:33,940 [main] DEBUG net.sf.hibernate.transaction.JDBCTransaction - begin
2004-12-26 10:57:33,942 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - total checked-out connections: 0
2004-12-26 10:57:33,944 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
2004-12-26 10:57:33,945 [main] DEBUG net.sf.hibernate.transaction.JDBCTransaction - current autocommit status:false
2004-12-26 10:57:33,950 [main] DEBUG net.sf.hibernate.id.IncrementGenerator - fetching initial value: select max(surrpk) from userright
2004-12-26 10:57:33,960 [main] DEBUG net.sf.hibernate.id.IncrementGenerator - first free id: 1
2004-12-26 10:57:33,962 [main] DEBUG net.sf.hibernate.impl.SessionImpl - generated identifier: 1
2004-12-26 10:57:33,967 [main] DEBUG net.sf.hibernate.impl.SessionImpl - saving [okay.hbvo.UserRight#1]
######### Exception in insertUR ############
2004-12-26 10:57:34,143 [main] DEBUG okay.dao.HibernateUtil - Tyring to rollback database transaction of this thread.
2004-12-26 10:57:34,145 [main] DEBUG net.sf.hibernate.transaction.JDBCTransaction - rollback
2004-12-26 10:57:34,147 [main] DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion
2004-12-26 10:57:34,148 [main] DEBUG okay.dao.HibernateUtil - Closing Session of this thread.
2004-12-26 10:57:34,149 [main] DEBUG net.sf.hibernate.impl.SessionImpl - closing session
2004-12-26 10:57:34,150 [main] DEBUG net.sf.hibernate.impl.SessionImpl - disconnecting session
2004-12-26 10:57:34,155 [main] DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - returning connection to pool, pool size: 1
2004-12-26 10:57:34,157 [main] DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion
net.sf.hibernate.MappingException: Unknown entity class: java.lang.String
net.sf.hibernate.MappingException: Unknown entity class: java.lang.String
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:346)
at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2694)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2701)
at net.sf.hibernate.impl.SessionImpl.isUnsaved(SessionImpl.java:1092)
at net.sf.hibernate.impl.SessionImpl.nullifyTransientReferences(SessionImpl.java:1038)
at net.sf.hibernate.impl.SessionImpl.nullifyTransientReferences(SessionImpl.java:1024)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:928)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:858)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:780)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:739)
at okay.dao.MainUserRightDAO.insertUR(MainUserRightDAO.java:16)
at okay.hibMain.main(hibMain.java:23)
######### Exception in hibMAIN ############
okay.dao.InfrastructureException: net.sf.hibernate.MappingException: Unknown entity class: java.lang.String
okay.dao.InfrastructureException: net.sf.hibernate.MappingException: Unknown entity class: java.lang.String
at okay.dao.MainUserRightDAO.insertUR(MainUserRightDAO.java:27)
at okay.hibMain.main(hibMain.java:23)
Caused by: net.sf.hibernate.MappingException: Unknown entity class: java.lang.String
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:346)
at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2694)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2701)
at net.sf.hibernate.impl.SessionImpl.isUnsaved(SessionImpl.java:1092)
at net.sf.hibernate.impl.SessionImpl.nullifyTransientReferences(SessionImpl.java:1038)
at net.sf.hibernate.impl.SessionImpl.nullifyTransientReferences(SessionImpl.java:1024)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:928)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:858)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:780)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:739)
at okay.dao.MainUserRightDAO.insertUR(MainUserRightDAO.java:16)
... 1 more


Name and version of the database you are using:
PostgreSQL 7.4.2

The generated SQL (show_sql=true):
2004-12-26 10:57:33,950 [main] DEBUG net.sf.hibernate.id.IncrementGenerator - fetching initial value: select max(surrpk) from userright
This is all I see.

Debug level Hibernate log excerpt:
its up there.

_________________
persist. persist. persist.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 26, 2004 4:06 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
first guess: your username property is mapped to a User but is actually a string on your java class...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 03, 2005 12:57 pm 
Newbie

Joined: Sun Dec 26, 2004 1:06 pm
Posts: 7
Location: TX, USA
how do i make that change??
i tried a one-to-one mapping, but got the same errors.

_________________
persist. persist. persist.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 03, 2005 1:16 pm 
Newbie

Joined: Sun Dec 26, 2004 1:06 pm
Posts: 7
Location: TX, USA
here's some more msgs when hibernate is making the mapping:

10:43:33,339 INFO Environment:528 - using java.io streams to persist binary types
10:43:33,341 INFO Environment:529 - using CGLIB reflection optimizer
10:43:33,493 INFO Configuration:331 - Mapping resource: okay/hbvo/Users.hbm.xml
10:43:34,874 DEBUG DTDEntityResolver:20 - trying to locate http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath under net/sf/hibernate/
10:43:34,879 DEBUG DTDEntityResolver:29 - found http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath
10:43:35,530 INFO Binder:229 - Mapping class: okay.hbvo.Users -> users
10:43:35,897 DEBUG Binder:486 - Mapped property: username -> username, type: string
10:43:36,062 DEBUG Binder:486 - Mapped property: pswd -> pswd, type: string
10:43:36,067 INFO Configuration:331 - Mapping resource: okay/hbvo/UserRight.hbm.xml
10:43:36,226 DEBUG DTDEntityResolver:20 - trying to locate http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath under net/sf/hibernate/
10:43:36,240 DEBUG DTDEntityResolver:29 - found http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath
10:43:36,525 INFO Binder:229 - Mapping class: okay.hbvo.UserRight -> userright
10:43:36,528 DEBUG Binder:486 - Mapped property: surrpk -> surrpk, type: integer
10:43:36,717 DEBUG Binder:486 - Mapped property: username, type: okay.hbvo.Users
10:43:36,719 DEBUG Binder:486 - Mapped property: userRights -> userRights, type: long

So, "username" is of type string in the Users class, but username is also a FK in the UserRight class, so it must be of type okay.hbvo.Users. That is the difference between the two, whereas both of them must be of the same type. How do I map this correctly?

_________________
persist. persist. persist.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 03, 2005 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
please read the basic hibernate tutorials - it's covered there.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 03, 2005 4:45 pm 
Newbie

Joined: Sun Dec 26, 2004 1:06 pm
Posts: 7
Location: TX, USA
I'm sorry, I've not been able to get a solution to this problem.
I've read the FAQs, the tutorial at systemmobile, the online reference and the book.
Maybe I'm not reading it right.

I don't want an inverse relationship between the tables. I am just looking for a simple FK refernce in my mapping from the UserRight class to the Users class. I can update, insert, whatever on the Users class right now, and I can retreive data from the UserRights table. However, I cannot insert into the UserRights table -- I keep getting the same error. AFAIK, I have to give this statement to show a FK reference in my mapping:
<code><many-to-one name="username" class="okay.hbvo.Users" column="username"/></code>
in UserRight.hbm.xml.

This should tell hibernate that the "username" column in Users class is being referenced here. I have tried changing this statement but I keep getting the same error. What am I doing wrong?

Thanks for any help.

_________________
persist. persist. persist.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 03, 2005 5:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
and how does your corresponding property field/methods look like ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 03, 2005 6:10 pm 
Newbie

Joined: Sun Dec 26, 2004 1:06 pm
Posts: 7
Location: TX, USA
Code:
      UserRightDAO urdao = DAOFactory.getUserRightDAO();
      UserRight uro = new UserRight("kmg", new Long("567"));
      urdao.insertUR(uro);


Code in the DAO:
Code:
      HibernateUtil.beginTransaction();
      Session session = HibernateUtil.getSession();
      session.save(URo);
      HibernateUtil.commitTransaction();
      HibernateUtil.closeSession();


Persistent Class for UserRight:
Code:
package okay.hbvo;
import java.io.Serializable;

public class UserRight implements Serializable
{
    private Integer surrpk = null;
    private String  username;
    private Long    userRights;
   
    public UserRight()
    {}

    public UserRight(String username, Long userRights)
    {
   this.username = username;
   this.userRights = userRights;
    }

    public Integer getSurrpk()
    {   return surrpk;    }

    public void setSurrpk(Integer surrpk)
    {   this.surrpk = surrpk;    }

    public String getUsername()
    {   return username;    }

    public void setUsername(String username)
    {   this.username = username;    }

    public Long getUserRights()
    {   return userRights;    }

    public void setUserRights(Long userRights)
    {   this.userRights = userRights;    }
}


The Database Schema:
Code:
CREATE TABLE Users
   (
      username   varchar(20)   PRIMARY KEY,
      pswd      varchar(20)
   );

CREATE TABLE UserRight
   (
                surrpk          integer         PRIMARY KEY,
      username   varchar(20)   UNIQUE REFERENCES Users(username),
      userRights   integer
   );


Thank you.

_________________
persist. persist. persist.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 3:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
ok - as I thought - you are mapping a String property to a User property, that is not the right way to go. You should have a User property mapped to a User property!

The String is just in the database and probably a key in your User object - UserRight should point to a User, not a String....


...and this is EVERYWHERE in all examples that concerns simple associations....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 1:15 pm 
Newbie

Joined: Sun Dec 26, 2004 1:06 pm
Posts: 7
Location: TX, USA
Thanks for your replies max.

In the database:
Users has a username and a pswd column.
UserRight has surrpk, username, and userright columns.
And UserRight(username) references Users(username).

So username IS a string in both Users and UserRights. The current code is what I've picked up and written from whatever I have read. I don't need the entire Users object in UserRights, so the current implementation is probably not the right way to go. I could make a persistent Users object in my UserRights class and work that way, but that is not what I require.

I've been looking at the mapping example on pg 221 of the book. In section "Using a Foreign Key Association", it shows that to map a billingAddress to a User, we have to use a FK column. In my case there is only one column, the username, whcih is common to both the tables. So I dont want to put in an extra FK_ID column.

Am I even thinking this right? The database will make sure that consistency is maintained, so I want to know if I even need to do a many-to-one in my code. Because what I am looking for right now is a real simple FK mapping, which will translate to the following: if I delete a user in Users, it must delete the corresponding row in UserRights. I know I can use inverse mapping with cascading for this, but I am still confused about using the User object UserRights. This is a conceptual problem, for which I would really appreciate your guidance.

_________________
persist. persist. persist.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 04, 2005 1:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
They are a string in the database TABLE.

In the UserRight java CLASS they should be a concrete User if you want to map with Hibernate.

The String user in UserRight class is not needed for Hibernate.

_________________
Max
Don't forget to rate


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