001package io.ebean; 002 003import javax.annotation.Nonnull; 004import javax.annotation.Nullable; 005import java.util.List; 006import java.util.Optional; 007 008/** 009 * Provides finder functionality for use with "Dependency Injection style" use of Ebean. 010 * <p> 011 * Note that typically users would extend BeanRepository rather than BeanFinder. 012 * </p> 013 * <pre>{@code 014 * 015 * public class CustomerFinder extends BeanFinder<Long,Customer> { 016 * 017 * @Inject 018 * public CustomerFinder(EbeanServer server) { 019 * super(Customer.class, server); 020 * } 021 * 022 * // ... add customer specific finders 023 * } 024 * 025 * }</pre> 026 * 027 * @param <I> The ID type 028 * @param <T> The Bean type 029 */ 030public abstract class BeanFinder<I,T> { 031 032 protected final EbeanServer server; 033 034 protected final Class<T> type; 035 036 /** 037 * Create with the given bean type and EbeanServer instance. 038 * 039 * @param type The bean type 040 * @param server The EbeanServer instance typically created via Spring factory or equivalent. 041 */ 042 protected BeanFinder(Class<T> type, EbeanServer server) { 043 this.type = type; 044 this.server = server; 045 } 046 047 /** 048 * Return the EbeanServer to use. 049 */ 050 public EbeanServer db() { 051 return server; 052 } 053 054 /** 055 * Return the current transaction. 056 */ 057 public Transaction currentTransaction() { 058 return db().currentTransaction(); 059 } 060 061 /** 062 * Flush the JDBC batch on the current transaction. 063 */ 064 public void flush() { 065 db().flush(); 066 } 067 068 /** 069 * Return typically a different EbeanServer to the default. 070 * <p> 071 * This is equivalent to {@link Ebean#getServer(String)} 072 * 073 * @param server The name of the EbeanServer. If this is null then the default EbeanServer is 074 * returned. 075 */ 076 public EbeanServer db(String server) { 077 return Ebean.getServer(server); 078 } 079 080 /** 081 * Creates an entity reference for this ID. 082 * <p> 083 * Equivalent to {@link EbeanServer#getReference(Class, Object)} 084 */ 085 @Nonnull 086 public T ref(I id) { 087 return db().getReference(type, id); 088 } 089 090 /** 091 * Retrieves an entity by ID. 092 * <p> 093 * Equivalent to {@link EbeanServer#find(Class, Object)} 094 */ 095 @Nullable 096 public T findById(I id) { 097 return db().find(type, id); 098 } 099 100 /** 101 * Find an entity by ID returning an Optional. 102 */ 103 @Nullable 104 public Optional<T> findByIdOrEmpty(I id) { 105 return db().find(type).setId(id).findOneOrEmpty(); 106 } 107 108 /** 109 * Delete a bean by Id. 110 * <p> 111 * Equivalent to {@link EbeanServer#delete(Class, Object)} 112 */ 113 public void deleteById(I id) { 114 db().delete(type, id); 115 } 116 117 /** 118 * Retrieves all entities of the given type. 119 */ 120 @Nonnull 121 public List<T> findAll() { 122 return query().findList(); 123 } 124 125 /** 126 * Creates an update query. 127 * 128 * <pre>{@code 129 * 130 * int rows = 131 * updateQuery() 132 * .set("status", Customer.Status.ACTIVE) 133 * .set("updtime", new Timestamp(System.currentTimeMillis())) 134 * .where() 135 * .gt("id", 1000) 136 * .update(); 137 * 138 * }</pre> 139 * 140 * <p> 141 * Equivalent to {@link EbeanServer#update(Class)} 142 */ 143 protected UpdateQuery<T> updateQuery() { 144 return db().update(type); 145 } 146 147 /** 148 * Creates a query. 149 * <p> 150 * Equivalent to {@link EbeanServer#find(Class)} 151 */ 152 protected Query<T> query() { 153 return db().find(type); 154 } 155 156 /** 157 * Creates a native sql query. 158 */ 159 protected Query<T> nativeSql(String nativeSql) { 160 return db().findNative(type, nativeSql); 161 } 162 163 /** 164 * Creates a query using the ORM query language. 165 */ 166 protected Query<T> query(String ormQuery) { 167 return db().createQuery(type, ormQuery); 168 } 169}