c++ - 我可以将 const 对象传递给 ODB 中的 db.persist 吗?

标签 c++ odb

我正在尝试 ODB ORM,我必须坚持使用一个接口(interface),所以我需要获取一个 const 对象并将其持久化。我不确定 ODB API 是否允许保留 const 对象,因为某些部分似乎为此做好了准备,但它不起作用。

我在这里从 gcc 收到这个错误:

void OdbReportRW::write_object(const MyObject &my_object)
{
    odb::core::transaction t{db->begin()};
    db->persist(my_object);
    t.commit();
}

这是错误,我认为它说 my_object 不应该是常量:

In file included from /usr/local/include/odb/database.hxx:632:0,
from odb_report.hpp:21,
from src/vcf/odb_report.cpp:18:
/usr/local/include/odb/database.txx: In instantiation of ‘typename odb::object_traits<T>::id_type odb::database::persist_(T&) [with T = const MyObject; odb::database_id DB = (odb::database_id)5u; typename odb::object_traits<T>::id_type = long unsigned int]’:
/usr/local/include/odb/database.ixx:167:45:   required from ‘typename odb::object_traits<T>::id_type odb::database::persist(const T&) [with T = MyObject; typename odb::object_traits<T>::id_type = long unsigned int]’
src/vcf/odb_report.cpp:134:26:   required from here
/usr/local/include/odb/database.txx:38:39: error: no matching function for call to ‘odb::object_traits_impl<MyObject, (odb::database_id)5u>::persist(odb::database&, const MyObject&)’
object_traits::persist (*this, obj);
^
/usr/local/include/odb/database.txx:38:39: note: candidate is:
In file included from src/vcf/odb_report.cpp:27:0:
my-object-error-odb.hxx:247:5: note: static void odb::access::object_traits_impl<MyObject, (odb::database_id)1u>::persist(odb::database&, odb::access::object_traits<MyObject>::object_type&)
persist (database&, object_type&);
^
my-object-odb.hxx:247:5: note:   no known conversion for argument 2 from ‘const MyObject’ to ‘odb::access::object_traits<MyObject>::object_type& {aka MyObject&}’

与 clang 相同的错误,更具描述性:

In file included from src/vcf/odb_report.cpp:18:
In file included from inc/vcf/odb_report.hpp:21:
In file included from /usr/local/include/odb/database.hxx:632:
/usr/local/include/odb/database.txx:38:36: error: binding of reference to type 'object_type' (aka 'MyObject') to a value of type 'const MyObject' drops qualifiers
    object_traits::persist (*this, obj);
                                   ^~~
/usr/local/include/odb/database.ixx:167:12: note: in instantiation of function template specialization 'odb::database::persist_<const MyObject, 5>' requested here
    return persist_<const T, id_common> (obj);
           ^
src/vcf/odb_report.cpp:134:13: note: in instantiation of function template specialization 'odb::database::persist<MyObject>' requested here
        db->persist(my_object);
            ^
inc/vcf/error-odb.hxx:247:37: note: passing argument to parameter here
    persist (database&, object_type&);
                                    ^

但是,我可以在数据库接口(interface)(来自 odb)中看到提供了两种类型的 persist:reference 和 const reference(以及其他带有指针的):

// in odb/database.hxx
template <typename T>
typename object_traits<T>::id_type
persist (T& object);

template <typename T>
typename object_traits<T>::id_type
persist (const T& object);

我看到当类中没有要持久化的默认构造函数(这里是 MyObject)时,会引发类似的错误(对于 find,而不是 persist),但它是在那里,所以这不是问题。我已经检查过默认构造函数只在生成的代码中生成一个额外的方法 find

它在我的 write_object 方法中删除了 const 说明符,但正如我所说,我必须坚持使用接口(interface)。

有什么想法可以持久化一个 const 对象吗?

最佳答案

更彻底地阅读文档我发现了这个(http://www.codesynthesis.com/products/odb/doc/manual.xhtml#3.8):

The first persist() function expects a constant reference to an instance being persisted. The second function expects a constant object pointer. Both of these functions can only be used on objects with application-assigned object ids (Section 14.4.2, "auto").

确实,我对数据库处理的 ID 使用了 auto 说明符:

// class MyObject    
#pragma db id auto
unsigned long id_;

所以看来我不能同时使用 auto id 和 const 引用。

关于c++ - 我可以将 const 对象传递给 ODB 中的 db.persist 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38184320/

相关文章:

c# - 对于封装和可重用性,首选扩展方法?

C++ Lambda 通过引用捕获

c++ - ORM ODB for C++可以从数据库生成代码吗

c++ - 试图让简单的 ODB 'hello' 程序工作

c++ - 如何在 XCode 3.2.3 上启用异常处理?

c++ - 为什么要增加我的 openmp 代码的执行时间?

c++ - odb/pgsql/version.hxx 没有这样的文件或目录

c++ - C++ ODB向mysql插入汉字问题

c++ - 我可以从它包含的数据中分离一个 std::vector<char> 吗?

python - 从变形的测试对象中提取节点坐标(abaqus-python)