Fluent Nhibernate Many-to-Many mapping with extra column, update - fluent-nhibernate

Can not update / insert data into the database
This is the tables I have:
Role (Id, Name, ...)
Privilege (Id, Name, ...)
RolePrivilege (Id, RoleId, PrivilegeId, IsAllowed)
I have map:
public class RoleWithPrivilegesEntityMap : ClassMap<RoleWithPrivilegesEntity>
{
public RoleWithPrivilegesEntityMap()
{
Table( "ROLE" );
Not.LazyLoad();
Id( x => x.Id, "ID" ).GeneratedBy.Identity();
Map( x => x.Name, "NAME" ).Not.Nullable().Length( 100 );
HasMany(x => x.RolePrivileges)
.Cascade.AllDeleteOrphan()
.Inverse()
.Fetch.Join().KeyColumn("RoleId");
}
}
public class PrivilegeEntityMap : ClassMap<PrivilegeEntity>
{
public PrivilegeEntityMap()
{
Table("PRIVILEGE");
Id(x => x.Id, "ID").GeneratedBy.Identity();
Map(x => x.Name, "NAME").Not.Nullable();
HasMany(x => x.RolePrivileges)
.Cascade.AllDeleteOrphan()
.Inverse()
.Fetch.Join().KeyColumn("PrivilegeId");
}
}
public class RolePrivilegeEntityMap : ClassMap<RolePrivilegeEntity>
{
public RolePrivilegeEntityMap()
{
Table("ROLEPRIVILEGE");
Id(x => x.Id, "R_ID").GeneratedBy.Identity();
Map(x => x.IsAllowed, "ISALLOWED").Not.Nullable();
References(x => x.Role)
.Not.Nullable()
.Column("R_ID");
References(x => x.Privilege)
.Not.Nullable()
.Column("P_ID");
}
}
I'm trying to update existing record
session.SaveOrUpdate(rolePrivilege)
database make insert into table RolePrivilege, but I need to at first delete record.
what am I doing wrong?

normally nothing will go to the database at all because you never call session.Flush() or transaction.Commit(). However with Id generation Identity you basicly force NHibernate to insert immediately hence you see the INSERT statement.
You can also get rid of the Id in RolePrivilegeEntity and form a compositeId over Role and Priviledge which would also enhance performance (no need to insert immediatly).

Related

NHibernate inserts twice to base class when saving subclass. Violates unique constraint

I am using fluent nhibernate with a legacy oracle db, and Devart Entity Developer to generate mapping and entity classes.
I have a base table Product, which has several subclasses, including Tour. When saving Tour, nhibernate issues 2 identical inserts to the Product table which violates PK unique constraint.
Product mapping is:
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Schema("CT_PRODUCTS");
Table("PRODUCT");
OptimisticLock.None();
LazyLoad();
CompositeId()
.KeyProperty(x => x.Season, set =>
{
set.Type("CT.DomainKernel.Enums.Season,CT.DomainKernel");
set.ColumnName("SEASON");
set.Access.Property();
})
.KeyProperty(x => x.ProductCode, set =>
{
set.ColumnName("PROD_CODE");
set.Length(10);
set.Access.Property();
});
Map(x => x.Name)
.Column("NAME")
.Access.Property()
.Generated.Never()
.CustomSqlType("VARCHAR2")
.Length(200);
HasOne(x => x.Tour)
.Class<Tour>()
.Access.Property()
.Cascade.SaveUpdate()
.LazyLoad();
}
}
Tour mapping is:
public class TourMap : SubclassMap<Tour>
{
public TourMap()
{
Schema("CT_PRODUCTS");
Table("TOUR");
LazyLoad();
KeyColumn("SEASON");
KeyColumn("PROD_CODE");
Map(x => x.Duration)
.Column("DURATION")
.Access.Property()
.Generated.Never()
.CustomSqlType("NUMBER")
.Not.Nullable()
.Precision(3);
HasOne(x => x.Product)
.Class<Product>()
.Access.Property()
.Cascade.SaveUpdate()
.LazyLoad()
.Constrained();
}
}
Tour Entity class:
public partial class Tour2 : Product
{
public virtual Product Product
{
get
{
return this._Product;
}
set
{
this._Product = value;
}
}
}
Any Ideas as to what is going wrong?
The solution to this was to Remove the Property references from Tour to Product, and from Product to Tour, which when thought about, make no sense anyway.

