@SuppressWarnings("unchecked") protected static Entity createInstanceWithId(Class<? extends Entity> entityClass, Object id) { Entity entity = createInstance(entityClass); if (entity instanceof BaseGenericIdEntity) { ((BaseGenericIdEntity) entity).setId(id); } return entity; }
@Override public <T extends BaseGenericIdEntity<K>, K> T getReference(Class<T> entityClass, K id) { T entity = metadata.create(entityClass); entity.setId(id); entityStates.makePatch(entity); return entity; }
@Override public <T extends BaseGenericIdEntity<K>, K> T getReference(Class<T> entityClass, K id) { T entity = metadata.create(entityClass); entity.setId(id); entityStates.makePatch(entity); return entity; } }
protected <T extends Entity> T findOrCreate(Class<T> entityClass, Object id) { Entity reloadedRef = find(entityClass, id); if (reloadedRef == null) { reloadedRef = metadata.create(entityClass); if (reloadedRef instanceof BaseGenericIdEntity) { ((BaseGenericIdEntity) reloadedRef).setId(id); } persist(reloadedRef); } //noinspection unchecked return (T) reloadedRef; }
@SuppressWarnings("unchecked") protected void assignIdentifier(Entity entity) { if (!(entity instanceof BaseGenericIdEntity)) return; MetaClass metaClass = getClassNN(entity.getClass()); MetaProperty primaryKeyProperty = tools.getPrimaryKeyProperty(metaClass); if (primaryKeyProperty != null && tools.isEmbedded(primaryKeyProperty)) { // create an instance of embedded ID Entity key = create(primaryKeyProperty.getRange().asClass()); ((BaseGenericIdEntity) entity).setId(key); } else { if (!config.getEnableIdGenerationForEntitiesInAdditionalDataStores() && !Stores.MAIN.equals(tools.getStoreName(metaClass))) { return; } if (tools.isPersistent(metaClass)) { if (entity instanceof BaseLongIdEntity) { ((BaseGenericIdEntity<Long>) entity).setId(numberIdSource.createLongId(getEntityNameForIdGeneration(metaClass))); } else if (entity instanceof BaseIntegerIdEntity) { ((BaseGenericIdEntity<Integer>) entity).setId(numberIdSource.createIntegerId(getEntityNameForIdGeneration(metaClass))); } } } }
/** * Returns an entity instance which can be used as a reference to an object which exists in the database. * <p> * For example, if you are creating a User, you have to set a Group the user belongs to. If you know the group id, * you could load it from the database and set to the user. This method saves you from unneeded database round trip: * <pre> * user.setGroup(dataManager.getReference(Group.class, groupId)); * dataManager.commit(user); * </pre> * * A reference can also be used to delete an existing object by id: * <pre> * dataManager.remove(dataManager.getReference(Customer.class, customerId)); * </pre> * * @param entityClass entity class * @param id id of an existing object */ default <T extends BaseGenericIdEntity<K>, K> T getReference(Class<T> entityClass, K id) { T entity = AppBeans.get(Metadata.class).create(entityClass); entity.setId(id); AppBeans.get(EntityStates.class).makePatch(entity); return entity; }
@SuppressWarnings("unchecked") protected void copySystemState(Entity srcEntity, Entity dstEntity) { if (dstEntity instanceof BaseGenericIdEntity) { ((BaseGenericIdEntity) dstEntity).setId(srcEntity.getId()); BaseEntityInternalAccess.copySystemState((BaseGenericIdEntity) srcEntity, (BaseGenericIdEntity) dstEntity); if (srcEntity instanceof FetchGroupTracker && dstEntity instanceof FetchGroupTracker) { FetchGroup srcFetchGroup = ((FetchGroupTracker) srcEntity)._persistence_getFetchGroup(); ((FetchGroupTracker) dstEntity)._persistence_setFetchGroup(srcFetchGroup); } } else if (dstEntity instanceof AbstractNotPersistentEntity) { ((AbstractNotPersistentEntity) dstEntity).setId((UUID) srcEntity.getId()); BaseEntityInternalAccess.setNew((AbstractNotPersistentEntity) dstEntity, BaseEntityInternalAccess.isNew((AbstractNotPersistentEntity) srcEntity)); } if (dstEntity instanceof Versioned) { ((Versioned) dstEntity).setVersion(((Versioned) srcEntity).getVersion()); } }
public InstanceRef(EntityLoadInfo loadInfo) throws InstantiationException, IllegalAccessException { if (loadInfo == null) throw new NullPointerException("No load info passed"); this.loadInfo = loadInfo; MetaClass childMetaClass = loadInfo.getMetaClass(); if (!Strings.isNullOrEmpty(loadInfo.getViewName()) && !loadInfo.isNewEntity()) { DataService dataService = AppBeans.get(DataService.class); LoadContext<BaseGenericIdEntity<Object>> ctx = new LoadContext<>(loadInfo.getMetaClass()); ctx.setId(loadInfo.getId()).setView(loadInfo.getViewName()); instance = dataService.load(ctx); if (instance == null) { throw new RuntimeException("Entity with loadInfo " + loadInfo + " not found"); } } else { //noinspection unchecked instance = (BaseGenericIdEntity<Object>) AppBeans.get(Metadata.class).create(childMetaClass); if (!loadInfo.isNewEntity()) { for (MetaProperty metaProperty : childMetaClass.getProperties()) { if (!metaProperty.getRange().isClass()) { try { instance.setValue(metaProperty.getName(), null); } catch (Exception ignored) { } } } } } instance.setId(this.loadInfo.getId()); }
((BaseGenericIdEntity) entity).setId(id);
((BaseGenericIdEntity) refEntity).setId(refEntityId); PersistenceHelper.makeDetached((BaseGenericIdEntity) refEntity); ((AbstractInstance) entity).setValue(property.getName(), refEntity, false);