Using NHibernate session load method, is there a way to access an entity's id without initializing the proxy? -
according docs, session.load(id)
returns object uninitialized proxy , not hit database ...
this great have scenario want load object (that know exists in db) , later (within same session) access entity's id , id , not have proxy initialized. seems me if access id of proxy, shouldn't have initialized. @ least hoping can't seem work way.
essentially i'm trying following test pass:
[test] public void accessing_loaded_entity_id_should_not_initialize_the_proxy() { // arrange var repo = new nhrepository<order>(); var order = new orderbuilder().build(); repo.save(order); repo.flush(); repo.clear(); // act var fromdb = repo.load(order.id); // assert assert.areequal(order.id, fromdb.id); assert.isfalse(nhibernateutil.isinitialized(fromdb)); }
this test fails here:
assert.isfalse(nhibernateutil.isinitialized(fromdb));
update here's hbm mapping of id:
<id name="id" access="field"> <generator class="hilo"> <param name="column">ordernexthi</param> <param name="max_lo">100</param> </generator> </id>
here's base entity type. i'm thinking problem might here copied long time ago , didn't put thought it. let me know think:
public abstract class singleidentitydomainentity<t> t : singleidentitydomainentity<t> { private readonly int id; private int? _oldhashcode; protected singleidentitydomainentity() { this.id = 0; } public virtual int id { { return this.id; } } public override bool equals(object obj) { var other = obj t; if (other == null) return false; // handle case of comparing 2 new objects if (other.istransient() && this.istransient()) return referenceequals(other, this); return other.id.equals(this.id); } /// <summary> /// transient objects not associated item in storage. /// </summary> public virtual bool istransient() { return this.id == 0; } /// <summary> /// must provided compare 2 objects /// </summary> public override int gethashcode() { // once have hash code we'll never change if (_oldhashcode.hasvalue) return _oldhashcode.value; // when instance transient, use base gethashcode() // , remember it, instance can never change hash code. if (this.istransient()) { _oldhashcode = base.gethashcode(); return _oldhashcode.value; } return this.id.gethashcode(); } }
i not know implementation of repository think problem in the:
repo.clear();
line. guess clears session (all cache values evicted).
Comments
Post a Comment