nHibernate 4.1.4 is not quoting table name that matches reserved keyword - nhibernate

I'm using nHibernate 4.1.4 MappingByCode. My Dialect is
public class Dialect : NHibernate.Dialect.MsSql2012Dialect
{
protected override void RegisterKeywords()
{
base.RegisterKeywords();
RegisterKeyword("user");
}
}
My config is setting
config.SetProperty(Environment.Hbm2ddlKeyWords, "keywords");
I have a table named User.
SQL error is thrown that invalid table name User is invalid. nHibernate fails to wrap it in brackets.
Any ideas?

Keywords are used for different purpose...
what we need is ad hoc table name escaping
5.3. SQL quoted identifiers
You may force NHibernate to quote an identifier in the generated SQL
by enclosing the table or column name in back-ticks in the mapping
document. NHibernate will use the correct quotation style for the SQL
Dialect (usually double quotes, but brackets for SQL Server and
back-ticks for MySQL).
<class name="LineItem" table="`Line Item`">
<id name="Id" column="`Item Id`"/><generator class="assigned"/></id>
<property name="ItemNumber" column="`Item #`"/>
...
</class>
I.e. in mapping, we need to escape table name:
"`user`"
Keywords, defined in configuration of the dialect, would help NH parser when working with custom sql statements. E.g. in formulas, subselects.

Related

Coldfusion ORM Macromedia][Oracle JDBC Driver][Oracle]ORA-02289: sequence does not exist

I have a ColdFusion ORM application that uses an oracle sequence as the generator for the primary key field. I have verified that I can access the sequence as the user that ColdFusion is connected to the database as in SQL navigator.
My problem is I am getting the following error when I attempt to saveEntity() on any object
Root cause :java.sql.SQLException: [Macromedia][Oracle JDBC Driver][Oracle]ORA-02289: sequence does not exist
I have tried the syntax below with and without the akc. prefix. I do have the akc schema defined in the CFC
<cfproperty name="KEY_BREED_PAGE" fieldtype="id" generator="sequence" params="{sequence='akc.seq_breed_page_display'}" />
and
<cfproperty name="KEY_BREED_PAGE" fieldtype="id" generator="sequence" sequence="akc.seq_breed_page_display" />
Any ideas on what I can do to resolve this? The ORM works fine when updating but fails on every attempt to create a new record.
I had a similar when I didn't have the schema. The only difference between your example and mine is that I have the column attribute set.
<cfproperty name="KEY_BREED_PAGE" fieldtype="id" column="KEY_BREED_PAGE" generator="sequence" sequence="akc.seq_breed_page_display" />

Querying by property of mapped subclass in NHibernate

I'm very new to NHibernate so this may be fairly trivial, but searching is leaving me confused.
I have an AddOnAmount table as follows:
AddOnID | AddOnTypeID | Period | Amount
where AddOnTypeID is a FK. The rows have a unique constraint on AddOnTypeID and Period.
The mapping looks like this:
<id name="Id" column="AddOnId" type="long">
<generator class="native" />
</id>
<many-to-one name="AddOnType" column="AddOnTypeID" class="AddOnTypeStatic" not-null="true" />
<property name="Period" />
etc.
The AddOnTypeStatic class/table just has an Id, which is the numerical value stored on the table, and a descriptive Name.
I'm trying to write a query that will search by AddOnTypeId and Period, so I can validate the existence (or not) of a row before attempting to add a duplicate from my front end, but I'm not sure how to do that as the AddOnAmountStatic class has a subclass whereas the table has just an Id.
So far I've written:
public AddOnAmountStatic FindByAddOnTypeAndPeriod(long addOnType, string period)
{
return FindOne(CreateCriteria()
.Add(Restrictions.Eq("AddOnTypeId", addOnType))
.Add(Restrictions.Eq("Period", period))
.SetCacheable(true));
}
which does not work, as AddOnTypeId isn't a property of AddOnAmountStatic. Not sure how to access the property of the subclass in this context.
My mapping works, as I've been using it so far with no problems.
Solved my problem - it was simple but thought I'd add the solution here in case it helps anyone else.
I'd been thinking of constructing the query from the table's perspective (i.e., with the AddOnTypeID), whereas what I should have done is look at it from the entity's perspective. In other words, I just needed to pass in an AddOnTypeStatic object.
What I did was take my AddOnTypeID parameter, check it exists through NHibernate (returning either an AddOnTypeStatic object or null) then passed that through to the original query. Final query is simply
return FindOne(CreateCriteria()
.Add(Restrictions.Eq("AddOnType", addOnType))
.(Restrictions.Eq("Period", period))
.SetCacheable(true));
Hope this helps another newbie!

