简体   繁体   中英

Spring Data GemFire unique constraint

I am working with Spring Data GemFire and using it like this:

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) ? At best it should throw an Exception on saving.

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.

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. If this column constraint was not present in the RDBMS table, then Column(unique = true) would have no effect.

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 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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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