SaveOrUpdateCopy를 사용하여 데이터베이스에 새 행을 삽입하지 않는 CompositeId가있는 엔티티가 있습니다. NHibernate에 의해 생성 된 INSERT 문은 "?" 모든 필드 값 그것은 SaveOrUpdate와 함께 잘 삽입하고 SaveOrUpdateCopy 또는 SaveOrUpdate로 잘 업데이트되며 SaveOrUpdateCopy를 사용하여 CompositeId 삽입/업데이트를하지 않은 엔티티도 정상적으로 업데이트됩니다. 나는 ComposOld를 사용하여 엔티티를 찾고 ifOrUpdate 또는 SaveOrUpdateCopy를 사용해야하는지 결정하고 싶지 않습니다. SaveOrUpdateCopy가 CompositeId가있는 엔티티와 함께 작동하도록하는 데있어 몇 가지 트릭이 있습니까?NHibernate SaveOrUpdateCopy는 CompositeId와 함께 엔티티를 삽입하지 않습니다.
public class MyEntity { public virtual Int32 FirstProperty { get; set; } public virtual string SecondProperty { get; set; } public virtual string DataText { get; set; } public override int GetHashCode() { int hashCode = 0; hashCode = hashCode^FirstProperty.GetHashCode()^ SecondProperty.GetHashCode(); return hashCode; } public override bool Equals(object obj) { MyEntity toCompare = obj as MyEntity; if(toCompare == null) { return false; } return (GetHashCode() != toCompare.GetHashCode()); } }
public MyEntityMap() { CompositeId() .KeyProperty(x => x.FirstProperty, "first_property") .KeyProperty(x => x.SecondProperty, "second_property"); Map(x => x.DataText, "data_text") .Nullable(); Table("dbo.my_entity"); }
데이터베이스 전화 : 여기
가 (이름이 무죄를 보호하기 위해 변경) 코드의public MyEntity GetMyEntity(long firstProperty, string secondProperty) { using (var session = sessionFactory.OpenSession()) { var result = from entity in session.Linq() where entity.FirstProperty == firstProperty && entity.SecondProperty== secondProperty select entity; return result.Count() > 0 ? result.First() : null; } }
데이터베이스 저장 :
이
using (var session = sessionFactory.OpenSession()) { using (var transaction = session.BeginTransaction()) { try { session.SaveOrUpdateCopy(entity); transaction.Commit(); } catch(Exception ex) { transaction.Rollback(); throw; } } }
내가 제안한 링크에서 제안 된 솔루션을 조사했지만 nhibernate xml 스키마에서 버전 요소가 허용되지 않습니다. 내가 잘못하고 있습니다? 감사 – Hoghweed