-->
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: Can the generator class be a string?
PostPosted: Mon Nov 27, 2006 5:41 am 
Newbie

Joined: Mon Nov 27, 2006 5:36 am
Posts: 6
Is it possible to have the <id> in a mapping file be related to a unique string?



When I try this I receive a null reference exception.



My hibernate file is as follows:



<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">



<class name="WindowsApplication1.Sleeve, WindowsApplication1" table="tblReleaseDetails" lazy="true">



<!-- Doesn't work when column is an string -->

<!--

<id name="CatNumber" column="txtCatalogueNumber" access="nosetter.camelcase-underscore" unsaved-value="unknown-catalogue-number">

<generator class="increment"/>

</id>

-->



<!-- Works when column is an autonumber (integer) -->

<id name="Id" column="txtCatalogueNumber" access="nosetter.camelcase-underscore" unsaved-value="0">

<generator class="increment"/>

</id>





<property name="Label" column="txtRecordLabel"/>



etc. etc.

</class>



</hibernate-mapping>



In case this is significant – using NHibernate v1.0.2.0 and NHibernateContrib v1.0.2.0 to communicate with a Jet3.51 database using .Net 2.0



Cheers



Colin


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 5:52 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
It is probably has something to do with the database not letting you use a string as a primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 7:10 am 
Newbie

Joined: Mon Nov 27, 2006 5:36 am
Posts: 6
>It is probably has something to do with the database not letting you use
>a string as a primary key.

Hi.

Thanks for your thoughts.

The database (Access 97) has and does allow a string to be used as a primary key.

Any other ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 7:22 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
I'm not sure then. Maybe NHibernate does not allow id's to be of string type. What error are you getting when you try to run the code?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 7:32 am 
Newbie

Joined: Mon Nov 27, 2006 5:36 am
Posts: 6
>What error are you getting when you try to run the code?

A null reference exception - despite the property to which the Id relates returning a string with a value in it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 7:34 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
Have you marked the property as virtual ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 7:48 am 
Newbie

Joined: Mon Nov 27, 2006 5:36 am
Posts: 6
>Have you marked the property as virtual ?

Oops! I hadn't.

However, when marking the property as virtual I am now receiving a different error message:

"Specified cast is not valid"

Any ideas?

Colin


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 7:51 am 
Beginner
Beginner

Joined: Wed Nov 22, 2006 5:59 am
Posts: 29
Location: London
Can you post your mapping file and entity class so I can have a look.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 8:09 am 
Newbie

Joined: Mon Nov 27, 2006 5:36 am
Posts: 6
My class is as follows:

using System.Drawing;

namespace WindowsApplication1
{

public class Sleeve //: CoreBusinessObject
{
#region Fields

private Image _frontCover;
private int _id;

public virtual int Id
{
get { return _id; }
set { _id = value; }
}

#endregion

#region Constructors

public Sleeve()
: this(0, null)
{
}


public Sleeve(Image frontCover)
: this(0, frontCover)
{
}

public Sleeve(int identity, Image frontCover)
//: base(identity)
{
this.FrontCover = frontCover;
this.CatNumber = "catalogue-number-in-constructor";
}


public Sleeve(Sleeve sleeveToCopy)
//: this(sleeveToCopy.Id, sleeveToCopy.FrontCover)
{
}

#endregion

#region Properties

private string _catNumber;

public virtual string CatNumber
{
get { return "some-primary-key"; }
set { _catNumber = value; }
}
private string _label;

public string Label
{
get { return "record-label"; }
set { _label = value; }
}
private string _format;

public string Format
{
get { return ""; }
set { _format = value; }
}
private string _artist;

public string Artist
{
get { return ""; }
set { _artist = value; }
}
private string _title;

public string Title
{
get { return ""; }
set { _title = value; }
}
private short _value;

public short Value
{
get { return 0; }
set { _value = value; }
}
private string _condition;

public string Condition
{
get { return ""; }
set { _condition = value; }
}
private bool _forSale;

public bool ForSale
{
get { return true; }
set { _forSale = value; }
}



public virtual Image FrontCover
{
get
{
return _frontCover;
}

set
{
_frontCover = value;
}
}

#endregion

#region Methods


public override string ToString()
{
if (FrontCover != null)
{
return "<FrontCover=System.Drawing.Bitmap>";
}
else
{
return "<FrontCover=null>";
}
}

#endregion
}
}

and my hibernate mapping file is as follows:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="WindowsApplication1.Sleeve, WindowsApplication1" table="tblReleaseDetails" lazy="true">

<!-- Doesn't work when column is an string -->
<!---->
<id name="CatNumber" column="txtCatalogueNumber" access="nosetter.camelcase-underscore" unsaved-value="unknown-catalogue-number">
<generator class="assigned"/>
</id>


<!-- Works when column is an autonumber (integer)
<id name="Id" column="txtCatalogueNumber" access="nosetter.camelcase-underscore" unsaved-value="0">
<generator class="increment"/>
</id>
-->


<property name="Label" column="txtRecordLabel"/>
<!--
<property name="Format" column="txtFormat"/>
<property name="Artist" column="txtArtist"/>
<property name="Title" column="txtTitle"/>
<property name="Value" column="txtCondition"/>
<property name="Condition" column="intValue"/>
<property name="ForSale" column="ynForSale"/>
-->

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 8:22 am 
Newbie

Joined: Mon Mar 27, 2006 10:24 am
Posts: 8
Location: Malmoe, Sweden
try
Code:
<generator class="uuid.string"/>


Don't know if this is the problem. Se documentation "5.1.4.1. generator"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 27, 2006 8:29 am 
Newbie

Joined: Mon Nov 27, 2006 5:36 am
Posts: 6
>try <generator class="uuid.string">

I have tried this and various others all to no avail.

FWIW I would have thought 'assigned' would have done the trick where I am assigning this within the business object - but it doesnt seem to like this either.

Regardless of what I provide a message "Specified cast is not valid" is displayed.


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.