entitytype.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,848 行 · 第 1/3 页
JAVA
1,848 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Rodrigo Westrupp */package com.caucho.amber.type;import com.caucho.amber.AmberRuntimeException;import com.caucho.amber.cfg.AbstractConfigIntrospector;import com.caucho.amber.entity.AmberCompletion;import com.caucho.amber.entity.AmberEntityHome;import com.caucho.amber.entity.Entity;import com.caucho.amber.entity.EntityItem;import com.caucho.amber.entity.Listener;import com.caucho.amber.field.*;import com.caucho.amber.gen.EntityComponent;import com.caucho.amber.idgen.AmberTableGenerator;import com.caucho.amber.idgen.IdGenerator;import com.caucho.amber.idgen.SequenceIdGenerator;import com.caucho.amber.manager.AmberConnection;import com.caucho.amber.manager.AmberPersistenceUnit;import com.caucho.amber.table.AmberColumn;import com.caucho.amber.table.AmberTable;import com.caucho.config.ConfigException;import com.caucho.jdbc.*;import com.caucho.java.JavaWriter;import com.caucho.java.gen.ClassComponent;import com.caucho.lifecycle.Lifecycle;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import java.io.IOException;import java.lang.reflect.*;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.logging.Logger;/** * Base for entity or mapped-superclass types. */public class EntityType extends BeanType { private static final Logger log = Logger.getLogger(EntityType.class.getName()); private static final L10N L = new L10N(EntityType.class); private EntityType _parentType; AmberTable _table; private String _rootTableName; private ArrayList<AmberTable> _secondaryTables = new ArrayList<AmberTable>(); private ArrayList<ListenerType> _listeners = new ArrayList<ListenerType>(); private ArrayList<ListenerCallback> _postLoadCallbacks = new ArrayList<ListenerCallback>(); private ArrayList<ListenerCallback> _prePersistCallbacks = new ArrayList<ListenerCallback>(); private ArrayList<ListenerCallback> _postPersistCallbacks = new ArrayList<ListenerCallback>(); private ArrayList<ListenerCallback> _preUpdateCallbacks = new ArrayList<ListenerCallback>(); private ArrayList<ListenerCallback> _postUpdateCallbacks = new ArrayList<ListenerCallback>(); private ArrayList<ListenerCallback> _preRemoveCallbacks = new ArrayList<ListenerCallback>(); private ArrayList<ListenerCallback> _postRemoveCallbacks = new ArrayList<ListenerCallback>(); private Id _id; private String _discriminatorValue; private boolean _isJoinedSubClass; private HashSet<String> _eagerFieldNames; private HashMap<String,EntityType> _subEntities; private ArrayList<AmberField> _mappedSuperclassFields = new ArrayList<AmberField>(); private ArrayList<AmberField> _fields; private boolean _hasDependent; private Class _proxyClass; private AmberEntityHome _home; protected int _defaultLoadGroupIndex; protected int _loadGroupIndex; protected int _minDirtyIndex; protected int _dirtyIndex; private boolean _excludeDefaultListeners; private boolean _excludeSuperclassListeners; protected boolean _hasLoadCallback; private HashMap<String,IdGenerator> _idGenMap = new HashMap<String,IdGenerator>(); private final Lifecycle _lifecycle = new Lifecycle(); private VersionField _versionField; private int _flushPriority; private boolean _isIdentityGenerator; private boolean _isSequenceGenerator; public EntityType(AmberPersistenceUnit amberPersistenceUnit) { super(amberPersistenceUnit); } /** * returns true for a loadable entity */ @Override public boolean isEntity() { return ! Modifier.isAbstract(getBeanClass().getModifiers()); } /** * Sets the table. */ public void setTable(AmberTable table) { _table = table; // jpa/0gg0 if (table == null) return; table.setType(this); if (_rootTableName == null) _rootTableName = table.getName(); } /** * Returns the table. */ public AmberTable getTable() { // jpa/0gg0 if (_table == null && ! isAbstractClass()) { String sqlName = AbstractConfigIntrospector.toSqlName(getName()); setTable(_amberPersistenceUnit.createTable(sqlName)); } return _table; } /** * Gets the instance class. */ public Class getInstanceClass() { return getInstanceClass(Entity.class); } /** * Returns the component interface name. */ @Override public String getComponentInterfaceName() { return "com.caucho.amber.entity.Entity"; } /** * Gets a component generator. */ @Override public ClassComponent getComponentGenerator() { return new EntityComponent(); } /** * Returns the flush priority. */ public int getFlushPriority() { return _flushPriority; } /** * Adds a mapped superclass field. */ public void addMappedSuperclassField(AmberField field) { if (_mappedSuperclassFields.contains(field)) return; _mappedSuperclassFields.add(field); Collections.sort(_mappedSuperclassFields, new AmberFieldCompare()); } /** * Returns the mapped superclass fields. */ public ArrayList<AmberField> getMappedSuperclassFields() { return _mappedSuperclassFields; } /** * Returns the mapped superclass field with a given name. */ public AmberField getMappedSuperclassField(String name) { for (int i = 0; i < _mappedSuperclassFields.size(); i++) { AmberField field = _mappedSuperclassFields.get(i); if (field.getName().equals(name)) return field; } return null; } /** * returns the merged fields */ @Override public ArrayList<AmberField> getFields() { if (_fields != null) return _fields; else return super.getFields(); } /** * Returns the root table name. */ public String getRootTableName() { return _rootTableName; } /** * Sets the root table name. */ public void setRootTableName(String rootTableName) { _rootTableName = rootTableName; } /** * Returns the version field. */ public VersionField getVersionField() { return _versionField; } /** * Sets the version field. */ public void setVersionField(VersionField versionField) { addField(versionField); _versionField = versionField; } /** * Adds a secondary table. */ public void addSecondaryTable(AmberTable table) { if (! _secondaryTables.contains(table)) { _secondaryTables.add(table); } table.setType(this); } /** * Gets the secondary tables. */ public ArrayList<AmberTable> getSecondaryTables() { return _secondaryTables; } /** * Adds an entity listener. */ public void addListener(ListenerType listener) { if (_listeners.contains(listener)) return; _listeners.add(listener); } /** * Gets the entity listeners. */ public ArrayList<ListenerType> getListeners() { return _listeners; } /** * Gets a secondary table. */ public AmberTable getSecondaryTable(String name) { for (AmberTable table : _secondaryTables) { if (table.getName().equals(name)) return table; } return null; } /** * Returns true if and only if it has a * many-to-one, one-to-one or embedded field/property. */ public boolean hasDependent() { return _hasDependent; } /** * Sets true if and only if it has a * many-to-one, one-to-one or embedded field/property. */ public void setHasDependent(boolean hasDependent) { _hasDependent = hasDependent; } /** * Returns the java type. */ @Override public String getForeignTypeName() { return getId().getForeignTypeName(); } /** * Gets the proxy class. */ public Class getProxyClass() { if (_proxyClass != null) return _proxyClass; else return _tBeanClass; } /** * Gets the proxy class. */ public void setProxyClass(Class proxyClass) { _proxyClass = proxyClass; } /** * Returns true if the corresponding class is abstract. */ public boolean isAbstractClass() { // ejb/0600 - EJB 2.1 are not abstract in this sense return (Modifier.isAbstract(getBeanClass().getModifiers()) && _proxyClass == null); } /** * Sets the id. */ public void setId(Id id) { _id = id; } /** * Returns the id. */ public Id getId() { return _id; } /** * Set true for joined-subclass */ public void setJoinedSubClass(boolean isJoinedSubClass) { _isJoinedSubClass = isJoinedSubClass; } /** * Set true for joined-subclass */ public boolean isJoinedSubClass() { if (getParentType() != null) return getParentType().isJoinedSubClass(); else return _isJoinedSubClass; } /** * Sets the discriminator value. */ public String getDiscriminatorValue() { if (_discriminatorValue != null) return _discriminatorValue; else { return getBeanClass().getSimpleName(); } } /** * Sets the discriminator value. */ public void setDiscriminatorValue(String value) { _discriminatorValue = value; } /** * Returns true if read-only */ public boolean isReadOnly() { return getTable().isReadOnly(); } /** * Sets true if read-only */ public void setReadOnly(boolean isReadOnly) { getTable().setReadOnly(isReadOnly); } /** * Returns the cache timeout. */ public long getCacheTimeout() { return getTable().getCacheTimeout(); } /** * Sets the cache timeout. */ public void setCacheTimeout(long timeout) { getTable().setCacheTimeout(timeout); } /** * Adds a new field. */ @Override public void addField(AmberField field) { super.addField(field); if (! field.isLazy()) { if (_eagerFieldNames == null) _eagerFieldNames = new HashSet<String>(); _eagerFieldNames.add(field.getName()); } } /** * Gets the EAGER field names. */ public HashSet<String> getEagerFieldNames() { return _eagerFieldNames; } /** * Returns the field with a given name. */ @Override public AmberField getField(String name) { if (_id != null) { ArrayList<IdField> keys = _id.getKeys(); for (int i = 0; i < keys.size(); i++) { IdField key = keys.get(i); if (key.getName().equals(name)) return key; } } return super.getField(name); } /** * Returns the columns. */ public ArrayList<AmberColumn> getColumns() { // jpa/0gg0 if (getTable() == null) return null; return getTable().getColumns(); } /** * Gets the exclude default listeners flag. */ public boolean getExcludeDefaultListeners() { return _excludeDefaultListeners; } /** * Sets the exclude default listeners flag. */ public void setExcludeDefaultListeners(boolean b) { _excludeDefaultListeners = b; } /** * Gets the exclude superclass listeners flag. */ public boolean getExcludeSuperclassListeners() { return _excludeSuperclassListeners; } /** * Sets the exclude superclass listeners flag. */ public void setExcludeSuperclassListeners(boolean b) { _excludeSuperclassListeners = b; } /** * True if the load lifecycle callback should be generated. */ public void setHasLoadCallback(boolean hasCallback) { _hasLoadCallback = hasCallback; } /** * True if the load lifecycle callback should be generated. */ public boolean getHasLoadCallback() { return _hasLoadCallback; } /** * Returns the root type. */ public EntityType getRootType() { EntityType parent = getParentType(); if (parent != null)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?