i have an sql query that has one unnamed column as a list of strings.
my hbm is declared as follows:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Services.Data" namespace="Services.Data" >
<sql-query name="GetDiagramSubscriptions">
exec Diagram_Subscriptions:contactId
</sql-query>
</hibernate-mapping>
my repository method:
IQuery query = Session.GetNamedQuery("GetDiagramSubscriptions")
.SetInt32("contactId", contactId)
.SetResultTransformer(Transformers.AliasToBean<string>());
return query.List<string>();
this doesn't work because type string doesn't have a setter.
i don't want to declare a mapping class just for this one column. is there a way to transform this to a Tuple or something?
just remove this call .SetResultTransformer(Transformers.AliasToBean<string>()) and the List<string>() will do what you expect.
Related
Can any body tell me what is wrong with below query Named SQL Query using NHibernate.
As I am getting error - "Error in Named Query : GetSecondReviewIncomeStatements ":
<sql-query name="GetSecondReviewIncomeStatements" read-only="true">
<![CDATA[
SELECT I.TotalNetSales,I.CostOfGoodsSold,I.GrossProfit
FROM IncomeStatement as incomeStatement
INNER JOIN FETCH CompSearchResultItem as resultItem
ON incomeStatement.Comparable.ID = resultItem.Comparable.ID
AND resultItem.CompSearch.ID = :compSearchID
]]>-->
</sql-query>-->
I have searched a lot on the web, some say instead on On put Where.
I also tried NamedQuery only but everytime I get the same error.
Please check the Format for Named Query in .hbm.xml file.
in hbm.xml
<sql-query name="ShowProducts">
<return alias="Product" class="Product" />
exec ShowProducts
</sql-query>
<sql-query name="DeleteProducts">
<query-param name="Id" type="int"></query-param>
exec DeleteProducts #ID=:Id
</sql-query>
in Code Usage
IQuery query = (IQuery)session.GetNamedQuery("ShowProducts");
var listProducts = query.List<Product>();
GridView1.DataSource = listProducts;
GridView1.DataBind();
Does your SQL runs if you put it 'as-is' in your rdbms sql executor?
Besides the strange --> after the enclosing CDATA element, I've noticed that you haven't declared the I alias in your query.
I mean, these columns preceded with I...
SELECT I.TotalNetSales,I.CostOfGoodsSold,I.GrossProfit
...are coming from where?
Can anyone tell where to map Stored-Procedures exactly in Nhibernate.
In class hbm.xml file or newly declared file(hbm.xml) especially for Stored Procedures???
can u tell with reason??
I create a single XML file that contains ALL stored procedure calls, e.g.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<sql-query name="SummaryReport">
exec getSummaryReport :productId
</sql-query>
<sql-query name="FullReport">
exec getFullReport :productId
</sql-query>
</hibernate-mapping>
and mark this as an embedded resource. I can then call me SP like this:-
var results = Session
.GetNamedQuery("SummaryReport")
.SetInt32("productId", productId);
.SetResultTransformer(
new AliasToBeanResultTransformer(typeof(SummaryReport)));
return results.List<SummaryReport>();
This works fine for me but there really isn't a recommended way, its always down to what you feel is right for you.
I have an NHibernate mapping that makes use of a <database-object> element that makes use of a definition class. The mapping looks a bit like this:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Foo" assembly="Foo">
<database-object>
<definition class="Foo.AuxilliaryData,Foo"/>
</database-object>
</hibernate-mapping>
This works fine, but I would like to use mapping by code as per NHibernate >=3.2. Is there a way to do this?
(Out of interest, my reason for wanting to convert this is so that R# "Find usages" will work for the AuxilliaryData class. At the moment R# (and any other code inspection tool) thinks that this class is unused).
just add it when generating the Configuration object
config.AddAuxiliaryDatabaseObject(new Foo.AuxilliaryData());
I'm new to Nhibernate. My problem is that I want to narrow down a query by using a column that is not included in my entity (ie hbm). I want to do something like this:
Session.QueryOver<MyEntity>()
.SQL_Where("MyFlag = 1")
Since I have no use of that flag later I don't want to include it to the entity
I know I can use:
Session
.CreateSQLQuery("SELECT A,B,C FROM ENTITY WHERE MyFlag = 1")
.SetResultTransformer(Transformers.AliasToBean<MyEntity>())
.List<MyEntity>();
It would be nice to use QueryOver<>(), it's more safe if a column is added etc.
You may be able to use filters:-
Put a filter on your mappings class definition, however this will affect ALL returned rows
e.g.
<class name="Domain.Model.MyEntity, Domain.Model" table="MyTable"
where="(MyFlag=1)">
...
</class>
or it may be possible to use conditional filters with QueryOver
<filter-def name="SetMyFlag">
<filter-param name=":flag" type="System.Int"/>
</filter-def>
<class name="Domain.Model.MyEntity, Domain.Model" table="MyTable">
...
<filter name="SetMyFlag" condition="(MyFlag=:flag)"/>
</class>
and use:-
session.EnableFilter("SetMyFlag").SetParameter("flag", 1);
session.QueryOver<MyEntity>();
Although I have never use conditional filters with unmapped columns so this may not work!
I have read some post about fetch=join - http://nhforge.org/blogs/nhibernate/archive/2009/04/09/nhibernate-mapping-lt-many-to-one-gt.aspx (ser4ik.livejournal.com/2505.html)
So I have some question, Forexample I have class
<class name="AttributesInf" table="attr_inf">
<id name="Id">
<generator class="identity"/>
</id>
<property name="Name" />
<property name="Desc"/>
</class>
and
<class name="AttributeValue" table="attr_val">
<id name="Id">
<generator class="identity"/>
</id>
<property name="Value" />
<many-to-one name="AttrName" column="attrId"/>
</class>
If I use this mapping without set fetch="join" I get sql:
Select av.Id, av.Value, av.attrId From attr_val av where av.Id=...()
and after then separate sql queries like:
Select * From attr_inf where Id = av.attrId
So my Result Is:
class AttrinuteInf
{
int Id;
string Name;
string Desc;
}
class AttributeValue
{
int Id;
string Value;
AttributeInf AttrName;
}
If I set fetch="join" then I get one query:
Select u.Id, u.UserName, u.BlogId, b.Id, b.BlogName, b.BlogAuthor, b.BlogMsg
from Users u
Left outer join Blogs b
On u.BlogId=b.Id
Where u.Id = ...
So I expect to get one Class:
class AttributeValue
{
int Id;
string Value;
string Name;
string Desc;
}
But I have the same result as if I not set fetch to "join".
Is this all right?
Are there any way to get properties from class maped as <many-to-one> directly?
(not as AttrName.Name, but simply Name )
Explanation:
Part of mapping set above don't show my real problem.
I want to map some entity as IDictionary<string,AttributeValue>.
I map it as
<map name="Attributes" table="attr_val" lazy="true" cascade="all-delete-orphan" inverse="true">
<key column="item_id"></key>
<index column="name"></index> //I don't have that item in class AttributeValue, that is why I try to get it from other table
<one-to-many class="AttributeValue"/>
</map>
This isn't doing what you think it's doing. Using fetch=join is just there to eager load the many side of the relationship. In both cases you end up with the same objects returned. By default NHibernate will lazy load the related entities (which is why you get the second query). By using fetch=join you are asking for the entire object relationship at once but it will still populate the objects the same as without the fetch=join.
It's not the way you describe it. Your entities do not change depending on what you request.
You will obtain a list of instances of your main entity, with the association to the other being fetched. So if, in your code, you access the association, you will find the values.
If you don't fetch it, you wouldn't be able to access those fields, as they would not have been retrieved from the database.
I don't understand your question.
The 'fetch-join' attribute just defines how the sql that NHibernate generates to retrieve instances of your classes, should look like.
It has nothing to do with 'what' will be returned. NHibernate will translate the recordset (that is the result of the query) to correct instances of your classes.
If you just want to retrieve parts of your entity (like the name for instance), then you'll have to write a HQL query, or use the ICriteria API and maybe use projections.