Ejemplo n.º 1
0
 @Override
 public <E extends T, K> E findByKey(Class<E> type, K key) {
   Type<E> entityType = entityModel.typeOf(type);
   if (entityType.isCacheable() && entityCache != null) {
     E entity = entityCache.get(type, key);
     if (entity != null) {
       return entity;
     }
   }
   Set<Attribute<E, ?>> keys = entityType.getKeyAttributes();
   if (keys.isEmpty()) {
     throw new MissingKeyException();
   }
   Selection<? extends Result<E>> selection = select(type);
   if (keys.size() == 1) {
     QueryAttribute<E, Object> attribute = Attributes.query(keys.iterator().next());
     selection.where(attribute.equal(key));
   } else {
     if (key instanceof CompositeKey) {
       CompositeKey compositeKey = (CompositeKey) key;
       for (Attribute<E, ?> attribute : keys) {
         QueryAttribute<E, Object> keyAttribute = Attributes.query(attribute);
         Object value = compositeKey.get(keyAttribute);
         selection.where(keyAttribute.equal(value));
       }
     } else {
       throw new IllegalArgumentException("CompositeKey required");
     }
   }
   return selection.get().firstOrNull();
 }
Ejemplo n.º 2
0
 private void writeObject(ObjectOutputStream stream) throws IOException {
   stream.defaultWriteObject();
   Type<E> type = SerializationContext.getType(entityClass);
   EntityProxy<E> proxy = type.proxyProvider().apply(entity);
   for (Property<E, ?> property : proxy.filterProperties(getPropertyFilter())) {
     Object value = property.get();
     stream.writeObject(value);
   }
 }
Ejemplo n.º 3
0
 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
   stream.defaultReadObject();
   Type<E> type = SerializationContext.getType(entityClass);
   entity = type.factory().get();
   EntityProxy<E> proxy = type.proxyProvider().apply(entity);
   for (Property<E, ?> property : proxy.filterProperties(getPropertyFilter())) {
     Object value = stream.readObject();
     property.setObject(value, PropertyState.LOADED);
   }
 }
Ejemplo n.º 4
0
 @Override
 public <E extends T> Insertion<? extends Result<Tuple>> insert(Class<E> type) {
   checkClosed();
   Type<E> entityType = context.getModel().typeOf(type);
   Set<Expression<?>> keySelection = new LinkedHashSet<>();
   for (Attribute<E, ?> attribute : entityType.getKeyAttributes()) {
     keySelection.add((Expression<?>) attribute);
   }
   InsertReturningOperation operation = new InsertReturningOperation(context, keySelection);
   return new QueryElement<>(INSERT, entityModel, operation).from(type);
 }
Ejemplo n.º 5
0
 @Override
 public <E> EntityProxy<E> proxyOf(E entity, boolean forUpdate) {
   checkClosed();
   @SuppressWarnings("unchecked")
   Type<E> type = (Type<E>) entityModel.typeOf(entity.getClass());
   EntityProxy<E> proxy = type.getProxyProvider().apply(entity);
   if (forUpdate && type.isReadOnly()) {
     throw new ReadOnlyException();
   }
   if (forUpdate) {
     EntityTransaction transaction = transactionProvider.get();
     if (transaction != null && transaction.active()) {
       transaction.addToTransaction(proxy);
     }
   }
   return proxy;
 }