diff --git a/src/main/java/io/ebean/BeanFinder.java b/src/main/java/io/ebean/BeanFinder.java new file mode 100644 index 000000000..0afcc7227 --- /dev/null +++ b/src/main/java/io/ebean/BeanFinder.java @@ -0,0 +1,169 @@ +package io.ebean; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.List; +import java.util.Optional; + +/** + * Provides finder functionality for use with "Dependency Injection style" use of Ebean. + *
+ * Note that typically users would extend BeanRepository rather than BeanFinder. + *
+ *{@code
+ *
+ * public class CustomerFinder extends BeanFinder {
+ *
+ * @Inject
+ * public CustomerFinder(EbeanServer server) {
+ * super(Customer.class, server);
+ * }
+ *
+ * // ... add customer specific finders
+ * }
+ *
+ * }
+ *
+ * @param The ID type
+ * @param + * This is equivalent to {@link Ebean#getServer(String)} + * + * @param server The name of the EbeanServer. If this is null then the default EbeanServer is + * returned. + */ + public EbeanServer db(String server) { + return Ebean.getServer(server); + } + + /** + * Creates an entity reference for this ID. + *
+ * Equivalent to {@link EbeanServer#getReference(Class, Object)} + */ + @Nonnull + public T ref(I id) { + return db().getReference(type, id); + } + + /** + * Retrieves an entity by ID. + *
+ * Equivalent to {@link EbeanServer#find(Class, Object)}
+ */
+ @Nullable
+ public T findById(I id) {
+ return db().find(type, id);
+ }
+
+ /**
+ * Find an entity by ID returning an Optional.
+ */
+ @Nullable
+ public Optional
+ * Equivalent to {@link EbeanServer#delete(Class, Object)}
+ */
+ public void deleteById(I id) {
+ db().delete(type, id);
+ }
+
+ /**
+ * Retrieves all entities of the given type.
+ */
+ @Nonnull
+ public List
+ * Equivalent to {@link EbeanServer#update(Class)}
+ */
+ protected UpdateQuery
+ * Equivalent to {@link EbeanServer#find(Class)}
+ */
+ protected Query
+ *
+ * Typically users would extend BeanRepository rather than BeanFinder.
+ *
+ * This is used so that when a bean that is otherwise unmodified is updated the version
+ * property is updated.
+ *
+ * An unmodified bean that is saved or updated is normally skipped and this marks the bean as
+ * dirty so that it is not skipped.
+ *
+ *
+ * This would be used to specify a property that we did not wish to include in a stateless update.
+ *
+ * Ebean will detect if this is a new bean or a previously fetched bean and perform either an
+ * insert or an update based on that.
+ *
+ * @see EbeanServer#save(Object)
+ */
+ public void save(T bean) {
+ db().save(bean);
+ }
+
+ /**
+ * Update this entity.
+ *
+ * @see EbeanServer#update(Object)
+ */
+ public void update(T bean) {
+ db().update(bean);
+ }
+
+ /**
+ * Insert this entity.
+ *
+ * @see EbeanServer#insert(Object)
+ */
+ public void insert(T bean) {
+ db().insert(bean);
+ }
+
+ /**
+ * Delete this bean.
+ *
+ * This will return true if the bean was deleted successfully or JDBC batch is being used.
+ *
+ * If there is no current transaction one will be created and committed for
+ * you automatically.
+ *
+ * If the Bean does not have a version property (or loaded version property) and
+ * the bean does not exist then this returns false indicating that nothing was
+ * deleted. Note that, if JDBC batch mode is used then this always returns true.
+ *
+ * This is used when the bean contains a {@code
+ *
+ * int rows =
+ * updateQuery()
+ * .set("status", Customer.Status.ACTIVE)
+ * .set("updtime", new Timestamp(System.currentTimeMillis()))
+ * .where()
+ * .gt("id", 1000)
+ * .update();
+ *
+ * }
+ *
+ * {@code
+ *
+ * @Repository
+ * public class CustomerRepository extends BeanRepository
+ *
+ * @param The ID type
+ * @param {@code
+ *
+ * @Inject
+ * public CustomerRepository(EbeanServer server) {
+ * super(Customer.class, server);
+ * }
+ *
+ * }
+ *
+ * @param type The bean type
+ * @param server The EbeanServer instance typically created via Spring factory or equivalent
+ */
+ protected BeanRepository(Class{@code
+ *
+ * Customer customer = customerRepository.byId(id);
+ *
+ * // mark the bean as dirty so that a save() or update() will
+ * // increment the version property
+ *
+ * customerRepository.markAsDirty(customer);
+ * customerRepository.save(customer);
+ *
+ * }
+ *
+ * @see EbeanServer#markAsDirty(Object)
+ */
+ public void markAsDirty(T bean) {
+ db().markAsDirty(bean);
+ }
+
+ /**
+ * Mark the property as unset or 'not loaded'.
+ * {@code
+ *
+ * // populate an entity bean from JSON or whatever
+ * Customer customer = ...;
+ *
+ * // mark the email property as 'unset' so that it is not
+ * // included in a 'stateless update'
+ * customerRepository.markPropertyUnset(customer, "email");
+ *
+ * customerRepository.update(customer);
+ *
+ * }
+ *
+ * @param propertyName the name of the property on the bean to be marked as 'unset'
+ */
+ public void markPropertyUnset(T bean, String propertyName) {
+ ((EntityBean) bean)._ebean_getIntercept().setPropertyLoaded(propertyName, false);
+ }
+
+ /**
+ * Insert or update this entity depending on its state.
+ * @SoftDelete property and we
+ * want to perform a hard/permanent delete.
+ *