nhibernate

NHibernate ignores length attribute

99封情书 提交于 2020-01-04 04:04:10
问题 After running SQL Profiler, I realized that NHibernate was mapping strings to nvarchar(4000). I fixed it by specifying type=AnsiString and length=... in the hbm file. It is now generating varchar(8000) statements, and it is ignoring the length. How come?! hbm file: <property name="EmailAddress" column="EMAIL_ADDRESS" type="AnsiString" length="120" /> database field: [EMAIL_ADDRESS] [varchar](120) NULL, TIA 回答1: Actually in previous versions of nhibernate the check against length was

How to specify a “Like” on an integer column?

99封情书 提交于 2020-01-04 03:58:30
问题 This one has me stumped and thought I would pose it to the SO community for help. A user wants to select all orders that start with a certain ID, example: 123 would return 123 , 12345 , 1238790 , etc. ID is an int column however. I'm using nHibernate and my line currently is: criteria.Add(Restrictions.Eq("Id", itemId)); but that's only going to return me 123. I can do a Restrictions.Like, but that converts to a SQL LIKE clause and that won't work on an int col. Any ideas? EDIT: Sorry, the DB

Is a Person an aggregate root?

谁说我不能喝 提交于 2020-01-04 03:51:25
问题 Since all entities have a stamped of who created/modified the record, can we consider a Person entity an aggregate root to all entities? That is, all entities that references the Person will become a collection to Person, e.g. public class Person { public virtual int PersonId { get; set; } public virtual string Lastname { get; set; } public virtual IList<OrderHeader> CreatedOrders { get; set; } public virtual IList<OrderHeader> ModifiedOrders { get; set; } // Other entities that have a

NHibernate iStatelessSession returns duplicate parent instances on eager fetch

浪子不回头ぞ 提交于 2020-01-04 03:25:31
问题 I'm trying to get a root entity and eager fetch it's child entities. But because I'm using the IStatelessSession of NHibernate, it returns duplicates of the root entity for each child. Using an ISession, it would be solved with .TransformUsing(new DistinctRootEntityResultTransformer()) But for an IStatelessSession it's not. Basically it's about the code below, where there's just one instance of Parent, holding 3 Childs. var result = session.QueryOver<Parent>() .Fetch(i => i.Childs).Eager();

Can NHibernate's QueryOver syntax select MAX() of an SqlFunction?

自闭症网瘾萝莉.ら 提交于 2020-01-04 02:56:06
问题 I have registered an SQL function in my dialect subclass RegisterFunction("addseconds", new SQLFunctionTemplate(NHibernateUtil.Date, "dateadd(second, ?1, ?2)")); which can be used in queries like so var q = _session.QueryOver<Event>() .Select( Projections.SqlFunction( "addseconds", NHibernateUtil.Date, Projections.Property<Event>(x => x.DurationInSeconds), Projections.Property<Event>(x => x.StartTime))); producing the SQL SELECT dateadd(second, this_.DurationInSeconds, this_.StartTime) as y0_

Hibernate Criteria: Projecting Count with group by clause

坚强是说给别人听的谎言 提交于 2020-01-04 02:39:33
问题 I want to execute the following SQL select count(*) as myCount from user group by name; I came up with the following criteria for the same DetachedCriteria.ForClass(typeof(UserDTO)) .setProjections(Projections.ProjectionList() .Add(Projections.rowCount(),"myCount") .Add(Projections.groupProperty("this.name")); I get the result back as pair of the count and name,How can I get just the count from this. 回答1: I don't think you can do it with Criteria, but it's easy with HQL. It's exactly the same

Castle ActiveRecord Table name conflict

孤街浪徒 提交于 2020-01-04 02:34:07
问题 When you run into a reserved word like "User" in NHibernate you would just put single quotes around the offending text and nHibernate will surround the text with square brackets for querying. My question is how do you do the same thing using Castle.ActiveRecord? 回答1: Actually, the portable way to express this is using backticks, e.g.: [ActiveRecord("`User`")] class User {} From the NHibernate Column class: If a value is passed in that is wrapped by ` then NHibernate will Quote the column

Add to Nhibernate collection without initializing the collection

白昼怎懂夜的黑 提交于 2020-01-04 01:51:29
问题 I have an entity called a Meter (Think an electric meter). The meter has a collection of Data Points. Every 15 minutes or so a new data point is added to the meter. The meter has a collection of these data points. I would like to code this up such as Meter myMeter = session.get<Meter>(id) and then myMeter.AddDataPoint(point) then session.Update(myMeter) . Internally the Meter class has a collection ( IESI.ISet<DataPoint> ). However when adding to this collection I am thinking that NHibernate

Using NHibernate to execute DDL statements

谁说我不能喝 提交于 2020-01-03 20:57:10
问题 How can I execute a DDL statement via NHibernate? To be clear, I don't want to auto-generate my schema from my mapping files. My DDL is stored in plain text files along the lines of: CREATE TABLE Foo (Bar VARCHAR(10)) GO CREATE TABLE Hello (World INTEGER) GO I want to cycle through these in order and execute each of them. I could just open a SqlConnection and execute via a SqlCommand but I'd like to go through NHibernate if there is a nice way to do this. This is mainly because I want to

NHibernate - join without mapping

丶灬走出姿态 提交于 2020-01-03 19:18:36
问题 Is it possible to join two classes without specified mapping between them (using Criteria API)? I must join two classes and retrieve data from both but I can't mapping them. I know only foreign key SomeID in the first class and primary key ID in second. How to create Criteria to join them? Is it possible without mapping? Please, help, I'm really need it but I'm stuck. :/ PS I know 'any' mapping but I have 10 fields like SomeID. Creating any mappings for 10 fields only for creating joins is