/** * Create proxy for the specified ID value. You might need it for providing ID to the {@code EntityManager.find()} * method. * @param value real ID value */ public static <T extends Number> IdProxy<T> of(T value) { return new IdProxy<>(value); }
@Override public IdProxy<K> getId() { if (idProxy == null) { idProxy = new IdProxy<>(this); } // return a copy cleaned from the reference to the entity return idProxy.copy(); }
/** * 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; }