1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/**
* <copyright>
*
* Copyright (c) 2005, 2006 Springsite BV (The Netherlands) and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Martin Taal
* Davide Marchignoli
* </copyright>
*
* $Id: ManyAttributeMapper.java,v 1.3 2006/11/13 19:55:09 mtaal Exp $
*/
package org.eclipse.emf.teneo.hibernate.mapper;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.util.FeatureMapUtil;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEAttribute;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEReference;
import org.eclipse.emf.teneo.annotations.pannotation.JoinTable;
import org.eclipse.emf.teneo.annotations.pannotation.OneToMany;
import org.eclipse.emf.teneo.hibernate.hbmodel.HbAnnotatedEAttribute;
import org.eclipse.emf.teneo.simpledom.Element;
/**
* Maps many valued attributes.
*
* <p>
* Assumes that the given {@link PAnnotatedEAttribute} is normal, i.e.
* <ul>
* <li>it is a {@link PAnnotatedEReference};
* <li>it has a {@link OneToMany} annotation;
* <li>oneToMany.getCascade() is ALL
* <li>oneToMany.getTargetEntity is not specified
* <li>the {@link OneToMany} annotation have the following attributes set:
* <li>TODO which is the meaning of a column annotation? Can it have one?
* </ul>
*
* @author <a href="mailto:marchign at elver.org">Davide Marchignoli</a>
* @author <a href="mailto:mtaal at elver.org">Martin Taal</a>
*/
class ManyAttributeMapper extends AbstractAssociationMapper {
/** The logger */
private static final Log log = LogFactory.getLog(ManyAttributeMapper.class);
/**
* @param hbmContext
* The context in which the mapping takes place
*/
public ManyAttributeMapper(MappingContext hbmContext) {
super(hbmContext);
}
/**
* Process a many=true EAttribute
*/
public void processManyAttribute(PAnnotatedEAttribute paAttribute) {
if (log.isDebugEnabled())
log.debug("Generating many valued attribute mapping for " + paAttribute);
final HbAnnotatedEAttribute hbAttribute = (HbAnnotatedEAttribute) paAttribute;
final EAttribute eattr = paAttribute.getAnnotatedEAttribute();
final boolean isArray = eattr.getEType().getInstanceClass() != null
&& eattr.getEType().getInstanceClass().isArray();
final Element collElement = addCollectionElement(paAttribute);
final Element keyElement = collElement.addElement("key");
final JoinTable jt = paAttribute.getJoinTable();
final List jcs = paAttribute.getJoinColumns() == null ? new ArrayList() : (List) paAttribute.getJoinColumns();
final OneToMany otm = paAttribute.getOneToMany();
if (jt != null) {
addJoinTable(collElement, keyElement, jt);
} else {
// TODO should we also add joinColumns annotation?
addKeyColumns(keyElement, jcs);
}
if (!otm.isIndexed() && isArray) {
log.warn("One to many is not indexed but this is an array, force=ing index column!");
}
if ((otm.isIndexed() || isArray) && hbAttribute.getHbIdBag() == null) {
addListIndex(collElement, paAttribute);
}
if (!isArray) {
addFetchType(collElement, otm.getFetch());
}
addCascadesForMany(collElement, otm.getCascade());
if (FeatureMapUtil.isFeatureMap(paAttribute.getAnnotatedEAttribute())) {
FeatureMapMapping fmm = new FeatureMapMapping(getHbmContext(), paAttribute);
getHbmContext().addFeatureMapMapper(fmm);
collElement.addElement("one-to-many").addAttribute("entity-name", fmm.getEntityName());
} else {
addElementElement(collElement, eattr.getName(), paAttribute, getColumns(paAttribute), otm.getTargetEntity());
}
}
/**
* Add Element element in given collection element.
*/
private Element addElementElement(Element collElement, String defaultName, PAnnotatedEAttribute paAttribute,
List columns, String targetEntity) {
final Element elElement;
if (targetEntity == null) {
elElement = collElement.addElement("element").addAttribute("type", hbType(paAttribute));
} else {
elElement = collElement.addElement("element").addAttribute("type", targetEntity);
}
if (columns != null && columns.size() > 0) {
addColumns(elElement, defaultName, columns, getHbmContext().isCurrentElementFeatureMap(), true);
}
return elElement;
}
}
|