Closed lyworry closed 4 months ago
Persistable
is a Spring Data API to determine the identifier and whether an object is new or not. Persistable
is only used by Spring Data modules in which we fully control the object lifecycle such as Elasticsearch.
JPA handles object lifecycles on its own with the concept of attached entities hence Spring Data JPA doesn't make use of Persistable
for inserts vs. updates, instead, what you already found, whether to call EntityManager.merge(…)
vs. EntityManager.persist(…)
.
Generally speaking, if you use a single cache (Cache.cacheMap.get(md5)
) for two persistence technologies, after associating the object with your cache, all related store modules will use the same outcome for isNew
.
Ideally, you use the md5 value as primary key if that is what you're looking for.
Persistable
seem to be unuseful in spring data jpa
.All operations depend on whether id
has value.I can't imagine what can i do with Persistable
.I think spring data jpa
and spring data elasticsearch
,they all belong to spring data
,
they implements Persistable
,They should have the same behavior
document
’s id
has value and isNew=true will throw exception, isNew=false will select by id
and update
;document
’s id
is null and isNew=true will insert
,isNew=false wll also insert
;
The
Class Document extends BaseEntity implements Persistable<Long>
andnew HashMap<Long,Document>
to storage the document that have been stored to the mysql (The key is document.md5 and the value is document). I create two repositoriespublic interface JpaDocumentRepository extends CrudRepository<Document,Long>
andpublic interface ElasticSearchDocumentRepository extends CrudRepository<Document,Long>
.i findJpaDocumentRepository.save()
was not call method 'getId()' that implement,butElasticSearchDocumentRepository.save()
will call the methodgetId()
. If I were to callJpaDocumentRepository. save
to save the twodocument
has the same md5, will produce two records, because there is no callgetId()
andElasticSearchDocumentRepository.save
will insert and update, a record only. I thinkimplements Persistable
is want to judge the object isNew,and execut update or insert.In fact,isNew
only is only used to determine executentityManager.persist(entity)
orentityManager.merge(entity)
.entityManager.merge(entity)
performs an insert or update operation depending on whether theid
is empty.This does not lose the meaning of such a design?