FluentNHibernate - How to map KeyProperty with db generated value?

I have this mapping:
public sealed class AcessoMap : ClassMap<Acesso>
{
public AcessoMap()
{
CompositeId()
.KeyReference(x => x.Associado)
.KeyProperty(x => x.DataHora, k => k.ColumnName("aceDtHor").Type("Timestamp"));
Map(x => x.IP, "aceEndIP");
Map(x => x.NumeroAcesso).Not.Nullable().Generated.Insert();
Map(x => x.DataAcessoAnterior).Not.Nullable().Generated.Insert();
Map(x => x.ServerVariables).LazyLoad().Generated.Insert();
}
}
How can I configure it to DataHora property use database generated value? (Current it have default value on db, that sets it to current timestamp)
Thank you
I don't know if this will work, but it's something to try. Map your timestamp column regularly, outside the CompositeId definition.
Map(x=>x.RecordVersion).Column("aceDtHor")
.CustomSqlType( "timestamp" )
.Not.Nullable()
.CustomType( "BinaryBlob" )
.Generated.Always();

Fluent NHibernate Uni-Directional one to one mapping

I cant get at One-to-One relationship working with Fluent NHibernate. I have User and UserDetails tables and they 'share' a primary key. How do I map them?
This is my latest/best attempt, this fails with NHibernate.Id.IdentifierGenerationException: attempted to assign id from null one-to-one property: User
User
protected UserMap()
{
Table("user");
Id(x => x.Id)
.Column("user_key")
.GeneratedBy.GuidComb().UnsavedValue(Guid.Empty);
References(x => x.UserDetail)
.PropertyRef(x=>x.User)
.Column("user_key")
.Not.Insert().Not.Update().Cascade.All();
}
UserDetail
protected UserDetailMap()
{
Table("user_detail");
Id(x => x.Id).Column("user_key")
.GeneratedBy.Foreign("User")
.UnsavedValue(Guid.Empty);
References(x => x.User)
.Column("user_key")
.Not.Insert().Not.Update().Unique().Cascade.None();
}
Try this
protected UserDetailMap()
{
Table("user_detail");
Id(x => x.Id)
.Column("user_key")
.GeneratedBy.Foreign("User")
.UnsavedValue(Guid.Empty);
HasOne(x => x.User)
.Constrained();
}

How do you make non foreign key constraints in NHibernate?

