-->
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.  [ 8 posts ] 
Author Message
 Post subject: Inheritance problem resulting in
PostPosted: Wed Jul 27, 2005 9:44 am 
Newbie

Joined: Mon Jul 18, 2005 6:57 am
Posts: 12
Hi,

I'm running into a problem that occurs occassionally when I run my tests.

I have 2 classes, User and LoggedInUser.

LoggedInUser extends User. It is essentially exactly the same as User but has a few extra methods such as Privileges.

At the moment I created a Hibernate Mapping file for both classes and depending on which one I want to return I call 1 or the other.

However, sometimes when I have already returned a loggedInUser and then try and return a User it results in a WrongClassCastException stating it is not of Type LoggedInUser.

Both mapping files or exactly the same.

I have read through how I am advised to deal with inheritance in Hibernate in Action but it doesn't seem to really cater for what I am trying to do. I can't put a descriminator column in as I don't not whether they are logged in or not until I check that their password is correct.

Anyone have any experience of how to solve this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 11:10 am 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
Post the requested information please. It's very hard to guess what your code might be doing.

Quote:
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 10:42 am 
Newbie

Joined: Mon Jul 18, 2005 6:57 am
Posts: 12
Right, some more info.

I have gone for a slightly differenet approach. I now only have one mapping file for User. (see below for the file) When a User logins in a return a User object using the mapping file and then create a LoggedInUser object with the User data and the additional fields I need to populate.

That all works fine but I run into a problem a bit further down the line. At any point, and part of the application can get the LoggedInUser object by calling UserService.getCurrentUser().

If the try and save this object, it obviously complains that there is no mapping file associated with LoggedInUser. If I do have a mapping file associated with LoggedInUser then I get many other ClassCastExceptions in other places.

Is there a good way of designing a solution to this problem so that I Hibernate knows that LoggedInUser object is actually a User object and to save that in the database?

Hibernate Version
--------------------------------
3.0.5

User.hbm.xml
--------------------------------------------------------------


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="uk.gov.ch.chips.common.user.User"
table="USER_ACCESS"
dynamic-update="false"
dynamic-insert="false"
>

<id name="id"
column="USER_ACCESS_ID"
type="java.lang.Long"
access="field">
<generator class="assigned" />
</id>

<property name="loginId"
column="LOGIN_ID"
type="string"
access="field"
length="30"
not-null="true"
/>

<property name="firstName"
column="USER_FORENAME"
type="string"
access="field"
length="50"
/>

<property name="lastName"
column="USER_SURNAME"
type="string"
access="field"
length="160"
/>

<property name="emailAddress"
column="USER_EMAIL"
type="string"
access="field"
length="256"
/>

<property name="telExt"
column="USER_TELEPHONE_NO"
type="string"
access="field"
/>

<property
name="locked"
type="yes_no"
update="true"
insert="true"
access="property"
column="Locked_IND"
length="1"
not-null="true"
/>

<property name="loginFailureCount"
column="LOGIN_FAILURE_COUNT"
type="int"
update="true"
insert="true"
access="property"
length="4"
not-null="true"
/>

<property
name="customStyleSheet"
column="CUSTOM_STYLESHEET"
type="string"
access="field"
length="64"
/>


<property
name="effectiveDate"
type="java.util.Date"
access="field"
column="USER_EFF_DATE"
length="10"
not-null="true"
/>

<property
name="endDate"
type="java.util.Date"
access="field"
column="USER_END_DATE"
length="10"
not-null="true"
/>

<property
name="tdfStart"
column="TDF_START"
type="java.lang.Integer"
access="field"
length="3"
/>

<property
name="tdfEnd"
column="TDF_END"
type="java.lang.Integer"
access="field"
length="3"
/>

<many-to-one name="homeOrgUnit"
class="uk.gov.ch.chips.common.user.OrgUnit"
access="field">
<column
name="ORGANISATIONAL_UNIT_ID"
/>
</many-to-one>

<list name="userPrivileges"
table="USER_PRIVILEGE"
lazy="true"
inverse="true"
access="field">
<key column="USER_ACCESS_ID" />
<index column="USER_PRIVILEGE_ID" />
<many-to-many class="uk.gov.ch.chips.common.user.Privilege"
column="PRIVILEGE_TYPE_ID" />
</list>

<list name="userGroups"
table="USERACC_GROUPACC_LINK"
lazy="true"
inverse="true"
access="field"
cascade="save-update">
<key column="USER_ACCESS_ID" />
<index column="USER_GROUPACC_LINK_ID" />
<many-to-many class="uk.gov.ch.chips.common.user.Group"
column="GROUP_ACCESS_ID" />
</list>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 10:54 am 
Newbie

Joined: Mon Jul 18, 2005 6:57 am
Posts: 12
Sorry, forgot to include stack trace
------------------------------------------------

net.sf.hibernate.MappingException: No persister for: uk.gov.ch.chips.common.user.LoggedInUser
at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:347)
at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2690)
at net.sf.hibernate.impl.SessionImpl.getPersister(SessionImpl.java:2697)
at net.sf.hibernate.impl.SessionImpl.isUnsaved(SessionImpl.java:1091)
at net.sf.hibernate.impl.SessionImpl.nullifyTransientReferences(SessionImpl.java:1037)
at net.sf.hibernate.impl.SessionImpl.nullifyTransientReferences(SessionImpl.java:1023)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:927)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:779)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at uk.gov.ch.chips.server.mis.MisServiceDaoTest.setUp(MisServiceDaoTest.java:56)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 11:00 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
1.) I don't understand what to say against LoggedInUser extends User and using a subclass in the mapping, for example.

2.) I think wouldn't design it this way. Why don't you take a user which has an additional attribute "loggedIn" (boolean)? Your application then checks whether any user method can be called or not - depending on the attribute "loogedIn".

3.) You're using the wrong DTD with your version of Hibernate. You the following instead:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 11:11 am 
Newbie

Joined: Mon Jul 18, 2005 6:57 am
Posts: 12
Unfortunatly the application is already at an advanced stage and the design of how LoggedInUser and User works can't be changed or that idea would have worked.

Upon further investigation I realise that we are actually not using the 3.0.5 but Hibernate 2, I was told we were using the latest version but that turned out to be rubbish!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 11:15 am 
Newbie

Joined: Mon Jul 18, 2005 6:57 am
Posts: 12
I thought to put LoggedInUser as a subclass of User in the mapping file I would need some kind of descriminator in the database table? Is this not so?

Can you give me an example of what I put in the mapping file. Everything I put in seems to make my mapping file invalid. i.e.

<subclass name="...LoggedInUser" (What else do I need to put in here?)

Many thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 11:19 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Of course you need a discriminator using table per class hierarchy strategy (<subclass>).

You can find examples and more information here:
http://www.hibernate.org/hib_docs/v3/re ... nheritance

You will also find information about the other inheritance strategies.

Best regards
Sven


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