EzDevInfo.com

fluent-nhibernate interview questions

Top fluent-nhibernate frequently asked interview questions

How do you map an enum as an int value with fluent NHibernate?

Question says it all really, the default is for it to map as a string but I need it to map as an int.

I'm currently using PersistenceModel for setting my conventions if that makes any difference. Thanks in advance.

Update Found that getting onto the latest version of the code from the trunk resolved my woes.


Source: (StackOverflow)

Override for fluent NHibernate for long text strings nvarchar(MAX) not nvarchar(255)

When ever you set a string value in fluent NHibernate it alwasy sets the DB vales to Nvarchar(255), I need to store quite a lot of long string which are based on user inputs and 255 is impractical.

Just to add this is an issue with the automapper as I am using fluent NHibernate to build the database.


Source: (StackOverflow)

Advertisements

How can I have NHibernate only generate the SQL without executing it?

I know how to log the SQL to log4net/NLog/trace window at runtime with the show_sql configuration option.

What I'm looking for is a way to give a Query<T>() to NHibernate retrieve the generated SQL.

I've looked through the Persister class, the Drivers, different Interceptors and Events. There are so many places to look, even narrowing down my search would be of great help.


Source: (StackOverflow)

Generate table indexes using Fluent NHibernate

Is it possible to generate table indexes along with the rest of the database schema with Fluent NHibernate? I would like to be able to generate the complete database DDL via an automated build process.


Source: (StackOverflow)

how to set generate_statistics = true with fluent nhibernate

From what I understand I need to end up with this

<property name="hibernate.generate_statistics">true</property>

on the session factory configuration, but I've no idea how to do that with fluent nhibernate.


Source: (StackOverflow)

How to specify table name in Fluent NHibernate ClassMap class?

Hi I am newbie to NHibernate and trying to use Fluent for mapping. My entity class name is different from the database table name it has to be mapped to. I am using mapping class derived from ClassMap<>, but I can't specify the table name: the property TableName from ClassMap is read-only.

Thanks for your help.


Source: (StackOverflow)

NHibernate or Fluent NHibernate? [closed]

I would be interested in hearing op opinions from others regarding whether which they would choose (no 'neithers' please ;), and why.

What are the downsides to using fluent? (version dependancy maybe?) Pros, Cons, Experiences etc.


Source: (StackOverflow)

NHibernate L2 Cache configuration in Fluent NHibernate

Is ti possible to configure the L2 cache provider in code via FHN?

Adding a line to the following config is what I'm after:

 return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey("Temp")).ShowSql())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<IMap>())
                .ExposeConfiguration(c => { })
                .BuildSessionFactory();

Cheers

AWC


Source: (StackOverflow)

Mapping Composite keys in Fluent NHibernate

I am new to fluent NHibernate. Now I face one problem with mapping composite keys. Can anyone point out the URL or sample please?


Source: (StackOverflow)

Where can i find a Fluent NHibernate Tutorial?

I have googled and looked around does anyone know of any hidden gems out there that is not in first couple pages of a google search....


Source: (StackOverflow)

How to configure fluent nHibernate with MySQL

I'm trying to configure nHibernate to use a MySql database. I found examples for mssql and sqlite but none for mysql. So, how do I change this so it uses mysql:

Fluently.Configure().Database(
        MsSqlConfiguration.MsSql2005.ConnectionString(
            c => c.FromConnectionStringWithKey("ConnectionString")
        )
    )
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyAutofacModule>())
    .BuildSessionFactory())

Source: (StackOverflow)

NHibernate Eager loading multi-level child objects

I have a hierarchy of objects, Order, Contact, Address:

public class Order {
     public virtual Contact BillingContact { get; set; }
}

public class Contact {
     public virtual Address Address { get; set; }
}

I want to query an order by id, and eager load the billingcontact, along with it's address.

var criteria = DetachedCriteria.For<Order>()
     .SetFetchMode("BillingContact", FetchMode.Eager)

