在 NHibernate 中访问数据
Acessing Data in NHibernate
我已经阅读了有关 NHibernate 的文档和教程,但我不明白如何实际操作数据库中的数据。我的设置是 NHibernate 2.1 和 Oracle 数据库。
当我想操作或读取数据库中的数据时,我是使用 Get/Load 将我想要的数据加载到实体中,还是必须先使用 Criteria 查询数据库?
您看过 NHibernate 参考的第 9 章吗,Manipulating Data?它对不同方面有简短的指示。至于Get()
或查询,如果你知道你通常使用Get()
或Load()
的标识符,否则你使用任何查询API。
这里是一个使用 Load() 的例子。在开放的会话和交易中,您可以这样做:
DomesticCat cat = (DomesticCat) sess.Load<Cat>( 69L );
cat.Name = "PK";
sess.Flush(); // changes to cat are automatically detected and persisted
// Flush not required if FlushMode is Commit or Auto (and
// transactions are used, which you should).
另请注意,NHibernate 2.1 非常旧。考虑使用较新的版本,但至少要注意当前文档中提到的某些内容在这样的旧版本中不可用。
我已经阅读了有关 NHibernate 的文档和教程,但我不明白如何实际操作数据库中的数据。我的设置是 NHibernate 2.1 和 Oracle 数据库。
当我想操作或读取数据库中的数据时,我是使用 Get/Load 将我想要的数据加载到实体中,还是必须先使用 Criteria 查询数据库?
您看过 NHibernate 参考的第 9 章吗,Manipulating Data?它对不同方面有简短的指示。至于Get()
或查询,如果你知道你通常使用Get()
或Load()
的标识符,否则你使用任何查询API。
这里是一个使用 Load() 的例子。在开放的会话和交易中,您可以这样做:
DomesticCat cat = (DomesticCat) sess.Load<Cat>( 69L );
cat.Name = "PK";
sess.Flush(); // changes to cat are automatically detected and persisted
// Flush not required if FlushMode is Commit or Auto (and
// transactions are used, which you should).
另请注意,NHibernate 2.1 非常旧。考虑使用较新的版本,但至少要注意当前文档中提到的某些内容在这样的旧版本中不可用。