entitytype.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,848 行 · 第 1/3 页
JAVA
1,848 行
return parent.getRootType(); else return this; } /** * Returns the parent type. */ public EntityType getParentType() { return _parentType; } /** * Returns the parent type. */ public void setParentType(EntityType parentType) { _parentType = parentType; } /** * Adds a sub-class. */ public void addSubClass(EntityType type) { if (_subEntities == null) _subEntities = new HashMap<String,EntityType>(); _subEntities.put(type.getDiscriminatorValue(), type); } /** * Gets a sub-class. */ public EntityType getSubClass(String discriminator) { if (_subEntities == null) return this; EntityType subType = _subEntities.get(discriminator); if (subType != null) return subType; else { // jpa/0l15 for (EntityType subEntity : _subEntities.values()) { subType = subEntity.getSubClass(discriminator); if (subType != subEntity) return subType; } return this; } } /** * Creates a new entity for this specific instance type. */ public Entity createBean() { try { Entity entity = (Entity) getInstanceClass().newInstance(); return entity; } catch (Exception e) { throw new AmberRuntimeException(e); } } /** * Returns the home. */ public AmberEntityHome getHome() { if (_home == null) { _home = getPersistenceUnit().getEntityHome(getName()); } return _home; } /** * Returns the next load group. */ public int nextLoadGroupIndex() { int nextLoadGroupIndex = getLoadGroupIndex() + 1; _loadGroupIndex = nextLoadGroupIndex; return nextLoadGroupIndex; } /** * Returns the current load group. */ public int getLoadGroupIndex() { return _loadGroupIndex; } /** * Sets the next default loadGroupIndex */ public void nextDefaultLoadGroupIndex() { _defaultLoadGroupIndex = nextLoadGroupIndex(); } /** * Returns the current load group. */ public int getDefaultLoadGroupIndex() { return _defaultLoadGroupIndex; } /** * Returns true if the load group is owned by this type (not a subtype). */ public boolean isLoadGroupOwnedByType(int i) { return getDefaultLoadGroupIndex() <= i && i <= getLoadGroupIndex(); } /** * Returns the next dirty index */ public int nextDirtyIndex() { int dirtyIndex = getDirtyIndex(); _dirtyIndex = dirtyIndex + 1; return dirtyIndex; } /** * Returns the current dirty group. */ public int getDirtyIndex() { return _dirtyIndex; } /** * Returns the min dirty group. */ public int getMinDirtyIndex() { return _minDirtyIndex; } /** * Returns true if the load group is owned by this type (not a subtype). */ public boolean isDirtyIndexOwnedByType(int i) { return getMinDirtyIndex() <= i && i < getDirtyIndex(); } /** * Initialize the entity. */ @Override public void init() throws ConfigException { if (getConfigException() != null) return; if (! _lifecycle.toInit()) return; super.init(); // forces table lazy load getTable(); initId(); _fields = getMergedFields(); for (AmberField field : getFields()) { if (field.isUpdateable()) field.setIndex(nextDirtyIndex()); field.init(); } if (getMappedSuperclassFields() == null) return; for (AmberField field : getMappedSuperclassFields()) { if (field.isUpdateable()) field.setIndex(nextDirtyIndex()); field.init(); } } protected void initId() { assert getId() != null : "null id for " + getName(); getId().init(); } protected ArrayList<AmberField> getMergedFields() { ArrayList<AmberField> mappedFields = getMappedSuperclassFields(); if (mappedFields == null) return getFields(); ArrayList<AmberField> resultFields = new ArrayList<AmberField>(); resultFields.addAll(getFields()); for (AmberField field : mappedFields) { resultFields.add(field); } Collections.sort(resultFields, new AmberFieldCompare()); return resultFields; } /** * Start the entry. */ public void start() throws ConfigException { init(); startGenerator(); startImpl(); if (! _lifecycle.toActive()) return; } private void startGenerator() { IdField idGenField = getId().getGeneratedIdField(); if (idGenField == null) return; JdbcMetaData md = getPersistenceUnit().getMetaData(); if ("sequence".equals(idGenField.getGenerator())) { _isIdentityGenerator = false; _isSequenceGenerator = true; if (! md.supportsSequences()) throw new ConfigException(L.l("'{0}' does not support sequences", md.getDatabaseName())); } else if ("identity".equals(idGenField.getGenerator())) { _isIdentityGenerator = true; _isSequenceGenerator = false; if (! md.supportsIdentity()) throw new ConfigException(L.l("'{0}' does not support identity", md.getDatabaseName())); } else if ("auto".equals(idGenField.getGenerator())) { if (md.supportsIdentity()) _isIdentityGenerator = true; else if (md.supportsSequences()) _isSequenceGenerator = true; } if (! _isIdentityGenerator && getGenerator(idGenField.getName()) == null) { IdGenerator gen; if (_isSequenceGenerator) { String name = getTable().getName() + "_cseq"; gen = getPersistenceUnit().createSequenceGenerator(name, 1); } else gen = getPersistenceUnit().getTableGenerator("caucho"); _idGenMap.put(idGenField.getName(), gen); } // XXX: really needs to be called from the table-init code for (IdGenerator idGen : _idGenMap.values()) { try { if (idGen instanceof SequenceIdGenerator) { ((SequenceIdGenerator) idGen).init(_amberPersistenceUnit); } else if (idGen instanceof AmberTableGenerator) { // jpa/0g60 ((AmberTableGenerator) idGen).init(_amberPersistenceUnit); } } catch (SQLException e) { throw ConfigException.create(e); } } } private void startImpl() { try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); for (ListenerType listenerType : getListeners()) { String listenerClass = listenerType.getBeanClass().getName(); Class cl = Class.forName(listenerClass, false, loader); Object listener; try { listener = cl.newInstance(); } catch (InstantiationException e) { throw new ConfigException(L.l("'{0}' could not be instantiated.", cl)); } for (Method jMethod : listenerType.getCallbacks(Listener.PRE_PERSIST)) { Method method = getListenerMethod(cl, jMethod.getName()); if (method != null) _prePersistCallbacks.add(new ListenerCallback(listener, method)); } for (Method jMethod : listenerType.getCallbacks(Listener.POST_PERSIST)) { Method method = getListenerMethod(cl, jMethod.getName()); if (method != null) _postPersistCallbacks.add(new ListenerCallback(listener, method)); } for (Method jMethod : listenerType.getCallbacks(Listener.POST_LOAD)) { Method method = getListenerMethod(cl, jMethod.getName()); if (method != null) _postLoadCallbacks.add(new ListenerCallback(listener, method)); } } } catch (RuntimeException e) { throw e; } catch (Exception e) { throw ConfigException.create(e); } } private Method getListenerMethod(Class cl, String methodName) { if (cl == null || cl.equals(Object.class)) return null; Method []methods = cl.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { Class []paramTypes = methods[i].getParameterTypes(); if (methods[i].getName().equals(methodName) && paramTypes.length == 1 && paramTypes[0].isAssignableFrom(getBeanClass())) { return methods[i]; } } return getListenerMethod(cl.getSuperclass(), methodName); } /** * Generates a string to load the field. */ @Override public int generateLoad(JavaWriter out, String rs, String indexVar, int index) throws IOException { out.print("(" + getInstanceClassName() + ") "); index = getId().generateLoadForeign(out, rs, indexVar, index); return index; } /** * Returns true if there's a field with the matching load group. */ public boolean hasLoadGroup(int loadGroupIndex) { if (loadGroupIndex == 0) return true; for (AmberField field : getFields()) { if (field.hasLoadGroup(loadGroupIndex)) return true; } return false; } /** * Generates loading code after the basic fields. */ public int generatePostLoadSelect(JavaWriter out, int index, int loadGroupIndex) throws IOException { if (loadGroupIndex == 0 && getDiscriminator() != null) index++; // jpa/0l40 for (EntityType type = this; type != null; type = type.getParentType()) { index = generatePostLoadSelect(out, index, type.getMappedSuperclassFields()); index = generatePostLoadSelect(out, index, type.getFields()); } return index; } private int generatePostLoadSelect(JavaWriter out, int index, ArrayList<AmberField> fields) throws IOException { if (fields != null) { for (int i = 0; i < fields.size(); i++) { AmberField field = fields.get(i); // jpa/0l40 if (field.getLoadGroupIndex() == loadGroupIndex) index = field.generatePostLoadSelect(out, index); } } return index; } /** * Generates the load code for native fields */ public void generateLoadNative(JavaWriter out) throws IOException { int index = 0; for (AmberField field : getFields()) { index = field.generateLoadNative(out, index); } } /** * Generates the load code for native fields */ public void generateNativeColumnNames(ArrayList<String> names) throws IOException { for (AmberField field : getFields()) { field.generateNativeColumnNames(names); } } /** * Generates a string to set the field. */ @Override public void generateSet(JavaWriter out, String pstmt, String index, String value) throws IOException { if (getId() != null) getId().generateSet(out, pstmt, index, value); } /** * Gets the value. */ @Override public Object getObject(AmberConnection aConn, ResultSet rs, int index) throws SQLException { return getHome().loadLazy(aConn, rs, index); } /** * Finds the object */ @Override public EntityItem findItem(AmberConnection aConn, ResultSet rs, int index) throws SQLException { return getHome().findItem(aConn, rs, index); } /** * Gets the value. */ public Object getLoadObject(AmberConnection aConn, ResultSet rs, int index) throws SQLException { return getHome().loadFull(aConn, rs, index); } /** * Returns true for sequence generator */ public boolean isSequenceGenerator() { return _isSequenceGenerator; } /** * Returns true for sequence generator */ public boolean isIdentityGenerator() { return _isIdentityGenerator; } /** * Sets the named generator. */ public void setGenerator(String name, IdGenerator gen) { _idGenMap.put(name, gen); } /** * Sets the named generator. */ public IdGenerator getGenerator(String name) { return _idGenMap.get(name); } /** * Gets the named generator. */ public long nextGeneratorId(AmberConnection aConn, String name) throws SQLException { IdGenerator idGen = _idGenMap.get(name); return idGen.allocate(aConn); } /** * Loads from an object. */ public void generateLoadFromObject(JavaWriter out, String obj) throws IOException { getId().generateLoadFromObject(out, obj); for (AmberField field : getFields()) { field.generateLoadFromObject(out, obj); } } /** * Copy from an object. */ public void generateCopyLoadObject(JavaWriter out, String dst, String src, int loadGroup) throws IOException { if (getParentType() != null) // jpa/0ge3 getParentType().generateCopyLoadObject(out, dst, src, loadGroup); ArrayList<AmberField> fields = getFields(); for (AmberField field : fields) { // XXX: setter issue, too field.generateCopyLoadObject(out, dst, src, loadGroup); } } /** * Copy from an object. */ public void generateMergeFrom(JavaWriter out, String dst, String src) throws IOException { if (getParentType() != null) getParentType().generateMergeFrom(out, dst, src); ArrayList<AmberField> fields = getFields(); for (int i = 0; i < fields.size(); i++) { AmberField field = fields.get(i); field.generateMergeFrom(out, dst, src); } } /** * Copy from an object. */ public void generateCopyUpdateObject(JavaWriter out, String dst, String src, int updateIndex) throws IOException { if (getParentType() != null) getParentType().generateCopyUpdateObject(out, dst, src, updateIndex);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?