How to insert child element referencing foreign key in NHibernate? - fluent-nhibernate

I am still new to Fluent NHibernate. Not sure how I should approach this.
I have two entities:
public class Student
{
public virtual Guid StudentId { get; set; }
public virtual String Dept_id { get; set; }
public virtual String Name { get; set; }
public virtual int Age { get; set; }
public virtual String Address { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public virtual int Dept_id { get; set; }
public virtual String Dept_name { get; set; }
public virtual IList<Student> Students { get; set; }
public Department()
{
Students = new List<Student>();
}
}
and the mappings are :
public class DepartmentMap : ClassMap<Department>
{
public DepartmentMap()
{
Table("Department");
Id(x => x.Dept_id).Column("Dept_id");
Map(x => x.Dept_name).Column("Dept_name");
HasMany(x => x.Students).KeyColumn("Student_id").Inverse()
.Cascade.All();
}
}
public class StudentMap :ClassMap<Student>
{
public StudentMap()
{
Table("Student");
Id(x => x.StudentId).Column("Student_id").GeneratedBy.GuidComb();
Map(x => x.Name);
Map(x => x.Age);
Map(x => x.Address);
Map(x => x.Dept_id).Column("Dept_id");
References(x => x.Department).Column("Dept_id").Not.Nullable();
}
}
when i am trying to insert as :
StudentRepository rep = new StudentRepository();
Student s = new Student();
s.Name = txtname.Text;
s.Age = int.Parse(txtage.Text);
s.Address = txtaddress.Text;
s.Dept_id = dddept.SelectedItem.Value;
rep.Add(s);
It's giving me error as:
{"not-null property references a null or transient value nHibernateTest.Domain.Student.Department"}

If you will be doing this like is in your post you will get an error because you want to send empty data of Department object. As Cole W says. You need to compare id of Department which you want to add with id of existing Department from database.
I will define SelectedDepartment from Cole W answer. You can add it to repository.
//repository
public Department SelectedDepartment(int id)
{
Department getDepartment = session.Get<Department>(id);
return getDepartment;
}
//controller
s.Department = rep.SelectedDepartment(1) //for example department with id = 1

When you are creating your new Student you need to set the Department not just the Id. Is there a reason you need a separate property for this? I would remove this property from your entity and from your mapping class and just leave in the Department reference. If you need the Department Id you can just reference it via Student.Department.Dept_id.
Basically you are trying to insert an entity with a null value in a foreign key column. You would need to change your creation to something like this:
StudentRepository rep = new StudentRepository();
Student s = new Student();
s.Name = txtname.Text;
s.Age = int.Parse(txtage.Text);
s.Address = txtaddress.Text;
s.Department = SelectedDepartment; //SelectedDepartment is the actual department entity from the database
rep.Add(s);

Related

Mapping by code on Class that has a property of type ICollection<>

I want to map a class that has a property of type ICollection<> using NHibernate mapping by code. The code below works. But I don't like the extra Person property within CarSet to make the mapping work.
public class PersonSet
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<CarSet> Cars { get; set; }
}
public class CarSet
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual PersonSet Person { get; set; }
}
public class PersonSetMap : ClassMapping<PersonSet>
{
public PersonSetMap()
{
Id(x => x.Id, m=>m.Generator(Generators.Identity));
Property(x=>x.Name);
Set(x => x.Cars, c =>
{
c.Key(k =>
{
k.Column("PersonId");
});
c.Cascade(Cascade.Persist);
c.Lazy(CollectionLazy.NoLazy);
}, r =>
{
r.OneToMany();
}
);
}
}
public class CarSetMap : ClassMapping<CarSet>
{
public CarSetMap()
{
Id(x => x.Id, m => m.Generator(Generators.Identity));
Property(x => x.Name);
ManyToOne(x => x.Person, m =>
{
m.Column("PersonId");
m.Cascade(Cascade.None);
m.NotNullable(true);
});
}
}
public void Save(){
using (var session = Cfg.Session)
using (var tx = session.BeginTransaction())
{
PersonSet John = new PersonSet { Name = PersonName.John };
John.Cars = new List<CarSet> {
new CarSet { Name = CarnName.BMW,Person = John},
new CarSet { Name = CarnName.BM,Person = John }};
session.Save(entity);
tx.Commit();
}
}
The code above generates SQL script below:
create table PersonSet (
Id INT IDENTITY NOT NULL,
Name NVARCHAR(255) null,
primary key (Id)
)
create table CarSet (
Id INT IDENTITY NOT NULL,
Name NVARCHAR(255) null,
PersonId INT not null,
primary key (id)
)
alter table CarSet
add constraint FKF967D6489A220265
foreign key (PersonId)
references PersonSet
What I want is to generate SQL script with difference shown below, and keep the rest the same:
create table CarSet (
Name NVARCHAR(255) null,
PersonId INT not null,
)
Ideally I want the CarSet like this instead:
public class CarSet
{
public virtual int PersonId { get; set; }
public virtual string Name { get; set; }
}
Any idea?
map Cars as ComponentCollection
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Car> Cars { get; set; }
}
class Car
{
public virtual Person Owner { get; set; }
public virtual string Name { get; set; }
}
public class PersonMap : ClassMapping<Person>
{
public PersonMap()
{
Id(x => x.Id, m => m.Generator(Generators.Identity));
Property(x => x.Name);
Set(x => x.Cars, c =>
{
c.Key(k => k.Column("PersonId"));
c.Cascade(NHibernate.Mapping.ByCode.Cascade.Persist);
c.Lazy(CollectionLazy.NoLazy);
}, r =>
{
r.Component(c =>
{
c.Parent(x => x.Owner);
c.Property(x => x.Name);
});
});
}
}
Your ideal solution isn't possible. To use a CarSet table without it's own ID column it has to be a component, but component sets can't have nullable columns. If it's ok for you to mark Name as not-null you can adapt the solution Firo posted.
If that's not ok you can at least solve your first request to remove the Person property. Just delete the property and mark the key column in your set mapping as not-nullable. CarSet will still be an entity (and therefore have it's own ID) but you don't need the reference to PersonSet in code.
Btw, why are your classes postfixed by Set? Just naming them Person and Car would be much better since they only represent one person or car, not a collection of them.