I am using fluent nhibernate(v1.2) and nhibernate(v3.1) and I am having a weird Problem.
I have this
public class GradeMap : ClassMap<Grade>
{
public GradeMap()
{
Id(x => x.GradeId);
Map(x => x.TaskName).NvarcharWithMaxSize().Not.Nullable();
Map(x => x.Weight).Not.Nullable().Precision(5).Scale(2);
Map(x => x.OutOf).Not.Nullable().Precision(5).Scale(2);
Map(x => x.Mark).Not.Nullable().Precision(5).Scale(2);
Map(x => x.CourseBackgroundColor).Not.Nullable();
Map(x => x.CoursePrefix).Not.Nullable();
References(x => x.Student).Not.Nullable();
References(x => x.Course);
}
}
public class CourseMap : ClassMap<Course>
{
public CourseMap()
{
Id(x => x.Id).Column("CourseId");
Map(x => x.Prefix).NvarcharWithMaxSize().Not.Nullable();
HasMany(x => x.Tasks).Cascade.Delete().Inverse();
HasMany(x => x.CoursePermissions).Cascade.All().Inverse();
HasMany(x => x.CourseSharing).Cascade.All().Inverse();
HasMany(x => x.Grades).Cascade.None().Inverse();
}
}
I then do something like this
return session.Query<Grade>().ToList();
If I would try to grab a propety such as Grade.Course.Id it would crash and I would get.
Grade.Course = {Castle.Proxies.CourseProxy}
Grade.Course.Id = '((new System.Collections.Generic.Mscorlib_CollectionDebugView<OnlGrade>(grades)).Items[0].Course).Id' threw an exception of type 'NHibernate.ObjectNotFoundException'
Grade.Course.Prefix = above error except .Prefix instead of .Id
I would have thought Course Object would be empty or null. Not that it would have some Proxy with all properties throwing exceptions.
Edit
I found this posting but I don't have this attribute anymore so maybe they got rid of it or moved it.
Anyone know?
Is it possible to avoid NHibernate.ObjectNotFoundException when there is a foreign key but the referenced row does not exist?
I think the error here is because you're accessing the Id property of an object which does not exist.
Do you get the same error if you do TableA.TableB ?
What about NotFound.Ignore()?
public class GradeMap : ClassMap<Grade>
{
public GradeMap()
{
Id(x => x.GradeId);
Map(x => x.TaskName).NvarcharWithMaxSize().Not.Nullable();
Map(x => x.Weight).Not.Nullable().Precision(5).Scale(2);
Map(x => x.OutOf).Not.Nullable().Precision(5).Scale(2);
Map(x => x.Mark).Not.Nullable().Precision(5).Scale(2);
Map(x => x.CourseBackgroundColor).Not.Nullable();
Map(x => x.CoursePrefix).Not.Nullable();
References(x => x.Student).Not.Nullable();
References(x => x.Course)
.NotFound.Ignore();
}
}
EDIT:
Let's say database schema looks like this:
Grade(GradeId, TaskName, Course_id)
Course(CourseId, Prefix)
If there is no foreign key constraint on Course_id column, then row in Course table with CourseId that corresponds to Course_id column in Grade table can be deleted. E.g.:
Course table:
CourseId Prefix
1 Course1Prefix
Grade table:
GradeId TaskName Course_id
1 Grade1Task 1
without foreign key constraint you can issue this dml query:
delete from Course where CourseId = 1
And it could be reason of problem explained by sJhonny.
When "NotFound.Ignore()" is used in the mapping then NHibernate tries to load Courses that belong to all Grades that were loaded by the query:
session.Query<Grade>().ToList();
No proxies are generated and when course referenced by grade is not found in database then the Course property is simply null.
If you can modify the database schema it would be helpful to create PK constraint on Course_id column.

fluent nhibernate mapping

I am trying to create a map to get results as like from below query.
I am having hard time to get set Product mapping to set References to Product_Line on 3 columns as in where condition. How can I achieve this using fluent?
Product table: cId, ProjID, Line, etc., columns
Product_Line table: cId, ProjID, Line, etc., columns
select f.* from Product f
join Product_Line v on f.cId = v.CId and f.ProjID = v.ProjID and f.line = v.line
Thanks in Advance.
RajeshC
First, thank you for looking into it and Here with more info:
//Req: I want to query product such that if there is NO ProductLine, then I want to create a ProductLine, if there is one, then I'll update it.
public class ProductMap : ClassMap<Product>
{
Id(x => x.Id);
Map(x => x.CustomerId, "CustId");
Map(x => x.ProjId, "PROJId");
Map(x => x.LineNumber, "LineNumber");
Map(x => x.ReportType, "ReportType");
// Reference to Product_Line? - this reference should be based on three columns (custId, ProjId, LineNumber)
References(x => x.Line);
}
public class ProductLineMap : ClassMap<ProductLine>
{
Table("Product_Line");
Map(x => x.CustomerId, "CustId"); //same column as Product.CustId
Map(x => x.ProjId, "PROJId"); //Same as Product.ProjId
Map(x => x.LineNumber, "LINENUMBER"); //Same as Product.LineNumber
//etc.,
//for me, this reference is not needed as I need from Product to ProductLine - one way.
//References(x => x.Product).Column("ProjId") //
}
We could give you a much better answer if you showed us your C# code and wrapped the SQL in < code > tags... Here's my guess at what I think you want:
public class ProductMap : ClassMap<Product>
{
Id(x => x.Id);
References(x => x.Line); // Reference to Product_Line?
// etc.
}
public class ProductLineMap : ClassMap<ProductLine>
{
Table("Product_Line");
Id(x => x.Id).Column("cId");
References(x => x.Product).Column("ProjId")
}