This criteria eager loads the BillingContact, but understandably not the address of the BillingContact. If i add:

     .SetFetchMode("BillingContact.Address", FetchMode.Eager)

This does nothing to help.

Also note that these relationships are unidirectional:

public OrderMap()
{
    References(x => x.BillingContact)
    	.Not.Nullable()
    	.Cascade.All();
}

public ContactMap()
{
    HasOne(x => x.Address)
    	.Cascade.All()
    	.FetchType.Join();
}

public AddressMap()
{
    Map(x => x.Address1);
}

How can I construct a criteria object that will load the child of the child? Do these relationship mappings seem correct?


Source: (StackOverflow)

Cascade Saves with Fluent NHibernate AutoMapping

How do I "turn on" cascading saves using AutoMap Persistence Model with Fluent NHibernate?

As in:

I Save the Person and the Arm should also be saved. Currently I get

"object references an unsaved transient instance - save the transient instance before flushing"

public class Person : DomainEntity
{
  public virtual Arm LeftArm { get; set; }
}

public class Arm : DomainEntity
{
  public virtual int Size { get; set; }
}

I found an article on this topic, but it seems to be outdated.


Source: (StackOverflow)

Map Enum as Int with Fluent NHibernate and NHibernate 3

I used this How do you map an enum as an int value with fluent NHibernate? to map in the past but I've recently upgraded to NHibernate 3 and this doesn't seem to work anymore. I've put breakpoints in my EnumConvention class and they're not being hit. The query that is hitting the database has the enum as a string which is the default configuration.

How does this work with NHibernate 3?

Update

Here is part of the mapping file that is generated:

<property name="ComponentType" type="FluentNHibernate.Mapping.GenericEnumMapper`1[[...ComponentType, ..., Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], FluentNHibernate, Version=1.1.0.0, Culture=neutral, PublicKeyToken=8aa435e3cb308880">
  <column name="ComponentTypeId" />
</property>

It doesn't seem right that it would be using a GenericEnumMapper when an IUserTypeConvention is specified for enums.

Here is my convention:

public class EnumConvention : IUserTypeConvention
{
    public void Accept( IAcceptanceCriteria<IPropertyInspector> criteria )
    {
        criteria.Expect( e => e.Property.PropertyType.IsEnum );
    }

    public void Apply( IPropertyInstance instance )
    {
        instance.CustomType( instance.Property.PropertyType );
    }
}

Source: (StackOverflow)

Fluent NHibernate Many-to-Many

I am using Fluent NHibernate and having some issues getting a many to many relationship setup with one of my classes. It's probably a stupid mistake but I've been stuck for a little bit trying to get it working. Anyways, I have a couple classes that have Many-Many relationships.

public class Person
{
    public Person()
    {
        GroupsOwned = new List<Groups>();
    }

    public virtual IList<Groups> GroupsOwned { get; set; }
}

public class Groups
{
    public Groups()
    {
        Admins= new List<Person>();
    }

    public virtual IList<Person> Admins{ get; set; }
}

With the mapping looking like this

Person: ...

HasManyToMany<Groups>(x => x.GroupsOwned)
    .WithTableName("GroupAdministrators")
    .WithParentKeyColumn("PersonID")
    .WithChildKeyColumn("GroupID")
    .Cascade.SaveUpdate();

Groups: ...

 HasManyToMany<Person>(x => x.Admins)
    .WithTableName("GroupAdministrators")
    .WithParentKeyColumn("GroupID")
    .WithChildKeyColumn("PersonID")
    .Cascade.SaveUpdate();

When I run my integration test, basically I'm creating a new person and group. Adding the Group to the Person.GroupsOwned. If I get the Person Object back from the repository, the GroupsOwned is equal to the initial group, however, when I get the group back if I check count on Group.Admins, the count is 0. The Join table has the GroupID and the PersonID saved in it.

Thanks for any advice you may have.


Source: (StackOverflow)