FluentNHibernate CustomType("Binary") MappingException

I have an issue with mapping an entity with the latest FluentNHibernate build available on NuGet (package version: 1.1.1.694) and NHibernate 3.0 GA
What I am trying to reach is sql type: binary(64) with FluentNHibernate in a database-agnostic manner (I don't want to use CustomSqlType).
The default is varbinary(64) which I don't want. Lowercase "binary" leads to this as well.
My mapping code:
this.Map(x => x.PasswordHash)
.CustomType("Binary")
.Length(64)
.Not.Nullable();
Gives in NHibernate mapping XML file:
<property name="PasswordHash" type="Binary">
<column name="PasswordHash" length="64" not-null="true" />
</property>
Exception on generating schema:
Could not load type Binary.
System.TypeLoadException: Could not load type Binary. Possible cause: no assembly name specified.
at NHibernate.Util.ReflectHelper.TypeFromAssembly(AssemblyQualifiedTypeName name, Boolean throwOnError)
On the other hand CustomType("StringClob") works. Is there something I am missing ?
Is there a way to make FluentNHibernate .CustomType<> work with built-in NHibernate types ?
(Useful for AnsiChar, or other non-standard mapping between .NET type and database type) ?
I believe you have to change the sql-type, not the type (fluent syntax is probably .SqlType("binary") or something like that)

Coldfusion ORM 9.0.1 - Error while resolving relationship

I got this example from the adobe coldfusion documentation, some of the names are changed but everything else is the same, unless I am just so frustrated that I have missed a letter.
user.cfc:
/**
*#persistent
*/
component
{
property name="id" fieldtype="id" generator="native";
property name="userName" type="string" length="100";
property name="Credential" fieldtype="one-to-one" cfc="model.user.credentials";
}
credentials.cfc:
/**
*#persistent
*/
component
{
property name="id" fieldtype="id" generator="foreign" params="{property='userinfo'}";
property name="userinfo" fieldtype="one-to-one" cfc="model.user.user" constrained="true";
property name="passwordHash" type="string";
}
no matter how I word it, after searching many sites, I still get a error of:
Error while resolving the relationship Credential in cfc user. Check the column mapping for this property.
I have checked that both cfcs are accessible by coldfusion by removing the one-to-one properties and the tables have been created successfully.
I am using SQL Server 2008 with Coldfusion 9.0.1 under Apache 2.2 web server.
I am new to ORM and Hibernate but have successfully created different types of relationships and will confess to a less then expert level of coldfusion.
Thanks, this is really bothering me as this came directly from the coldfusion documentation.
Do you have a mapping for model?
If not, add one, or you could try:
property name="Credential" fieldtype="one-to-one" cfc="credentials";

Automatically truncating strings in NHibernate / SQL Server

I have a nvarchar(2000) column in a SQL Server 2005 database, and have mapped this into NHibernate as:
<property name="Query" column="`Query`" type="String" length="2000" not-null="false"/>
The DTO class just presents a string property:
public virtual string Query { get; set; }
If I set the query to a string of > 2000 characters I get an exception from SQL server to the effect of:
"String or binary data would be
truncated. The statement has been
terminated."
What I want is for this truncation to just happen automatically and silently. I could override the virtual property and force the truncation on the property set, but feel there should be a way to get this behaviour by default, either via the NHibernate mapping or even as a SQL server setting.
Am I missing anything ... I don't want to change the db schema to allow longer strings.
Create a custom User Type and which truncates the string if it's longer than 2000 chars. Here is sample of creating User Type
<property name="Query" type="Common.Nhibernate.Types.StringTruncType, Common" column="`Query`" type="String" length="2000" not-null="false"/>