diff --git a/src/com/wentch/redkale/source/DataJPASource.java b/src/com/wentch/redkale/source/DataJPASource.java deleted file mode 100644 index 27b79318c..000000000 --- a/src/com/wentch/redkale/source/DataJPASource.java +++ /dev/null @@ -1,1154 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package com.wentch.redkale.source; - -import static com.wentch.redkale.source.FilterExpress.*; -import com.wentch.redkale.util.*; -import java.io.*; -import java.lang.reflect.*; -import java.util.*; -import java.util.concurrent.atomic.*; -import java.util.logging.*; -import javax.persistence.*; -import javax.persistence.criteria.*; - -/** - * 不再完全实现JPA版的DataSource - *

- * @author zhangjx - */ -@Deprecated -final class DataJPASource implements DataSource { - - protected final EntityManagerFactory factory; - - private final AtomicBoolean debug = new AtomicBoolean(false); - - private final Logger logger = Logger.getLogger(DataJPASource.class.getSimpleName()); - - @Override - public void refreshCache(Class clazz) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public void delete(Class clazz, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public void delete(DataConnection conn, Class clazz, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public void updateColumnIncrement(Class clazz, Serializable id, String column, long incvalue) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public void updateColumnIncrement(DataConnection conn, Class clazz, Serializable id, String column, long incvalue) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Number getNumberResult(Class entityClass, Reckon reckon, String column) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Number getNumberResult(Class entityClass, Reckon reckon, String column, FilterBean bean) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Number getNumberResult(Class entityClass, Reckon reckon, String column, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public T find(Class clazz, SelectColumn selects, Serializable pk) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public T find(Class clazz, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public T find(Class clazz, FilterBean bean) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Set queryColumnSet(String selectedColumn, Class clazz, String column, Serializable key) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Set queryColumnSet(String selectedColumn, Class clazz, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Set queryColumnSet(String selectedColumn, Class clazz, FilterBean bean) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public List queryColumnList(String selectedColumn, Class clazz, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public List queryColumnList(String selectedColumn, Class clazz, FilterBean bean) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public List queryList(Class clazz, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public List queryList(Class clazz, SelectColumn selects, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Sheet queryColumnSheet(String selectedColumn, Class clazz, Flipper flipper, FilterBean bean) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Sheet queryColumnSheet(String selectedColumn, Class clazz, Flipper flipper, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Sheet querySheet(Class clazz, Flipper flipper, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Sheet querySheet(Class clazz, SelectColumn selects, Flipper flipper, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Map getMapResult(Class entityClass, String keyColumn, Reckon reckon, String reckonColumn) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Map getMapResult(Class entityClass, String keyColumn, Reckon reckon, String reckonColumn, FilterBean bean) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public Map getMapResult(Class entityClass, String keyColumn, Reckon reckon, String reckonColumn, FilterNode node) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - private static class DataJPAConnection extends DataConnection { - - private final EntityManager manager; - - private DataJPAConnection(EntityManager m) { - super(m); - this.manager = m; - } - - public void close() { - manager.close(); - } - - public boolean commit() { - try { - manager.getTransaction().commit(); - return true; - } catch (Exception e) { - return false; - } - } - - public void rollback() { - manager.getTransaction().rollback(); - } - } - - public DataJPASource() { - this(""); - } - - public DataJPASource(final String unitName) { - factory = Persistence.createEntityManagerFactory(unitName); - setDebug(System.getProperty("javax.persistence.debug") != null); - } - - public static DataJPASource create() { - return new DataJPASource(""); - } - - public static DataJPASource create(final String unitName) { - return new DataJPASource(unitName); - } - - private void setDebug(boolean flag) { - this.debug.set(flag); - if (flag) logger.setLevel(Level.FINEST); - } - - public DataConnection createReadConnection() { - return new DataJPAConnection(factory.createEntityManager()); - } - - public DataConnection createWriteConnection() { - return new DataJPAConnection(factory.createEntityManager()); - } - - public void close() { - this.factory.close(); - } - //----------------------insert----------------------------- - - /** - * 新增对象, 必须是Entity对象 - * - * @param - * @param values - */ - public void insert(T... values) { - final EntityManager manager = factory.createEntityManager(); - try { - manager.getTransaction().begin(); - for (T value : values) { - manager.persist(value); - } - manager.getTransaction().commit(); - } finally { - manager.close(); - } - } - - /** - * 新增对象, 必须是Entity对象 - * - * @param - * @param conn - * @param values - */ - public void insert(final DataConnection conn, T... values) { - final EntityManager manager = conn.getConnection(); - for (T value : values) { - manager.persist(value); - } - } - - //-------------------------delete-------------------------- - /** - * 删除对象, 必须是Entity对象 - * - * @param - * @param values - */ - public void delete(T... values) { - final EntityManager manager = factory.createEntityManager(); - try { - manager.getTransaction().begin(); - delete(manager, values); - manager.getTransaction().commit(); - } finally { - manager.close(); - } - } - - /** - * 删除对象, 必须是Entity对象 - * - * @param - * @param conn - * @param values - */ - public void delete(final DataConnection conn, T... values) { - final EntityManager manager = conn.getConnection(); - delete(manager, values); - } - - private void delete(final EntityManager manager, T... values) { - for (T value : values) { - manager.remove(value); - } - } - - /** - * 根据主键值删除对象, 必须是Entity Class - * - * @param - * @param clazz - * @param ids 主键值 - */ - public void delete(Class clazz, Serializable... ids) { - final EntityManager manager = factory.createEntityManager(); - try { - manager.getTransaction().begin(); - delete(manager, clazz, ids); - manager.getTransaction().commit(); - } finally { - manager.close(); - } - } - - /** - * 根据主键值删除对象, 必须是Entity Class - * - * @param - * @param conn - * @param clazz - * @param ids - */ - public void delete(final DataConnection conn, Class clazz, Serializable... ids) { - final EntityManager manager = conn.getConnection(); - delete(manager, clazz, ids); - } - - private void delete(final EntityManager manager, Class clazz, Serializable... ids) { - for (Serializable id : ids) { - Object value = manager.find(clazz, id); - if (value != null) manager.remove(value); - } - } - - /** - * 根据column字段的值删除对象, 必须是Entity Class - * - * @param - * @param clazz - * @param column - * @param keys - */ - public void deleteByColumn(Class clazz, String column, Serializable... keys) { - final EntityManager manager = factory.createEntityManager(); - try { - manager.getTransaction().begin(); - deleteByColumn(manager, clazz, column, keys); - manager.getTransaction().commit(); - } finally { - manager.close(); - } - } - - /** - * 根据column字段的值删除对象, 必须是Entity Class - * - * @param - * @param conn - * @param clazz - * @param column - * @param keys - */ - public void deleteByColumn(final DataConnection conn, Class clazz, String column, Serializable... keys) { - final EntityManager manager = conn.getConnection(); - deleteByColumn(manager, clazz, column, keys); - } - - private void deleteByColumn(final EntityManager manager, Class clazz, String column, Serializable... keys) { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - CriteriaDelete cd = builder.createCriteriaDelete(clazz); - cd.where(cd.getRoot().get(column).in((Object[]) keys)); - manager.createQuery(cd).executeUpdate(); - } - - /** - * 根据两个column字段的值删除对象, 必须是Entity Class - * - * @param - * @param clazz - * @param column1 - * @param key1 - * @param column2 - * @param key2 - */ - public void deleteByTwoColumn(Class clazz, String column1, Serializable key1, String column2, Serializable key2) { - final EntityManager manager = factory.createEntityManager(); - try { - manager.getTransaction().begin(); - deleteByTwoColumn(manager, clazz, column1, key1, column2, key2); - manager.getTransaction().commit(); - } finally { - manager.close(); - } - } - - /** - * 根据两个column字段的值删除对象, 必须是Entity Class - * - * @param - * @param conn - * @param clazz - * @param column1 - * @param key1 - * @param column2 - * @param key2 - */ - public void deleteByTwoColumn(final DataConnection conn, Class clazz, String column1, Serializable key1, String column2, Serializable key2) { - deleteByTwoColumn((EntityManager) conn.getConnection(), clazz, column1, key1, column2, key2); - } - - private void deleteByTwoColumn(final EntityManager manager, Class clazz, String column1, Serializable key1, String column2, Serializable key2) { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - CriteriaDelete cd = builder.createCriteriaDelete(clazz); - final Root root = cd.getRoot(); - cd.where(builder.and(builder.equal(root.get(column1), key1), builder.equal(root.get(column2), key2))); - manager.createQuery(cd).executeUpdate(); - } - - /** - * 根据三个column字段的值删除对象, 必须是Entity Class - * - * @param - * @param clazz - * @param column1 - * @param key1 - * @param column2 - * @param key2 - * @param column3 - * @param key3 - */ - public void deleteByThreeColumn(Class clazz, String column1, Serializable key1, String column2, Serializable key2, String column3, Serializable key3) { - final EntityManager manager = factory.createEntityManager(); - try { - manager.getTransaction().begin(); - deleteByThreeColumn(manager, clazz, column1, key1, column2, key2, column3, key3); - manager.getTransaction().commit(); - } finally { - manager.close(); - } - } - - /** - * 根据三个column字段的值删除对象, 必须是Entity Class - * - * @param - * @param conn - * @param clazz - * @param column1 - * @param key1 - * @param column2 - * @param key2 - * @param column3 - * @param key3 - */ - public void deleteByThreeColumn(final DataConnection conn, Class clazz, String column1, Serializable key1, String column2, Serializable key2, String column3, Serializable key3) { - deleteByThreeColumn((EntityManager) conn.getConnection(), clazz, column1, key1, column2, key2, column3, key3); - } - - private void deleteByThreeColumn(final EntityManager manager, Class clazz, String column1, Serializable key1, String column2, Serializable key2, String column3, Serializable key3) { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - CriteriaDelete cd = builder.createCriteriaDelete(clazz); - final Root root = cd.getRoot(); - cd.where(builder.and(builder.equal(root.get(column1), key1), builder.equal(root.get(column2), key2), builder.equal(root.get(column3), key3))); - manager.createQuery(cd).executeUpdate(); - } - //------------------------update--------------------------- - - /** - * 更新对象, 必须是Entity对象 - * - * @param - * @param values - */ - public void update(T... values) { - final EntityManager manager = factory.createEntityManager(); - try { - manager.getTransaction().begin(); - update(manager, values); - manager.getTransaction().commit(); - } finally { - manager.close(); - } - } - - /** - * 更新对象, 必须是Entity对象 - * - * @param - * @param conn - * @param values - */ - public void update(final DataConnection conn, T... values) { - final EntityManager manager = conn.getConnection(); - update(manager, values); - } - - private void update(final EntityManager manager, T... values) { - for (T value : values) { - manager.merge(value); - } - } - - /** - * 根据主键值更新对象的column对应的值, 必须是Entity Class - * - * @param - * @param clazz - * @param id - * @param column - * @param value - */ - public void updateColumn(Class clazz, Serializable id, String column, Serializable value) { - final EntityManager manager = factory.createEntityManager(); - try { - manager.getTransaction().begin(); - updateColumn(manager, clazz, id, column, value); - manager.getTransaction().commit(); - } finally { - manager.close(); - } - } - - /** - * 根据主键值更新对象的column对应的值, 必须是Entity Class - * - * @param - * @param conn - * @param clazz - * @param id - * @param column - * @param value - */ - public void updateColumn(final DataConnection conn, Class clazz, Serializable id, String column, Serializable value) { - final EntityManager manager = conn.getConnection(); - updateColumn(manager, clazz, id, column, value); - } - - private void updateColumn(final EntityManager manager, Class clazz, Serializable id, String column, Serializable value) { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - CriteriaUpdate cd = builder.createCriteriaUpdate(clazz); - cd.set(column, value); - cd.where(builder.equal(cd.from(clazz).get(EntityInfo.load(clazz, 0, null).getPrimary().field()), id)); - manager.createQuery(cd).executeUpdate(); - } - - /** - * 更新对象指定的一些字段, 必须是Entity对象 - * - * @param - * @param value - * @param columns - */ - public void updateColumns(final T value, final String... columns) { - final EntityManager manager = factory.createEntityManager(); - try { - manager.getTransaction().begin(); - updateColumns(manager, value, columns); - manager.getTransaction().commit(); - } finally { - manager.close(); - } - } - - /** - * 更新对象指定的一些字段, 必须是Entity对象 - * - * @param - * @param conn - * @param value - * @param columns - */ - public void updateColumns(final DataConnection conn, final T value, final String... columns) { - final EntityManager manager = conn.getConnection(); - updateColumns(manager, value, columns); - } - - private void updateColumns(final EntityManager manager, final T value, final String... columns) { - final Class clazz = value.getClass(); - final EntityInfo info = EntityInfo.load(clazz, 0, null); - final Attribute idattr = info.getPrimary(); - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - final CriteriaUpdate cd = builder.createCriteriaUpdate(clazz); - for (String column : columns) { - cd.set(column, info.getAttribute(column).get(value)); - } - cd.where(builder.equal(cd.from(clazz).get(info.getPrimary().field()), idattr.get(value))); - manager.createQuery(cd).executeUpdate(); - } - - //-----------------------getSingleResult----------------------------- - //-----------------------------MAX----------------------------- - public Number getMaxSingleResult(final Class entityClass, final String column) { - return getMaxSingleResult(entityClass, column, (FilterBean) null); - } - - public Number getMaxSingleResult(final Class entityClass, final String column, FilterBean bean) { - return getSingleResult(ReckonType.MAX, entityClass, column, bean); - } - - //-----------------------------MIN----------------------------- - public Number getMinSingleResult(final Class entityClass, final String column) { - return getMinSingleResult(entityClass, column, (FilterBean) null); - } - - public Number getMinSingleResult(final Class entityClass, final String column, FilterBean bean) { - return getSingleResult(ReckonType.MIN, entityClass, column, bean); - } - - //-----------------------------SUM----------------------------- - public Number getSumSingleResult(final Class entityClass, final String column) { - return getSumSingleResult(entityClass, column, (FilterBean) null); - } - - public Number getSumSingleResult(final Class entityClass, final String column, FilterBean bean) { - return getSingleResult(ReckonType.SUM, entityClass, column, bean); - } - - //----------------------------COUNT---------------------------- - public Number getCountSingleResult(final Class entityClass) { - return getCountSingleResult(entityClass, (FilterBean) null); - } - - public Number getCountSingleResult(final Class entityClass, FilterBean bean) { - return getSingleResult(ReckonType.COUNT, entityClass, null, bean); - } - - //-----------------------------AVG----------------------------- - public Number getAvgSingleResult(final Class entityClass, final String column) { - return getAvgSingleResult(entityClass, column, (FilterBean) null); - } - - public Number getAvgSingleResult(final Class entityClass, final String column, FilterBean bean) { - return getSingleResult(ReckonType.AVG, entityClass, column, bean); - } - - private Number getSingleResult(final ReckonType type, final Class entityClass, final String column, FilterBean bean) { - final EntityManager manager = factory.createEntityManager(); - try { - String sql = "SELECT " + type.name() + "(a." + column + ") FROM " + entityClass.getSimpleName() + " a"; - if (debug.get()) logger.finer(entityClass.getSimpleName() + " single sql=" + sql); - return manager.createQuery(sql, Number.class).getSingleResult(); - } finally { - manager.close(); - } - } - - private Number getSingleResultCriteria(final ReckonType type, final Class entityClass, final String column, FilterBean bean) { - final EntityManager manager = factory.createEntityManager(); - try { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - final CriteriaQuery cry = builder.createQuery(Number.class); - final Root root = cry.from(entityClass); - Expression where = bean == null ? null : createWhereExpression(builder, root, bean); - if (where != null) cry.where(where); - switch (type) { - case MAX: cry.select(builder.max(root.get(column))); - break; - case MIN: cry.select(builder.min(root.get(column))); - break; - case SUM: cry.select(builder.sum(root.get(column))); - break; - case AVG: cry.select(builder.avg(root.get(column))); - break; - case COUNT: cry.select(builder.count(column == null ? root : root.get(column))); - break; - default: throw new RuntimeException("error ReckonType"); - } - //max count - return manager.createQuery(cry).getSingleResult(); - } finally { - manager.close(); - } - } - - //-----------------------find---------------------------- - /** - * 根据主键获取对象 - * - * @param - * @param clazz - * @param pk - * @return - */ - public T find(Class clazz, Serializable pk) { - final EntityManager manager = factory.createEntityManager(); - try { - return manager.find(clazz, pk); - } finally { - manager.close(); - } - } - - /** - * 根据主键值集合获取对象集合 - * - * @param - * @param clazz - * @param ids - * @return - */ - public T[] find(Class clazz, Serializable... ids) { - final EntityManager manager = factory.createEntityManager(); - try { - T[] result = (T[]) Array.newInstance(clazz, ids.length); - int i = 0; - for (Serializable id : ids) { - result[i++] = manager.find(clazz, id); - } - return result; - } finally { - manager.close(); - } - } - - /** - * 根据唯一索引获取单个对象 - * - * @param - * @param clazz - * @param column - * @param key - * @return - */ - public T findByColumn(Class clazz, String column, Serializable key) { - final EntityManager manager = factory.createEntityManager(); - try { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - CriteriaQuery cd = builder.createQuery(clazz); - cd.where(builder.equal(cd.from(clazz).get(column), key)); - List list = manager.createQuery(cd).getResultList(); - return list == null || list.isEmpty() ? null : list.get(0); - } finally { - manager.close(); - } - } - - /** - * 根据两个字段的值获取单个对象 - * - * @param - * @param clazz - * @param column1 - * @param key1 - * @param column2 - * @param key2 - * @return - */ - public T findByTwoColumn(Class clazz, String column1, Serializable key1, String column2, Serializable key2) { - final EntityManager manager = factory.createEntityManager(); - try { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - final CriteriaQuery cd = builder.createQuery(clazz); - final Root root = cd.from(clazz); - cd.where(builder.and(builder.equal(root.get(column1), key1), builder.equal(root.get(column2), key2))); - return manager.createQuery(cd).getSingleResult(); - } finally { - manager.close(); - } - } - - /** - * 根据三个字段的值获取单个对象 - * - * @param - * @param clazz - * @param column1 - * @param key1 - * @param column2 - * @param key2 - * @param column3 - * @param key3 - * @return - */ - public T findByThreeColumn(Class clazz, String column1, Serializable key1, String column2, Serializable key2, String column3, Serializable key3) { - final EntityManager manager = factory.createEntityManager(); - try { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - final CriteriaQuery cd = builder.createQuery(clazz); - final Root root = cd.from(clazz); - cd.where(builder.and(builder.equal(root.get(column1), key1), builder.equal(root.get(column2), key2), builder.equal(root.get(column3), key3))); - return manager.createQuery(cd).getSingleResult(); - } finally { - manager.close(); - } - } - - /** - * 根据唯一索引获取对象 - * - * @param - * @param clazz - * @param column - * @param keys - * @return - */ - public T[] findByColumn(Class clazz, String column, Serializable... keys) { - return findByColumn(clazz, null, column, keys); - } - - /** - * 根据字段值拉去对象, 对象只填充或排除SelectColumn指定的字段 - * - * @param - * @param clazz - * @param selects 只拉起指定字段名或者排除指定字段名的值 - * @param column - * @param keys - * @return - */ - public T[] findByColumn(Class clazz, final SelectColumn selects, String column, Serializable... keys) { - final EntityManager manager = factory.createEntityManager(); - try { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - CriteriaQuery cd = builder.createQuery(clazz); - cd.where(cd.from(clazz).get(column).in((Object[]) keys)); - List list = manager.createQuery(cd).getResultList(); - list = selectList(clazz, selects, list); - return list.toArray((T[]) Array.newInstance(clazz, list.size())); - } finally { - manager.close(); - } - } - - //-----------------------list---------------------------- - /** - * 根据指定字段值查询对象某个字段的集合 - * - * @param - * @param - * @param selectedColumn - * @param clazz - * @param column - * @param key - * @return - */ - public List queryColumnList(String selectedColumn, Class clazz, String column, Serializable key) { - final EntityManager manager = factory.createEntityManager(); - try { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - final CriteriaQuery query = builder.createQuery(); - final Root root = query.from(clazz); - query.select(root.get(selectedColumn)); - query.where(builder.equal(root.get(column), key)); - return manager.createQuery(query).getResultList(); - } finally { - manager.close(); - } - } - - /** - * 根据指定字段值查询对象集合 - * - * @param - * @param clazz - * @param column - * @param key - * @return - */ - public List queryList(Class clazz, String column, Serializable key) { - return queryList(clazz, (SelectColumn) null, column, key); - } - - /** - * 根据指定字段值查询对象集合 - * - * @param - * @param clazz - * @param column - * @param express - * @param key - * @return - */ - public List queryList(Class clazz, String column, FilterExpress express, Serializable key) { - return queryList(clazz, (SelectColumn) null, column, express, key); - } - - /** - * 根据指定字段值查询对象集合, 对象只填充或排除SelectColumn指定的字段 - * - * @param - * @param clazz - * @param selects - * @param column - * @param key - * @return - */ - public List queryList(Class clazz, final SelectColumn selects, String column, Serializable key) { - return queryList(clazz, selects, column, FilterExpress.EQUAL, key); - } - - /** - * 注意: 尚未实现识别express功能 根据指定字段值查询对象集合, 对象只填充或排除SelectColumn指定的字段 - * - * @param - * @param clazz - * @param selects - * @param column - * @param express - * @param key - * @return - */ - public List queryList(Class clazz, final SelectColumn selects, String column, FilterExpress express, Serializable key) { - final EntityManager manager = factory.createEntityManager(); - try { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - CriteriaQuery cd = builder.createQuery(clazz); - cd.where(builder.equal(cd.from(clazz).get(column), key)); - List list = manager.createQuery(cd).getResultList(); - return selectList(clazz, selects, list); - } finally { - manager.close(); - } - } - - /** - * 根据过滤对象FilterBean查询对象集合 - * - * @param - * @param clazz - * @param bean - * @return - */ - public List queryList(final Class clazz, final FilterBean bean) { - return queryList(clazz, null, bean); - } - - /** - * 根据过滤对象FilterBean查询对象集合, 对象只填充或排除SelectColumn指定的字段 - * - * @param - * @param clazz - * @param selects - * @param bean - * @return - */ - public List queryList(final Class clazz, final SelectColumn selects, final FilterBean bean) { - final EntityManager manager = factory.createEntityManager(); - try { - final CriteriaBuilder builder = manager.getCriteriaBuilder(); - CriteriaQuery cd = builder.createQuery(clazz); - final Expression where = createWhereExpression(builder, cd.from(clazz), bean); - if (where != null) cd.where(where); - List list = manager.createQuery(cd).getResultList(); - return selectList(clazz, selects, list); - } finally { - manager.close(); - } - } - - //-----------------------sheet---------------------------- - /** - * 根据过滤对象FilterBean和翻页对象Flipper查询一页的数据 - * - * @param - * @param clazz - * @param flipper - * @param bean - * @return - */ - public Sheet querySheet(Class clazz, final Flipper flipper, final FilterBean bean) { - return querySheet(clazz, null, flipper, bean); - } - - /** - * 根据过滤对象FilterBean和翻页对象Flipper查询一页的数据, 对象只填充或排除SelectColumn指定的字段 - * - * @param - * @param clazz - * @param selects - * @param flipper - * @param bean - * @return - */ - public Sheet querySheet(Class clazz, final SelectColumn selects, final Flipper flipper, final FilterBean bean) { - final EntityManager manager = factory.createEntityManager(); - try { - final CriteriaBuilder totalbd = manager.getCriteriaBuilder(); - final CriteriaQuery totalcry = totalbd.createQuery(Long.class); - final Root totalroot = totalcry.from(clazz); - totalcry.select(totalbd.count(totalroot)); - Expression totalwhere = createWhereExpression(totalbd, totalroot, bean); - if (totalwhere != null) totalcry.where(totalwhere); - long total = manager.createQuery(totalcry).getSingleResult(); - if (total < 1) return new Sheet<>(); - //------------------------------------------------ - final CriteriaBuilder listbd = manager.getCriteriaBuilder(); - final CriteriaQuery listcry = listbd.createQuery(clazz); - final Root listroot = listcry.from(clazz); - Expression listwhere = createWhereExpression(listbd, listroot, bean); - if (listwhere != null) listcry.where(listwhere); - final String sort = flipper.getSort(); - if (flipper != null && sort != null && !sort.isEmpty()) { - if (sort.indexOf(',') > 0) { - List orders = new ArrayList<>(); - for (String item : sort.split(",")) { - if (item.isEmpty()) continue; - String[] sub = item.split("\\s+"); - if (sub.length < 2 || sub[1].equalsIgnoreCase("ASC")) { - orders.add(listbd.asc(listroot.get(sub[0]))); - } else { - orders.add(listbd.desc(listroot.get(sub[0]))); - } - } - listcry.orderBy(orders); - } else { - for (String item : sort.split(",")) { - if (item.isEmpty()) continue; - String[] sub = item.split("\\s+"); - if (sub.length < 2 || sub[1].equalsIgnoreCase("ASC")) { - listcry.orderBy(listbd.asc(listroot.get(sub[0]))); - } else { - listcry.orderBy(listbd.desc(listroot.get(sub[0]))); - } - } - } - } - final TypedQuery listqry = manager.createQuery(listcry); - if (flipper != null) { - listqry.setFirstResult(flipper.index()); - listqry.setMaxResults(flipper.getSize()); - } - List list = selectList(clazz, selects, listqry.getResultList()); - return new Sheet<>(total, list); - } finally { - manager.close(); - } - } - - private List selectList(final Class clazz, final SelectColumn selects, final List list) { - if (selects == null || selects.isEmpty() || list.isEmpty()) return list; - final EntityInfo info = EntityInfo.load(clazz, 0, null); - final Object dftValue = info.getCreator().create(); - final Map map = info.getAttributes(); - final List attrs = new ArrayList<>(); - if (selects.isExcludable()) { - for (String col : selects.getColumns()) { - Attribute attr = map.get(col); - if (attr != null) attrs.add(attr); - } - } else { - map.entrySet().forEach(x -> { - if (!selects.validate(x.getKey())) attrs.add(x.getValue()); - }); - } - for (Object obj : list) { - for (Attribute attr : attrs) { - attr.set(obj, attr.get(dftValue)); - } - } - return list; - } - - private Expression createWhereExpression(final CriteriaBuilder builder, final Root root, final FilterBean bean) { - if (bean == null) return null; - final FilterInfo filter = FilterInfo.load(bean.getClass(), this); - final List list = new ArrayList<>(); - for (final FilterInfo.FilterItem item : filter.getFilters()) { - final FilterExpress express = item.express; - Object attrval = item.attribute.get(bean); - if (express == FilterExpress.ISNULL) { - list.add(builder.isNull(root.get(item.attribute.field()))); - continue; - } else if (express == FilterExpress.ISNOTNULL) { - list.add(builder.isNotNull(root.get(item.attribute.field()))); - continue; - } - if (attrval == null) continue; - if (item.number && ((Number) attrval).longValue() < item.least) continue; - switch (express) { - case EQUAL: - list.add(builder.equal(root.get(item.attribute.field()), attrval)); - break; - case NOTEQUAL: - list.add(builder.notEqual(root.get(item.attribute.field()), attrval)); - break; - case GREATERTHAN: - list.add(builder.greaterThan(root.get(item.attribute.field()), (Comparable) attrval)); - break; - case LESSTHAN: - list.add(builder.lessThan(root.get(item.attribute.field()), (Comparable) attrval)); - break; - case GREATERTHANOREQUALTO: - list.add(builder.greaterThanOrEqualTo(root.get(item.attribute.field()), (Comparable) attrval)); - break; - case LESSTHANOREQUALTO: - list.add(builder.lessThanOrEqualTo(root.get(item.attribute.field()), (Comparable) attrval)); - break; - case LIKE: - list.add(builder.like(root.get(item.attribute.field()), (String) (item.likefit ? ("%" + attrval + "%") : attrval))); - break; - case NOTLIKE: - list.add(builder.notLike(root.get(item.attribute.field()), (String) (item.likefit ? ("%" + attrval + "%") : attrval))); - break; - case BETWEEN: - case NOTBETWEEN: - Range range = (Range) attrval; - Predicate p = builder.between(root.get(item.attribute.field()), (Comparable) range.getMin(), (Comparable) range.getMax()); - if (NOTBETWEEN == express) { - p = builder.not(p); - } - list.add(p); - break; - case AND: - case OR: - Range[] ranges = (Range[]) attrval; - Predicate[] ps = new Predicate[ranges.length]; - int oi = 0; - for (Range r : ranges) { - ps[oi++] = builder.between(root.get(item.attribute.field()), (Comparable) r.getMin(), (Comparable) r.getMax()); - } - if (OR == express) { - list.add(builder.or(ps)); - } else { - list.add(builder.and(ps)); - } - break; - case IN: - case NOTIN: - Predicate pd = null; - if (attrval instanceof Collection) { - Collection c = (Collection) attrval; - if (!c.isEmpty()) { - pd = builder.in(root.get(item.attribute.field()).in(c)); - } - } else { - final int len = Array.getLength(attrval); - if (len > 0) { - Class comp = attrval.getClass().getComponentType(); - if (comp.isPrimitive()) { - Object[] os = new Object[len]; - for (int i = 0; i < len; i++) { - os[i] = Array.get(attrval, i); - } - pd = builder.in(root.get(item.attribute.field()).in(os)); - } else { - pd = builder.in(root.get(item.attribute.field()).in((Object[]) attrval)); - } - if (NOTIN == express) { - pd = builder.not(pd); - } - list.add(pd); - } - } - if (pd != null) { - if (NOTIN == express) { - pd = builder.not(pd); - } - list.add(pd); - } - break; - default: - throw new RuntimeException(bean.getClass() + "'s field (" + item.aliasfield + ") have a illegal express (" + express + ")"); - } - } - if (list.isEmpty()) return null; - return builder.and(list.toArray(new Predicate[list.size()])); - } - - private static enum ReckonType { - - MAX, MIN, SUM, COUNT, AVG; - } -} diff --git a/src/javax/persistence/Cacheable.java b/src/javax/persistence/Cacheable.java new file mode 100644 index 000000000..431fdca05 --- /dev/null +++ b/src/javax/persistence/Cacheable.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * Linda DeMichiel - Java Persistence 2.0 + * + ******************************************************************************/ +package javax.persistence; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Specifies whether an entity should be cached if caching is enabled + * when the value of the persistence.xml caching element + * is ENABLE_SELECTIVE or DISABLE_SELECTIVE. + * The value of the Cacheable annotation is inherited by + * subclasses; it can be overridden by specifying + * Cacheable on a subclass. + * + *

Cacheable(false) means that the entity and its state must + * not be cached by the provider. + * + * @since Java Persistence 2.0 + */ +@Target( { TYPE }) +@Retention(RUNTIME) +public @interface Cacheable { + + /** + * (Optional) Whether or not the entity should be cached. + */ + boolean value() default true; +} diff --git a/src/javax/persistence/Column.java b/src/javax/persistence/Column.java new file mode 100644 index 000000000..adc030b17 --- /dev/null +++ b/src/javax/persistence/Column.java @@ -0,0 +1,122 @@ +/******************************************************************************* + * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * Linda DeMichiel - Java Persistence 2.0 + * + ******************************************************************************/ +package javax.persistence; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Specifies the mapped column for a persistent property or field. + * If no Column annotation is specified, the default values apply. + * + *

+ *    Example 1:
+ *
+ *    @Column(name="DESC", nullable=false, length=512)
+ *    public String getDescription() { return description; }
+ *
+ *    Example 2:
+ *
+ *    @Column(name="DESC",
+ *            columnDefinition="CLOB NOT NULL",
+ *            table="EMP_DETAIL")
+ *    @Lob
+ *    public String getDescription() { return description; }
+ *
+ *    Example 3:
+ *
+ *    @Column(name="ORDER_COST", updatable=false, precision=12, scale=2)
+ *    public BigDecimal getCost() { return cost; }
+ *
+ * 
+ * + * + * @since Java Persistence 1.0 + */ +@Target({METHOD, FIELD}) +@Retention(RUNTIME) +public @interface Column { + + /** + * (Optional) The name of the column. Defaults to + * the property or field name. + */ + String name() default ""; + + /** + * (Optional) Whether the column is a unique key. This is a + * shortcut for the UniqueConstraint annotation at the table + * level and is useful for when the unique key constraint + * corresponds to only a single column. This constraint applies + * in addition to any constraint entailed by primary key mapping and + * to constraints specified at the table level. + */ + boolean unique() default false; + + /** + * (Optional) Whether the database column is nullable. + */ + boolean nullable() default true; + + /** + * (Optional) Whether the column is included in SQL INSERT + * statements generated by the persistence provider. + */ + boolean insertable() default true; + + /** + * (Optional) Whether the column is included in SQL UPDATE + * statements generated by the persistence provider. + */ + boolean updatable() default true; + + /** + * (Optional) The SQL fragment that is used when + * generating the DDL for the column. + *

Defaults to the generated SQL to create a + * column of the inferred type. + */ + String columnDefinition() default ""; + + /** + * (Optional) The name of the table that contains the column. + * If absent the column is assumed to be in the primary table. + */ + String table() default ""; + + /** + * (Optional) The column length. (Applies only if a + * string-valued column is used.) + */ + int length() default 255; + + /** + * (Optional) The precision for a decimal (exact numeric) + * column. (Applies only if a decimal column is used.) + * Value must be set by developer if used when generating + * the DDL for the column. + */ + int precision() default 0; + + /** + * (Optional) The scale for a decimal (exact numeric) column. + * (Applies only if a decimal column is used.) + */ + int scale() default 0; +} diff --git a/src/javax/persistence/Entity.java b/src/javax/persistence/Entity.java new file mode 100644 index 000000000..1dda0a3ab --- /dev/null +++ b/src/javax/persistence/Entity.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * Linda DeMichiel - Java Persistence 2.0 + * + ******************************************************************************/ +package javax.persistence; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import java.lang.annotation.Documented; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Specifies that the class is an entity. This annotation is applied to the + * entity class. + * + * @since Java Persistence 1.0 + */ +@Documented +@Target(TYPE) +@Retention(RUNTIME) +public @interface Entity { + + /** + * (Optional) The entity name. Defaults to the unqualified + * name of the entity class. This name is used to refer to the + * entity in queries. The name must not be a reserved literal + * in the Java Persistence query language. + */ + String name() default ""; +} diff --git a/src/javax/persistence/GeneratedValue.java b/src/javax/persistence/GeneratedValue.java new file mode 100644 index 000000000..9c90d80af --- /dev/null +++ b/src/javax/persistence/GeneratedValue.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * Linda DeMichiel - Java Persistence 2.0 + * + ******************************************************************************/ +package javax.persistence; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import static javax.persistence.GenerationType.AUTO; + +/** + * Provides for the specification of generation strategies for the + * values of primary keys. + * + *

The GeneratedValue annotation + * may be applied to a primary key property or field of an entity or + * mapped superclass in conjunction with the {@link Id} annotation. + * The use of the GeneratedValue annotation is only + * required to be supported for simple primary keys. Use of the + * GeneratedValue annotation is not supported for derived + * primary keys. + * + *

+ *
+ *     Example 1:
+ *
+ *     @Id
+ *     @GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")
+ *     @Column(name="CUST_ID")
+ *     public Long getId() { return id; }
+ *
+ *     Example 2:
+ *
+ *     @Id
+ *     @GeneratedValue(strategy=TABLE, generator="CUST_GEN")
+ *     @Column(name="CUST_ID")
+ *     Long id;
+ * 
+ * + * @see Id + * @see TableGenerator + * @see SequenceGenerator + * + * @since Java Persistence 1.0 + */ +@Target({METHOD, FIELD}) +@Retention(RUNTIME) + +public @interface GeneratedValue { + + /** + * (Optional) The primary key generation strategy + * that the persistence provider must use to + * generate the annotated entity primary key. + */ + GenerationType strategy() default AUTO; + + /** + * (Optional) The name of the primary key generator + * to use as specified in the {@link SequenceGenerator} + * or {@link TableGenerator} annotation. + *

Defaults to the id generator supplied by persistence provider. + */ + String generator() default ""; +} diff --git a/src/javax/persistence/GenerationType.java b/src/javax/persistence/GenerationType.java new file mode 100644 index 000000000..9d1980e83 --- /dev/null +++ b/src/javax/persistence/GenerationType.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * Linda DeMichiel - Java Persistence 2.0 + * + ******************************************************************************/ +package javax.persistence; + +/** + * Defines the types of primary key generation strategies. + * + * @see GeneratedValue + * + * @since Java Persistence 1.0 + */ +public enum GenerationType { + + /** + * Indicates that the persistence provider must assign + * primary keys for the entity using an underlying + * database table to ensure uniqueness. + */ + TABLE, + + /** + * Indicates that the persistence provider must assign + * primary keys for the entity using a database sequence. + */ + SEQUENCE, + + /** + * Indicates that the persistence provider must assign + * primary keys for the entity using a database identity column. + */ + IDENTITY, + + /** + * Indicates that the persistence provider should pick an + * appropriate strategy for the particular database. The + * AUTO generation strategy may expect a database + * resource to exist, or it may attempt to create one. A vendor + * may provide documentation on how to create such resources + * in the event that it does not support schema generation + * or cannot create the schema resource at runtime. + */ + AUTO +} diff --git a/src/javax/persistence/Id.java b/src/javax/persistence/Id.java new file mode 100644 index 000000000..a201843be --- /dev/null +++ b/src/javax/persistence/Id.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * Linda DeMichiel - Java Persistence 2.0 + * + ******************************************************************************/ +package javax.persistence; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Specifies the primary key of an entity. + * The field or property to which the Id annotation is applied + * should be one of the following types: any Java primitive type; + * any primitive wrapper type; + * String; + * java.util.Date; + * java.sql.Date; + * java.math.BigDecimal; + * java.math.BigInteger. + * + *

The mapped column for the primary key of the entity is assumed + * to be the primary key of the primary table. If no Column annotation + * is specified, the primary key column name is assumed to be the name + * of the primary key property or field. + * + *

+ *   Example:
+ *
+ *   @Id
+ *   public Long getId() { return id; }
+ * 
+ * + * @see Column + * @see GeneratedValue + * + * @since Java Persistence 1.0 + */ +@Target({METHOD, FIELD}) +@Retention(RUNTIME) + +public @interface Id {} diff --git a/src/javax/persistence/Index.java b/src/javax/persistence/Index.java new file mode 100644 index 000000000..5899a0330 --- /dev/null +++ b/src/javax/persistence/Index.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2011 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * + ******************************************************************************/ +package javax.persistence; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Used in schema generation to specify creation of an index. + *

+ * Note that it is not necessary to specify an index for a primary key, + * as the primary key index will be created automatically. + * + *

+ * The syntax of the columnList element is a + * column_list, as follows: + * + *

+ *    column::= index_column [,index_column]*
+ *    index_column::= column_name [ASC | DESC]
+ * 
+ * + *

If ASC or DESC is not specified, + * ASC (ascending order) is assumed. + * + * @see Table + * @see SecondaryTable + * @see CollectionTable + * @see JoinTable + * @see TableGenerator + * + * @since Java Persistence 2.1 + * + */ +@Target({}) +@Retention(RUNTIME) +public @interface Index { + + /** + * (Optional) The name of the index; defaults to a provider-generated name. + */ + String name() default ""; + + /** + * (Required) The names of the columns to be included in the index, + * in order. + */ + String columnList(); + + /** + * (Optional) Whether the index is unique. + */ + boolean unique() default false; + +} diff --git a/src/javax/persistence/Table.java b/src/javax/persistence/Table.java new file mode 100644 index 000000000..e92b77ca7 --- /dev/null +++ b/src/javax/persistence/Table.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * Linda DeMichiel - Java Persistence 2.0 + * + ******************************************************************************/ +package javax.persistence; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Specifies the primary table for the annotated entity. Additional + * tables may be specified using {@link SecondaryTable} or {@link + * SecondaryTables} annotation. + * + *

If no Table annotation is specified for an entity + * class, the default values apply. + * + *

+ *    Example:
+ *
+ *    @Entity
+ *    @Table(name="CUST", schema="RECORDS")
+ *    public class Customer { ... }
+ * 
+ * + * @since Java Persistence 1.0 + */ +@Target(TYPE) +@Retention(RUNTIME) +public @interface Table { + + /** + * (Optional) The name of the table. + *

Defaults to the entity name. + */ + String name() default ""; + + /** (Optional) The catalog of the table. + *

Defaults to the default catalog. + */ + String catalog() default ""; + + /** (Optional) The schema of the table. + *

Defaults to the default schema for user. + */ + String schema() default ""; + + /** + * (Optional) Unique constraints that are to be placed on + * the table. These are only used if table generation is in + * effect. These constraints apply in addition to any constraints + * specified by the Column and JoinColumn + * annotations and constraints entailed by primary key mappings. + *

Defaults to no additional constraints. + */ + UniqueConstraint[] uniqueConstraints() default {}; + + /** + * (Optional) Indexes for the table. These are only used if + * table generation is in effect. Note that it is not necessary + * to specify an index for a primary key, as the primary key + * index will be created automatically. + * + * @since Java Persistence 2.1 + */ + Index[] indexes() default {}; +} diff --git a/src/javax/persistence/Transient.java b/src/javax/persistence/Transient.java new file mode 100644 index 000000000..81446afd7 --- /dev/null +++ b/src/javax/persistence/Transient.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * Linda DeMichiel - Java Persistence 2.0 + * + ******************************************************************************/ +package javax.persistence; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Specifies that the property or field is not persistent. It is used + * to annotate a property or field of an entity class, mapped + * superclass, or embeddable class. + * + *

+ *    Example:
+ *
+ *    @Entity
+ *    public class Employee {
+ *        @Id int id;
+ *        @Transient User currentUser;
+ *        ...
+ *    }
+ * 
+ * + * @since Java Persistence 1.0 + */ +@Target({METHOD, FIELD}) +@Retention(RUNTIME) + +public @interface Transient {} diff --git a/src/javax/persistence/UniqueConstraint.java b/src/javax/persistence/UniqueConstraint.java new file mode 100644 index 000000000..2351b4511 --- /dev/null +++ b/src/javax/persistence/UniqueConstraint.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2008 - 2013 Oracle Corporation. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 + * which accompanies this distribution. + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Linda DeMichiel - Java Persistence 2.1 + * Linda DeMichiel - Java Persistence 2.0 + * + ******************************************************************************/ +package javax.persistence; + +import java.lang.annotation.Target; +import java.lang.annotation.Retention; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Specifies that a unique constraint is to be included in + * the generated DDL for a primary or secondary table. + * + *
+ *    Example:
+ *    @Entity
+ *    @Table(
+ *        name="EMPLOYEE", 
+ *        uniqueConstraints=
+ *            @UniqueConstraint(columnNames={"EMP_ID", "EMP_NAME"})
+ *    )
+ *    public class Employee { ... }
+ * 
+ * + * @since Java Persistence 1.0 + */ +@Target({}) +@Retention(RUNTIME) +public @interface UniqueConstraint { + + /** (Optional) Constraint name. A provider-chosen name will be chosen + * if a name is not specified. + * + * @since Java Persistence 2.0 + */ + String name() default ""; + + /** (Required) An array of the column names that make up the constraint. */ + String[] columnNames(); +}