generated NH fluent mapping column names

Posts   
 
    
fpdave100
User
Posts: 97
Joined: 06-Feb-2012
# Posted on: 08-Feb-2012 10:42:21   

the generated code contains column names surrounded by square brackets, eg column="[Id]"

eg:

Map(x=>x.Budget).Column("[Budget]")

in general this works, but causes problems when using NH3.2's support for SQL stored procedures as thecolumn names typically dont come back with [ ] around them in the returned result set, so I'm getting "ordinal not found errors".

I notice that most NH fluent mappings & plain old NH xml mapping samples (eg from the NH 3 cookbook) dont specify any square brackets, eg:

public MovieMapping()
{
Map(m => m.Director);
HasMany(m => m.Actors)
.KeyColumn("MovieId")
.AsList(l => l.Column("ActorIndex"));
}
<bag name="Actors">
<key column="MovieId"/>
<one-to-many class="ActorRole"/>
</bag>

is there a setting to disable this?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Feb-2012 15:02:10   

in general this works, but causes problems when using NH3.2's support for SQL stored procedures

We don't support Stored Procedures on NHibernate, so I don't understand how the generated square brackets are causing issues?

fpdave100
User
Posts: 97
Joined: 06-Feb-2012
# Posted on: 08-Feb-2012 15:08:48   

Walaa wrote:

in general this works, but causes problems when using NH3.2's support for SQL stored procedures

We don't support Stored Procedures on NHibernate, so I don't understand how the generated square brackets are causing issues?

They cause a problem when I write my own data access code using an SP, which uses the entities mapped by LLBLGEN using fluent NH.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39898
Joined: 17-Aug-2003
# Posted on: 08-Feb-2012 17:02:57   

The names are pre-escaped because they can contain spaces and without the brackets it might not work properly. I do recall NH3.0 not working without brackets when a space or other invalid char was in the name.

Could you give an example of how you utilize the mappings to entities for proc calls? As that's unclear to us.

Frans Bouma | Lead developer LLBLGen Pro
fpdave100
User
Posts: 97
Joined: 06-Feb-2012
# Posted on: 08-Feb-2012 17:10:12   

Otis wrote:

The names are pre-escaped because they can contain spaces and without the brackets it might not work properly. I do recall NH3.0 not working without brackets when a space or other invalid char was in the name.

Could you give an example of how you utilize the mappings to entities for proc calls? As that's unclear to us.


            ISQLQuery valuesqry = session.CreateSQLQuery(
                "exec budgets_GetBudgtValuesByMajorPeriod @budgetTypeID=:budgetTypeID, @budgetKeyID=:budgetKeyID, @bookingPeriodID=:bookingPeriodID,@allMajorPeriod=:allMajorPeriod")
                .AddEntity(typeof(BudgetValue));


            IList<BudgetValue> budgetValues = valuesqry
                        .SetInt32("budgetTypeID", request.BudgetType.Id)
                        .SetInt32("budgetKeyID", request.BookingKey.Id)
                        .SetInt32("bookingPeriodID", request.Period.Id)
                        .SetBoolean("allMajorPeriod", true).List<BudgetValue>();

This is actually used in a multiquery, but I get the same problem in a normal query as above.

A simpler example would be:

            ISQLQuery valuesqry = session.CreateSQLQuery(
                "SELECT * FROM MyEntity")
                .AddEntity(typeof(MyEntity)); 

            IList<MyEntity> ents= valuesqry.List<BudgetValue>
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39898
Joined: 17-Aug-2003
# Posted on: 09-Feb-2012 12:22:01   

Hmm, so NHibernate simply doesn't check for quote characters. disappointed When we don't add them, they don't add them (bad) and when we do add them, they don't check (bad).

Anyway, as we can't fix this as it will affect other users, I can only suggest a workaround for you. Please follow the following steps. This defines a copy of the template you're going to change and 'override' the original binding so it will use your copy instead of the original one. This makes it easier to keep the changes when you download a new version.

  • Create a copy of <llblgen pro installation folder>\Frameworks\NHibernate\Templates\Shared\Shared\generalTemplateUtils.lpt
  • Open that copy in the llblgen pro designer, by simply dragging it onto it.
  • Go to line 458, and remove the [] characters.
  • Save

You now have a copy of a template. To make the designer use it, create a new templatebindings file with the following contents:


<?xml version="1.0" encoding="utf-8"?>
<templateBindings name="SD.NHibernate.Custom" description="Custom Template bindings for the templates specific for NHibernate support for LLBLGen Pro" precedenceLevel="11" xmlns="http://sd/llblgen/pro/templateBindingsDefinition.xsd">
    <supportedPlatforms>
        <platform name=".NET 2.0"/>
        <platform name=".NET 3.0"/>
        <platform name=".NET 3.5"/>
        <platform name=".NET 4.0"/>
    </supportedPlatforms>
    <supportedFrameworks>
        <framework name="NHibernate"/>
    </supportedFrameworks>
    <supportedDrivers>
        <driver id="2D18D138-1DD2-467E-86CC-4838250611AE"/> <!-- SQL Server -->
        <driver id="3FABDE1A-21DF-4fcb-96FD-BBFA8F18B1EA"/> <!-- Sybase iAnywhere / ASA -->
        <driver id="758A392F-06F3-498b-AED9-D85A4C795BDA"/> <!-- MySql -->
        <driver id="88EBFD8C-CBDD-4452-88AF-1C99E41A123F"/> <!-- PostgreSql -->
        <driver id="A3076322-977C-4e28-BFF4-F25ED096D1DB"/> <!-- Sybase Advanced Server Enterprise / ASE -->
        <driver id="A8034CB0-319D-4cdd-BC7D-1E7CFDBA3B74"/> <!-- ODP.NET Oracle -->
        <driver id="BB438EBA-A0B5-4236-A2B8-64D828A138AF"/> <!-- DB2 -->
        <driver id="BCF16BE3-7E29-4E07-AC27-9FBF79D3CA7A"/> <!-- Firebird -->
        <driver id="DE158D99-1B10-4d88-8DEA-888E7930FF40"/> <!-- MS Oracle -->
    </supportedDrivers>
    <language name="C#">
        <templateBinding templateID="SD_NHibernateGeneralUtils" filename="Shared\Shared\YourCopyOfGeneralTemplateUtils.lpt" templateLanguage="C#"/>
        </language>
</templateBindings>

place this file in the folder <llblgen pro installation folder>\Frameworks\NHibernate\Templates

In the xml above, you have to change the name of YourCopyOfGeneralTemplateUtils to the real name you gave to the copy of the generalTemplateUtils.lpt file of course.

Re-start the designer. When generating code, by pressing F7, go to tab 2 in the dialog. You'll see the templatebindings file 'SD.NHibernate.Custom' be placed above the SD.NHibernate' templatebindings file. If not, move it above it with the up button.

Generate code. Your mappings should now not have [] characters around column names.

You can also place the custom templatebindings in another folder (you then have to adjust the local path in the templatebindings file of course, as it's relative to the templatebindings file) and specify that folder in the project properties in the designer (additional templates folder).

If you run into problems with this, please let us know and we'll help you further.

Frans Bouma | Lead developer LLBLGen Pro