Fluent NHibernate - bind List<int> - fluent-nhibernate

I have a Class which looks something like this:
public class User {
public virtual int ID;
public virtual string Name;
public virtual IList<int> userRights;
}
I want to make a UserMap : ClassMap<User>
Mapping the name is no problem however i cant seem to figure out how to map the userRights.
Table looks like
UserTable
User_id int
User_Name nvarchar
User_group int
UserRights
User_group int
RightID int
How would you map this ?

Well if you want a List you need an index. So I would recommend just making it an ICollection unless the ordering is significant.
The mapping should look something like:
HasMany(x=> x.userRights).Element("RightID").AsBag();
However, upon looking at your tables, I noticed something odd. You're trying to use a one-to-many without having the primary key in the User_Rights table. If you had User_Id in UserRights the above should work.
Otherwise it looks like there's a UserGroup, which should be modeled by your classes.

Related

GORM One-to-Many relationship without foreign keys

I've been trying for a while to understand how GORM works, and I think I've got it down. However, I'm having a heck of a time getting my relationships correct.
Here is my database schema setup (2 tables):
Table - Users
userhash varchar(255)
firstname varchar(255)
lastname varchar(255)
Table - Logs
userhash varchar(255)
accessdate date
There are no foreign key and primary key constraints defined in the tables. However, in the users table the userhash will be unique. I didn't design it this way, but I have to use it like this.
Now for my Grails domain classes:
Class - Users
class Users {
String userhash;
String firstname;
String lastname;
static hasMany = [logs: Logs]
}
Class - Logs
class Logs {
String userhash;
Date accessdate;
static belongsTo = Users;
}
In my controller, I do the following:
def user = Users.findByUserhash("nraboy");
println user.firstname;
println user.logs;
It prints out the correct firstname, but when I try to display the logs, it is null or empty. Am I requesting the data incorrectly for child tables or am I missing something somewhere in the domain class design?
I've tried to do the following, but had now luck as well:
Class - Logs
static mapping = {
id generator: "assigned", name: "userhash", type: "string"
}
Class - Users
static mapping = {
userhash generator: "foreign"
}
I figured the above would let me manually define the primary key and foreign key via code since it didn't exist int he tables. No luck though.
Any help would be appreciated.
Thanks,
It looks like you may have to do something with in Users for the logs association to force the foreign key to be in the Logs table, as it is. See the GORM docs One-to-Many Mapping section. Perhaps something like:
static hasMany = [logs: Logs]
static mapping = {
logs column: 'userhash'
}
I would be curious to hear if this worked...

Eager loading an optional one-to-one with NHibernate

Consider the following simplified domain:
public class Movie
{
public virtual int Id { get; set; }
public virtual MovieDetail MovieDetail { get; set; }
}
public class MovieDetail
{
public virtual int Id { get; set; }
public virtual Movie Movie { get; set; }
}
A MovieDetail cannot exist without a Movie, but a Movie could exist without a MovieDetail (i.e. we have no details about it).
Our database has a separate table for Movie with columns Id, and a separate table for MovieDetail with columns Id and MovieId. There is also a foreign key from MovieDetail.MovieId to Movie.Id.
We've got this all mapped in NHibernate, but when getting a collection of Movie instances, we want a left outer join with MovieDetail. If not, we could have a N+1 problem when iterating over the Movie instances. That is the case now: there is a separate query for every call to the Movie.MovieDetail property.
I've tried one-to-one mapping, but that seems to be for the case when you have both instances. In our case, we don't always have a MovieDetail. Also, they don't share the same primary key.
I've researched formula's, but that would require me to make my MovieDetail implement IUserType, essentially putting NHibernate into my domain. I'd like to avoid that.
Maybe you could try adding a many-to-one relation in the Movie mapping to MovieDetail, it will act as a one to one mapping.
When you set the option 'not-null' to "false" it is also nullable I suppose.
I don't know if you are lazy loading or not, when this is so the MovieDetailis loaded when needed and not by a left join construction.
Shouldn't all the properties be virtual in both classes?
<many-to-one name="MovieDetail" column="Id" class="MovieDetail" not-null="false" lazy="false"/>
I'm in a bit of a hurry and I don't know if you can modify your domain / db schema but you might want to try and take a look at http://ayende.com/blog/3937/nhibernate-mapping-component.
It seems to me that a Movie can have at most one MovieDetail which might not be there. MovieDetail might have properties like Description, ReleaseDate, Actors, etc. I don't really understand why you separated these concepts. By bringing them together you would have 1 less table and 1 less FK to join on each time you want to list movies.
The component allows you to isolate your data into a separate entity while mapping to the same table as Movie.

