Featured post
c# - Shouldn't my nhibernate mapping take care of this? Error: not-null property reference a null or transient value -
i have database 3 tables:
- project
- sponsor
- projectsponsor
in domain objects, have object project has child property called sponsors list of projectsponsor objects, this:
ilist<projectsponsor> sponsors;
here nhibernate mapping code:
productmap:
hasmany(x => x.sponsors).asbag().inverse().cascade.alldeleteorphan();
productsponsormap:
references(x => x.project).not.nullable(); references(x => x.sponsor).not.nullable();
here my domain objects:
public class project { public virtual ilist<projectsponsor> sponsors { get; set; } } public class projectsponsor { public virtual project project { get; set; } public virtual sponsor sponsor { get; set; } }
ok . . issue, when try add new projects (with associated projectsponsor) database.
here code:
foreach (project project in newprojects) { repository.save(project); foreach (projectsponsor projectsponsor in project.sponsors) { repository.save(projectsponsor); } }
i getting exception on line:
repository.save(projectsponsor);
saying not-null property reference null or transient value trying saved:
projectsponsor.project property complaining about. have thought set default based on nhibernate mapping.
do need have explicit piece of code have code:
projectsponsor.project = project
or there wrong mapping files ?
if haven't set projectsponsor.project
property (and no code here says have) have default value of null violate mapping condition you've placed.
nhibernate going reflect state of objects , state of objects - want of better word - broken is. remove project property on projectsponsor map , you're losing functionality because directionality of relationships in oo reverse of rdbm directionality.
i expect like:
public class projectsponsor { // how instantiate me! public projectsponsor(project project, sponsor sponsor) { this.project = project; this.sponsor = sponsor; } protected projectsponsor() { // exist nhibernates benefit only! } public virtual project project { get; set; } public virtual sponsor sponsor { get; set; } }
- Get link
- X
- Other Apps
Comments
Post a Comment