/** * @return real ID value or null if it is not assigned yet */ @Nullable public T get() { if (value != null) return value; if (entity == null) return null; return entity.getDbGeneratedId(); }
@Override public void afterCommit() { Number id = item.getDbGeneratedIdEntity().getId().getNN(); item.setObjectEntityId(id); try (Transaction tx = persistence.createTransaction()) { EntityManager em = persistence.getEntityManager(); em.persist(item); tx.commit(); } } });
if (pkValue instanceof IdProxy && entity instanceof BaseDbGeneratedIdEntity) { ((BaseDbGeneratedIdEntity) entity).setId((IdProxy) pkValue); } else { entity.setValue("id", pkValue);
@Override public void setId(IdProxy<K> idProxy) { this.idProxy = idProxy.copy(false); setDbGeneratedId(this.idProxy.get()); this.idProxy.setEntity(this); }
@Override public void flush() { EntityManagerContext context = persistence.getEntityManagerContext(); List<EntityLogItem> items = context.getAttribute(EntityLog.class.getName()); if (items == null || items.isEmpty()) return; for (EntityLogItem item : items) { List<EntityLogItem> sameEntityList = items.stream() .filter(entityLogItem -> entityLogItem.getDbGeneratedIdEntity() != null ? entityLogItem.getDbGeneratedIdEntity().equals(item.getDbGeneratedIdEntity()) : entityLogItem.getObjectEntityId().equals(item.getObjectEntityId())) .collect(Collectors.toList()); EntityLogItem itemToSave = sameEntityList.get(0); computeChanges(itemToSave, sameEntityList); saveItem(itemToSave); } }
@Override public String toString() { if (value != null) return value.toString(); if (entity != null && entity.getDbGeneratedId() != null) return entity.getDbGeneratedId().toString(); else return "?(" + uuid + ")"; } }
/** * @return real ID value * @throws IllegalStateException if it is not assigned yet */ public T getNN() { if (value != null) return value; if (entity == null) throw new IllegalStateException("Cannot get primary key value: entity is null"); if (entity.getDbGeneratedId() == null) throw new IllegalStateException("Cannot get primary key value: ID is null in entity " + entity); return entity.getDbGeneratedId(); }
@Override public boolean equals(Object other) { if (this == other) return true; if (other == null || getClass() != other.getClass()) return false; IdProxy that = (IdProxy) other; if (value != null) { if (that.value != null) return value.equals(that.value); if (that.entity != null && that.entity.getDbGeneratedId() != null) return value.equals(that.entity.getDbGeneratedId()); } if (entity != null && entity.getDbGeneratedId() != null) { if (that.entity != null && that.entity.getDbGeneratedId() != null) return entity.getDbGeneratedId().equals(that.entity.getDbGeneratedId()); if (that.value != null) return entity.getDbGeneratedId().equals(that.value); } return Objects.equals(uuid, that.uuid); }
/** * Returns a copy of this IdProxy cleaned from a reference to entity. * @param shared if true, a shared instance of the copy will be returned to avoid creating new object. * DO NOT use shared copies when assigning the same ID to another entity! */ public IdProxy<T> copy(boolean shared) { if (copy == null || !shared || !Objects.equals(value, copy.value) || !Objects.equals(uuid, copy.uuid) || (copy.value == null && entity != null && entity.getDbGeneratedId() != null)) { copy = new IdProxy<>(); if (value != null) copy.value = value; else if (entity != null && entity.getDbGeneratedId() != null) copy.value = entity.getDbGeneratedId(); copy.uuid = uuid; copy.hashCode = hashCode; } return copy; }