manytoonefield.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,403 行 · 第 1/3 页
JAVA
1,403 行
/* * 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 Scott Ferguson */package com.caucho.amber.field;import com.caucho.amber.AmberRuntimeException;import com.caucho.amber.cfg.*;import com.caucho.amber.expr.AmberExpr;import com.caucho.amber.expr.ManyToOneExpr;import com.caucho.amber.expr.PathExpr;import com.caucho.amber.query.QueryParser;import com.caucho.amber.table.AmberColumn;import com.caucho.amber.table.ForeignColumn;import com.caucho.amber.table.LinkColumns;import com.caucho.amber.table.AmberTable;import com.caucho.amber.type.*;import com.caucho.config.ConfigException;import com.caucho.java.JavaWriter;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import javax.persistence.CascadeType;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.logging.Logger;import javax.persistence.JoinColumn;/** * Represents a many-to-one link pointing to an entity. */public class ManyToOneField extends CascadableField { private static final L10N L = new L10N(ManyToOneField.class); private static final Logger log = Logger.getLogger(ManyToOneField.class.getName()); private LinkColumns _linkColumns; private EntityType _targetType; private int _targetLoadIndex; private DependentEntityOneToOneField _targetField; private AmberField _aliasField; private boolean _isInsert = true; private boolean _isUpdate = true; private boolean _isSourceCascadeDelete; private boolean _isTargetCascadeDelete; private boolean _isManyToOne; private JoinColumn _joinColumnsAnn[]; private HashMap<String, JoinColumnConfig> _joinColumnMap = null; public ManyToOneField(EntityType relatedType, String name, CascadeType[] cascadeType, boolean isManyToOne) throws ConfigException { super(relatedType, name, cascadeType); _isManyToOne = isManyToOne; } public ManyToOneField(EntityType relatedType, String name, CascadeType[] cascadeType) throws ConfigException { super(relatedType, name, cascadeType); } public ManyToOneField(EntityType relatedType, String name) throws ConfigException { this(relatedType, name, null); } public ManyToOneField(EntityType relatedType) { super(relatedType); } /** * Sets the target type. */ public void setType(AmberType targetType) { if (! (targetType instanceof EntityType)) throw new AmberRuntimeException(L.l("many-to-one requires an entity target at '{0}'", targetType)); _targetType = (EntityType) targetType; } /** * Returns the source type as * entity or mapped-superclass. */ public EntityType getRelatedType() { return (EntityType) getSourceType(); } /** * Returns the target type as * entity or mapped-superclass. */ public EntityType getEntityTargetType() { return _targetType; } /** * Returns the foreign type. */ public String getForeignTypeName() { //return ((KeyColumn) getColumn()).getType().getForeignTypeName(); return getEntityTargetType().getForeignTypeName(); } /** * Returns true if it is annotated as many-to-one. */ public boolean isAnnotatedManyToOne() { return _isManyToOne; } /** * Set true if deletes cascade to the target. */ public void setTargetCascadeDelete(boolean isCascadeDelete) { _isTargetCascadeDelete = isCascadeDelete; } /** * Set true if deletes cascade to the source. */ public void setSourceCascadeDelete(boolean isCascadeDelete) { _isSourceCascadeDelete = isCascadeDelete; } /** * Set true if deletes cascade to the target. */ public boolean isTargetCascadeDelete() { return _isTargetCascadeDelete; } /** * Set true if deletes cascade to the source. */ public boolean isSourceCascadeDelete() { return _isSourceCascadeDelete; } /** * Sets the join column annotations. */ public void setJoinColumns(JoinColumn joinColumnsAnn[]) { _joinColumnsAnn = joinColumnsAnn; } /** * Gets the join column annotations. */ public Object[] getJoinColumns() { return _joinColumnsAnn; } /** * Sets the join column map. */ public void setJoinColumnMap(HashMap<String, JoinColumnConfig> joinColumnMap) { _joinColumnMap = joinColumnMap; } /** * Gets the join column map. */ public HashMap<String, JoinColumnConfig> getJoinColumnMap() { return _joinColumnMap; } /** * Sets the join columns. */ public void setLinkColumns(LinkColumns linkColumns) { _linkColumns = linkColumns; } /** * Gets the columns. */ public LinkColumns getLinkColumns() { return _linkColumns; } /** * Sets the target field. */ public void setTargetField(DependentEntityOneToOneField field) { _targetField = field; } /** * Sets any alias field. */ public void setAliasField(AmberField alias) { _aliasField = alias; } /** * Creates a copy of the field for a parent */ @Override public AmberField override(BeanType type) { ManyToOneField field = new ManyToOneField((EntityType) getSourceType(), getName(), getCascadeType(), _isManyToOne); field.setOverride(true); field.setLazy(isLazy()); /* field.setInsert(_isInsert); field.setUpdate(_isUpdate); */ return field; } /** * Initializes the field. */ @Override public void init() throws ConfigException { init(getRelatedType()); } /** * Initializes the field. */ public void init(EntityType relatedType) throws ConfigException { boolean isJPA = relatedType.getPersistenceUnit().isJPA(); int loadGroupIndex = getEntitySourceType().getDefaultLoadGroupIndex(); super.setLoadGroupIndex(loadGroupIndex); // jpa/0l40 vs. ejb/0602 if (isJPA) _targetLoadIndex = loadGroupIndex; else _targetLoadIndex = relatedType.nextLoadGroupIndex(); AmberTable sourceTable = relatedType.getTable(); if (sourceTable == null || ! isJPA) { // jpa/0ge3, ejb/0602 super.init(); return; } // jpa/0j67 setSourceCascadeDelete(isCascade(CascadeType.REMOVE)); int n = 0; if (_joinColumnMap != null) n = _joinColumnMap.size(); ArrayList<ForeignColumn> foreignColumns = new ArrayList<ForeignColumn>(); EntityType parentType = _targetType; ArrayList<AmberColumn> targetIdColumns = _targetType.getId().getColumns(); while (targetIdColumns.size() == 0) { parentType = parentType.getParentType(); if (parentType == null) break; targetIdColumns = parentType.getId().getColumns(); } for (AmberColumn keyColumn : targetIdColumns) { String columnName; columnName = getName() + '_' + keyColumn.getName(); boolean nullable = true; boolean unique = false; if (n > 0) { JoinColumnConfig joinColumn; if (n == 1) { joinColumn = (JoinColumnConfig) _joinColumnMap.values().toArray()[0]; } else joinColumn = _joinColumnMap.get(keyColumn.getName()); if (joinColumn != null) { columnName = joinColumn.getName(); nullable = joinColumn.isNullable(); unique = joinColumn.isUnique(); } } else { JoinColumn joinAnn = BaseConfigIntrospector.getJoinColumn(_joinColumnsAnn, keyColumn.getName()); if (joinAnn != null) { columnName = joinAnn.name(); nullable = joinAnn.nullable(); unique = joinAnn.unique(); } } ForeignColumn foreignColumn; foreignColumn = sourceTable.createForeignColumn(columnName, keyColumn); foreignColumn.setNotNull(! nullable); foreignColumn.setUnique(unique); foreignColumns.add(foreignColumn); } LinkColumns linkColumns = new LinkColumns(sourceTable, _targetType.getTable(), foreignColumns); setLinkColumns(linkColumns); super.init(); Id id = getEntityTargetType().getId(); ArrayList<AmberColumn> keys = id.getColumns(); if (_linkColumns == null) { ArrayList<ForeignColumn> columns = new ArrayList<ForeignColumn>(); for (int i = 0; i < keys.size(); i++) { AmberColumn key = keys.get(i); String name; if (keys.size() == 1) name = getName(); else name = getName() + "_" + key.getName(); columns.add(sourceTable.createForeignColumn(name, key)); } _linkColumns = new LinkColumns(relatedType.getTable(), _targetType.getTable(), columns); } if (relatedType.getId() != null) { // resolve any alias for (AmberField field : relatedType.getId().getKeys()) { for (ForeignColumn column : _linkColumns.getColumns()) { if (field.getColumn() != null && field.getColumn().getName().equals(column.getName())) { _aliasField = field; } } } } _targetLoadIndex = relatedType.getLoadGroupIndex(); // nextLoadGroupIndex(); _linkColumns.setTargetCascadeDelete(isTargetCascadeDelete()); _linkColumns.setSourceCascadeDelete(isSourceCascadeDelete()); } /** * Generates the post constructor initialization. */ @Override public void generatePostConstructor(JavaWriter out) throws IOException { if (_aliasField == null) { out.println(getSetterName() + "(" + generateSuperGetter("this") + ");"); } } /** * Creates the expression for the field. */ @Override public AmberExpr createExpr(QueryParser parser, PathExpr parent) { return new ManyToOneExpr(parent, _linkColumns); } /** * Gets the column corresponding to the target field. */ public ForeignColumn getColumn(AmberColumn targetColumn) { return _linkColumns.getSourceColumn(targetColumn); } /** * Generates the insert. */ @Override public void generateInsertColumns(ArrayList<String> columns) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?