OPENJPA-1069: support of @OrderBy("DESC") on ElementCollection containing basic type

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@776878 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-05-20 21:59:24 +00:00
parent 097bda818c
commit 768cd337b0
5 changed files with 71 additions and 3 deletions

View File

@ -1,3 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.embed.attrOverrides;
import java.util.ArrayList;
@ -20,6 +38,22 @@ public class Person {
@OrderBy("zipcode.zip, zipcode.plusFour")
protected List<Address> residences = new ArrayList<Address>();
@ElementCollection
@OrderBy("DESC")
private List<String> nickNames = new ArrayList<String>();
public List<String> getNickNames() {
return nickNames;
}
public void setNickNames(List<String> nickNames) {
this.nickNames = nickNames;
}
public void addNickName(String nickName) {
nickNames.add(nickName);
}
public String getSsn() {
return ssn;
}

View File

@ -249,6 +249,7 @@ public class TestAttrOverrides extends SQLListenerTestCase {
zipCode.setPlusFour("plusFour_" + id + "_" + i);
addr.setZipcode(zipCode);
p.addResidence(addr);
p.addNickName("nickName_ + " + i);
}
em.persist(p);
return p;
@ -271,5 +272,14 @@ public class TestAttrOverrides extends SQLListenerTestCase {
assertEquals(expPlusFour, plusFour);
i++;
}
List<String> nickNames = p.getNickNames();
assertEquals(4, nickNames.size());
i = 4;
for (String s : nickNames) {
String expNickName = "nickName_ + " + i;
assertEquals(expNickName, s);
i--;
}
}
}

View File

@ -1542,8 +1542,18 @@ public class AnnotationPersistenceMetaDataParser
*/
private void parseOrderBy(FieldMetaData fmd, OrderBy anno) {
String dec = anno.value();
if (dec.length() == 0)
if (fmd.isElementCollection() &&
fmd.getElement().getEmbeddedMetaData() != null) {
if (dec.length() == 0 || dec.equals("ASC") ||
dec.equals("DESC"))
throw new MetaDataException(_loc.get(
"invalid-orderBy", fmd));
}
if (dec.length() == 0 || dec.equals("ASC"))
dec = Order.ELEMENT + " asc";
else if (dec.equals("DESC"))
dec = Order.ELEMENT + " desc";
fmd.setOrderDeclaration(dec);
}

View File

@ -72,6 +72,8 @@ import org.apache.openjpa.meta.ValueMetaData;
import static org.apache.openjpa.persistence.MetaDataTag.*;
import static org.apache.openjpa.persistence.PersistenceStrategy.*;
import org.apache.openjpa.util.ImplHelper;
import org.apache.openjpa.util.MetaDataException;
import serp.util.Numbers;
/**
@ -1549,8 +1551,18 @@ public class XMLPersistenceMetaDataParser
throws SAXException {
FieldMetaData fmd = (FieldMetaData) currentElement();
String dec = currentText();
if (StringUtils.isEmpty(dec))
if (fmd.isElementCollection() &&
fmd.getElement().getEmbeddedMetaData() != null) {
if (dec.length() == 0 || dec.equals("ASC") ||
dec.equals("DESC"))
throw new MetaDataException(_loc.get(
"invalid-orderBy", fmd));
}
if (StringUtils.isEmpty(dec) || dec.equals("ASC"))
dec = Order.ELEMENT + " asc";
else if (dec.equals("DESC"))
dec = Order.ELEMENT + " desc";
fmd.setOrderDeclaration(dec);
}

View File

@ -190,4 +190,6 @@ bad-lock-level: Invalid lock mode/level. Valid values are \
access-invalid: "{0}" is not a valid access style. Valid access styles are \
"PROPERTY" and "FIELD".
getter-unmatched: Getter method "{0}" has no matching setter method.
invalid-orderBy: This is not a valid OrderBy annotation. The property or \
field_name must be specified in the orederBy item of the orderBy list \
for "{0}".