Using Fluent NHibernate, how to map one entity to differently named tables?

To be clear, I am looking to map one entire object and all its properties to different copies of basically the same table. My searches show me how to split an object's properties across multiple tables, but that is not what I am trying to accomplish.
Here is my object model (stripped down):
class Customer
{
public Guid CustomerGuid { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
class Address
{
public Guid AddressGuid { get; set; }
public string Line1 { get; set; }
public string State { get; set; }
}
class Application
{
public Guid ApplicationGuid { get; set; }
public Address Address { get; set; }
public DateTime SubmittedDate { get; set; }
}
The problem is that I need the Address to act sort like a component, but be saved into two separate tables: CustomerAddress and ApplicationAddress, as such:
table Customer
(
CustomerGuid
Name
)
table Application
(
ApplicationGuid
SubmittedDate
)
table CustomerAddress
(
CustomerGuid
Line1
State
)
table ApplicationAddress
(
ApplicationGuid
Line1
State
)
I know I can accomplish one of the mappings using as one-to-one (HasOne) for say Customer to CustomerAddress, but then how can I do the same thing with Application to ApplicationAddress?
for customer and analog for Application
class CustomerMap : ClassMap<Customer>
{
public CustomerMap()
{
Id(x => x.Id, "CustomerGuid").GeneratedBy.GuidComb();
Map(x => x.Name);
Join("CustomerAddress", join =>
{
join.KeyColumn("CustomerGuid");
join.Component(x => x.Address, c =>
{
c.Map(x => x.Line1);
c.Map(x => x.State);
});
});
}
}

NHibernate returning duplicate rows

NHibernate appears to be returning the contents of the first row multiple times. As many times as there are actual, distinct rows in the database. For example, if one person has 3 campus affiliations like this:
Baker College - Teacher
Bryant Elementary - Teacher
Ohio State University - Student
NHibernate will return it like this:
Baker College - Teacher
Baker College - Teacher
Baker College - Teacher
I'm using FluentNHibernate. Here are some snippets of the entity and mapping files:
public class Person
{
public virtual string SysID { get; set; }
public virtual string FullName { get; set; }
public virtual ICollection<Campus> Campuses { get; set; }
}
public class Campus
{
public virtual string SysID { get; set; }
public virtual string Name { get; set; }
public virtual string Affiliation { get; set; }
}
public class PersonMapping
{
Table("Person");
Id(x => x.SysId);
Map(x => x.FullName).Column("FULL_NAME");
HasMany(x => x.Campuses).KeyColumn("SysId");
}
public class CampusMapping
{
Table("Campus");
Id(x => x.SysID);
Map(x => x.Name);
Map(x => x.Affiliation);
}
I'm iterating through the campuses in my view (MVC 3) like this:
#foreach(var campus in Model.Campuses)
{
#campus.Name #campus.Affiliation
}
I also tried adding this to the entity to make sure it wasn't a silly mistake with MVC or Razor and had the same result:
public virtual string campusesToString
{
get
{
string s = "";
for (int i = 0; i < Campuses.Count; i++)
{
s = s + Campuses.ElementAt(i).Name + " ";
}
return s;
}
}
Finally, I checked the SQL that was being output, and it's correct, and it's returning all of the rows uniquely...
Your mapping looks a bit weird.
First you set up this relationship: Person 1 -> * Campus
But in your mapping of Campus you make SysId primary key, but it is also the foreign key to Person? I think that is what confuses NHibernate..
What I think happens is that NHibernate sees the same SysId key multiple times and since it will resolve the same object for the same primary key to preserve object indentity it will return the same Campus object multiple times even though the other columns have different data.
You might want to use a many-to-many mapping as otherwise each Campus will only be able to have one person which seems wrong.
Ok. I found out my problem. I was indicating the Id in the mapping incorrectly.
There is one person and multiple campuses. The person ID is called SysID. It is also the foreign key on campus. But SysID is not the unique ID for campus. NHibernate was confused because it was trying to use the person unique ID for the campus ID.
public class Campus
{
public virtual string CampusID { get; set; }
public virtual string SysID { get; set; }
public virtual string Name { get; set; }
public virtual string Affiliation { get; set; }
}
public class CampusMapping
{
Table("Campus");
Id(x => x.CampusID);
Map(x => x.SysID);
Map(x => x.Name);
Map(x => x.Affiliation);
}

FluentNHibernate. I need to map a hasmany relationship to a joined subclass entity type

I am trying to get this FluentNHibernate mapping to work. I have three tables Person, Employee and Employer. The Employee table extends the attributes of the Person table, and it's primary key is a foreign key to the Person table.
The Employee table also has a foriegn key to the Employer table. An employer can have many employees, and each employee is a person.
I have used FluentNHibernate to map these three tables. I used a Joined Subclass mapping for Employee -> Person.
I am trying to get a mapping to work so that I can get employers and their associated employees (eager fetching), but the generated select statement selects the EmployerId on both the Employee table (good), and the Person table (bad) resulting in an error (see below).
SELECT employees0_.EmployerId as Employee5_2_, employees0_.
PersonId as PersonId2_, employees0_.PersonId as PersonId1_1_, employees0_.FirstN
ame as FirstName1_1_, employees0_.LastName as LastName1_1_, employees0_1_.PayRat
e as PayRate2_1_, employees0_1_.EmployerId as EmployerId2_1_, employer1_.Employe
rId as EmployerId0_0_, employer1_.Name as Name0_0_ FROM [Person] employees0_ lef
t outer join Employee employees0_1_ on employees0_.PersonId=employees0_1_.Employ
eeId left outer join [Employer] employer1_ on employees0_1_.EmployerId=employer1
.EmployerId WHERE employees0.Employee.EmployerId=#p0;#p0 = 1
Please find the schema, class and Fluent Mappings below. What have I done wrong?
I have defined classes for these tables as follows:
public class Person
{
public int PersonId { get; set; }
public string PersonClassification { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employer
{
public int EmployerId { get; set; }
public string Name { get; set; }
public IList<Employee> Employees { get; set; }
}
public class Employee : Person
{
public Employer Employer { get; set; }
public decimal PayRate { get; set; }
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.PersonId).Column("PersonId").GeneratedBy.Identity();
Map(x => x.FirstName);
Map(x => x.LastName);
DiscriminateSubClassesOnColumn("PersonClassification");
}
}
public class EmployeeMap : SubclassMap<Employee>
{
public EmployeeMap()
{
this.DiscriminatorValue("Employee");
Join
(
"Employee",
join =>
{
join.Optional();
join.KeyColumn("EmployeeId");
join.Map(x => x.PayRate);
join.References(x => x.Employer).Column("EmployerId");
}
);
}
}
public class EmployerMap : ClassMap<Employer>
{
public EmployerMap()
{
Id(x => x.EmployerId).Column("EmployerId").GeneratedBy.Identity();
Map(x => x.Name);
HasMany(x => x.Employees).KeyColumn("Employee.EmployerId");
}
}

Fluent NHibernate Many to one mapping

I am creating a NHibenate application with one to many relationship. Like City and State data.
City table
CREATE TABLE [dbo].[State](
[StateId] [varchar](2) NOT NULL primary key,
[StateName] [varchar](20) NULL)
CREATE TABLE [dbo].[City](
[Id] [int] primary key IDENTITY(1,1) NOT NULL ,
[State_id] [varchar](2) NULL refrences State(StateId),
[CityName] [varchar](50) NULL)
My mapping is follows
public CityMapping()
{
Id(x => x.Id);
Map(x => x.State_id);
Map(x => x.CityName);
HasMany(x => x.EmployeePreferedLocations)
.Inverse()
.Cascade.SaveUpdate();
References(x => x.State)
//.Cascade.All();
//.Class(typeof(State))
//.Not.Nullable()
.Cascade.None()
.Column("State_id");
}
public StateMapping()
{
Id(x => x.StateId)
.GeneratedBy.Assigned();
Map(x => x.StateName);
HasMany(x => x.Jobs)
.Inverse();
//.Cascade.SaveUpdate();
HasMany(x => x.EmployeePreferedLocations)
.Inverse();
HasMany(x => x.Cities)
// .Inverse()
.Cascade.SaveUpdate();
//.Not.LazyLoad()
}
Models are as follows:
[Serializable]
public partial class City
{
public virtual System.String CityName { get; set; }
public virtual System.Int32 Id { get; set; }
public virtual System.String State_id { get; set; }
public virtual IList<EmployeePreferedLocation> EmployeePreferedLocations { get; set; }
public virtual JobPortal.Data.Domain.Model.State State { get; set; }
public City(){}
}
public partial class State
{
public virtual System.String StateId { get; set; }
public virtual System.String StateName { get; set; }
public virtual IList<City> Cities { get; set; }
public virtual IList<EmployeePreferedLocation> EmployeePreferedLocations { get; set; }
public virtual IList<Job> Jobs { get; set; }
public State()
{
Cities = new List<City>();
EmployeePreferedLocations = new List<EmployeePreferedLocation>();
Jobs = new List<Job>();
}
//public virtual void AddCity(City city)
//{
// city.State = this;
// Cities.Add(city);
//}
}
My Unit Testing code is below.
City city = new City();
IRepository<State> rState = new Repository<State>();
Dictionary<string, string> critetia = new Dictionary<string, string>();
critetia.Add("StateId", "TX");
State frState = rState.GetByCriteria(critetia);
city.CityName = "Waco";
city.State = frState;
IRepository<City> rCity = new Repository<City>();
rCity.SaveOrUpdate(city);
City frCity = rCity.GetById(city.Id);
The problem is , I am not able to insert record. The error is below.
"Invalid index 2 for this SqlParameterCollection with Count=2."
But the error will not come if I comment State_id mapping field in the CityMapping file. I donot know what mistake is I did. If do not give the mapping Map(x => x.State_id); the value of this field is null, which is desired. Please help me how to solve this issue.
Few remarks:
Remove this State_id property from the City class and the mapping. You already have a State property so it makes no sense in your object model.
Those Jobs and EmployeePreferedLocations properties in the State class don't have any related columns/tables in your database (at least the one you've shown here) while you have mappings for them.