我正在评估 Spring Data 对 Couchbase 的支持,并且遇到了以下问题。考虑以下伪代码示例,其中我有两个 POJO 类,以及为每个定义和实例化的存储库:
public class Foo
{
@Id
private String _id;
@Version
private Long _rev;
// .. Other data and members.
}
public class Bar
{
@Id
private String _id;
@Version
private Long _rev;
// .. Other data and members.
}
//
// Repositories
//
@Repository
public interface FooRepository extends CrudRepository<Foo, String> {
}
@Repository
public interface BarRepository extends CrudRepository<Bar, String> {
}
两个存储库都使用相同的 Couchbase 存储桶。下一个:
// Create a new Foo object and save it.
Foo f = new Foo( "id_1" );
fooRepository.save( f );
// Now, try fetching a Bar object using the ID of a Foo object (?)
Bar b = barRepository.findOne( "id_1" );
这会导致返回 Bar 对象,但未正确初始化 - 不会引发异常。问题是,为什么在这种情况下没有指示错误?当请求的类型与持久类型不匹配时,引发异常似乎并不难。我错过了什么吗?
FWIW,当我通过管理控制台查看 Couchbase 中的原始文档时,我观察到每个文档都包含一个“_class”属性,大概可用于识别用于表示数据的类,并检测此类不匹配。