NHibernate 3.2 mapping by convention generating an unnecessary foreign key column

I have a couple of classes that look like this:
public class Client
{
public int Id {get;set;}
public string Name {get;set;}
}
public class User
{
public int Id {get;set;}
public string Email {get;set;}
public Client Client {get;set;}
}
I'm using ConventionModelMapper and SchemaUpdate from NHibernate 3.2 to generate the schema in my SQL Server database and I want the Client property of the User class to be mapped to a ClientId column with foreign key. My convention code looks like this:
mapper.AfterMapManyToOne += (inspector, member, map) =>
{
map.Column(member.LocalMember.Name + "Id");
// ...
};
This works, in that I get a column ClientId that is mapped as a foreign key, but I also end up with a Client column that is also mapped as a foreign key. It seems that NHibernate is treating the Client property as both a standard Property (and thus generating the Client column for it), and also a ManyToOne property (resulting in the additional ClientId column). How can I prevent the Client column from being generated
I have just copied your EXACT code and, after making the properties virtual, the behavior is the expected (there's a single column, ClientId)

How to bind a series of columns into a collection with NHibernate

Please believe me when I say I understand this isn't the "right" way to do this. File this under large-complex-legacy-system questions.
We have tables describing OCR'd data of documents. Each document type has its own table. Each table is generated based on the types of fields it expects. So have a schema which looks a bit like this:
Table: DocumentTypes
Field: Id
Field: Prefix
Table: DocumentFields
Field: Id
Field: DocId
Field: Name
And we would generate tables like:
Table: Type1_Data_Unit1000
Field: Id
Field: DocId
Field: DocField_A_Data
Field: DocField_A_Info1
Field: DocField_A_Info2
Field: DocField_Z_Data
Field: DocField_Z_Info1
Field: DocField_Z_Info2
NHibernate works well for all of our other data because the schema is more static.
My question: Is it at all possible to configure nhibernate to load one of the DataTables and bind the series of fields into a collection? If it is, at what interfaces should I start looking?
My idea is to have a class which is something like:
class FormData
{
public virtual int Id {get;set;}
public virtual int DocId {get;set;}
public virtual int Id {get;set;}
public virtual IList(Of FormFieldData) {get;private set;}
}
class FormFieldData
{
public virtual int Id {get;set;}
public virtual string Value {get;set;}
public virtual int Info1 {get;set;}
public virtual int Info2 {get;set;}
}
I've looked at "IInterceptor" a little bit and think that's where I should look first. But before investing days in it, I wanted to run it by the SO crowd.
Thanks!
If the schema isn't static, then it'll be hard for nhibernate to even create the query - and intercepters can't help you there as far as I'm concerned. I'd go with pure CreateSQLQuery and manipulating the returned values manually afterwards.

Joins in fluent nhibernate

I am using fluent nhibernate.
example:
i have 3 tables i.e.
CUSTOMER
CustomerId pk
CustomerName
PRODUCT
ProductId pk
ProductName
Cust_Product
cust_prodId pk
ProductId fk
CustomerId fk
Now, I want to show customername, productnae
so, how do i write mapping class for the same.
i want to use
session.CreateCriteria(typeof("className")).List()
like this. how do i do this..?
If you're looking for a full tutorial on how to do this, I recommend the FNH wiki or one of the many blog postings which can be found through Google.
However, you're trying to implement a many-to-many relationship here, and that seems to throw a lot of people off. Here's a rough guide:
On your Customer class, you'll need a collection like:
IList<Product> Products { get; private set; }
And similarly, on your Product class:
IList<Customers> Customers { get; private set; }
You start off a many-to-many map with the HasManyToMany function:
public class CustomerMap : ClassMap<Customer>
{
public CustomerMap()
{
// other mappings
HasManyToMany<Product>(x => x.Products)
.WithTableName("Cust_Product") // Specifies the join table name
.WithParentKeyColumn("CustomerId") // Specifies the key joining back to this table (defaults to [class]_id, Customer_id in this case)
.WithChildKeyColumn("ProductId")
.FetchType.Join(); // Instructs NHibernate to use a join instead of sequential select
}
}
Then repeat the process for the other side of the relationship (the Customers property on the Product class).