简体   繁体   English

Spring Data GemFire唯一约束

[英]Spring Data GemFire unique constraint

I am working with Spring Data GemFire and using it like this: 我正在使用Spring Data GemFire并像这样使用它:

Repository: 库:

@Region("Token")
public interface TokenRepository extends GemfireRepository<Token, String> {}

Entity: 实体:

public class Token implements Serializable {

@Id
private UUID id;
private String serial;

Is it possible to enforce uniqueness on the serial property, like with JPAs @Column(unique=true) ? 是否可以像JPAs @Column(unique=true)一样在serial属性上实施唯一性? At best it should throw an Exception on saving. 充其量最好应该在保存时抛出Exception

The quick and simple answer is NO. 快速简单的答案是“否”。 Neither Spring Data GemFire nor Pivotal GemFire (itself) support the ability to enforce unique properties on objects stored in a Region. Spring Data GemFire和Pivotal GemFire(本身)都不支持对存储在Region中的对象实施唯一属性的功能。

Even when using JPA (eg Hibernate), the @Column(unique = true) is implemented as an RDMBS table column (uniqueness) constraint, so there is nothing that JPA in particular is doing to enforce uniqueness. 即使在使用JPA(例如Hibernate)时, @Column(unique = true)也作为RDMBS表列(唯一性)约束实现,因此JPA并没有采取任何措施来强制唯一性。 If this column constraint was not present in the RDBMS table, then Column(unique = true) would have no effect. 如果此列约束是不存在的RDBMS表,然后Column(unique = true)将没有任何效果。

See here . 这里

This means that the uniqueness needs to be enforced at the data store, which would be especially true for Pivotal GemFire, since... 这意味着需要在数据存储中实施唯一性,这对于Pivotal GemFire尤其如此,因为...

Pivotal GemFire is a key/value store where the value is your (entire) object and the objects stored in a particular Region will be distributed and replicated (for redundancy in HA scenarios) across the GemFire cluster, which would not only make enforcing uniqueness very difficult, but also very expensive (eg time consuming to check). Pivotal GemFire是一个键/值存储,其中的值是您的(整个)对象,并且存储在特定区域中的对象将在整个GemFire集群中分布和复制(用于HA场景中的冗余),这不仅会使强制实施非常独特困难,但也很昂贵(例如检查很费时间)。

There are of course other ways to handle this, such as keeping an in-memory data structure of value hashes for each of the unique columns/properties. 当然,还有其他方法可以处理此问题,例如为每个唯一列/属性保留值哈希的内存数据结构。 But, that puts pressure on memory and you also need to keep the data structure up-to-date. 但是,这给内存带来压力,您还需要保持数据结构为最新。 Also, depending on the hash algorithm used, they are not always guaranteed to be strictly unique. 而且,根据所使用的哈希算法,并非始终保证它们严格唯一。

So, there really is not a good answer here. 因此,这里确实没有一个好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM