Is it possible to use `QueryOver<T>` to query all `IUserType` items?

19 views
Skip to first unread message

Mike

unread,
Feb 10, 2012, 11:32:12 AM2/10/12
to nhusers
I implemented GenericWellKnownInstanceType from the unoficial
NHibernate addins and managed to get my custom user type persisted
without any issues. What I'm struggling with is retrieving all data
from my new type.

The query I'm using is as simple as it gets. It returns no errors and
no data.

var states = Session.QueryOver<State>().List();

How can I retrieve all states in the system?

I've looked at the addins tests and none of them test this scenario so
maybe this isn't possible?

Here is the rest of my code:

[Serializable]
public class State
{
public State(int id, string name)
{
Id = id;
Name = name;
}

public int Id { get; set; }
public string Name { get; set; }
}

public class States : ReadOnlyCollection<State>
{
public static State FirstState = new State(0, "First State");
public static State SecondState = new State(1, "Second State");

public States()
: base(new [] { FirstState, SecondState })
{
}
}

public class StateUserType : GenericWellKnownInstanceType<State, int>
{
public StateUserType()
: base(new States(),
(entity, id) => entity.Id == id,
entity => entity.Id)
{
}

public override SqlType[] SqlTypes
{
get { return new[] { SqlTypeFactory.Int16 }; }
}
}

* Note that this is a cross post with SO.

Oskar Berggren

unread,
Feb 10, 2012, 12:02:02 PM2/10/12
to nhu...@googlegroups.com
2012/2/10 Mike <michael...@hydroone.com>:

> I implemented GenericWellKnownInstanceType from the unoficial
> NHibernate addins and managed to get my custom user type persisted
> without any issues. What I'm struggling with is retrieving all data
> from my new type.
>
> The query I'm using is as simple as it gets. It returns no errors and
> no data.
>
> var states = Session.QueryOver<State>().List();

But your state is not an entity, despite the id.

If the instances are well known, why go to the database to fetch them?
You already have them in States collection that you have already
written.


/Oskar

Message has been deleted

Mike

unread,
Feb 10, 2012, 1:11:14 PM2/10/12
to nhusers
Right, hadn't thought of that. Guess I was a little thrown off by
NHibernate not throwing an Exception.

Turns out I can simply new up the States object or make `public static
IEnumerable<State> GetAll() { return new States(); }` to retrieve the
items.

Thank you. :)
On Feb 10, 12:02 pm, Oskar Berggren <oskar.bergg...@gmail.com> wrote:
> 2012/2/10 Mike <michael.came...@hydroone.com>:
Reply all
Reply to author
Forward
0 new messages