mirror of https://github.com/apache/openjpa.git
OPENJPA-162 : Allow explicit @OrderBy(<primary key field>).
git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@514847 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ebe1b5711e
commit
afce8f543a
|
@ -38,7 +38,7 @@ class JDBCRelatedFieldOrder
|
||||||
|
|
||||||
public JDBCRelatedFieldOrder(FieldMapping owner, FieldMapping rel,
|
public JDBCRelatedFieldOrder(FieldMapping owner, FieldMapping rel,
|
||||||
boolean asc) {
|
boolean asc) {
|
||||||
if (!rel.isInDefaultFetchGroup())
|
if (!rel.isInDefaultFetchGroup() && !rel.isPrimaryKey())
|
||||||
throw new MetaDataException(_loc.get("nondfg-field-orderable",
|
throw new MetaDataException(_loc.get("nondfg-field-orderable",
|
||||||
owner, rel.getName()));
|
owner, rel.getName()));
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed 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.relations;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.Version;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class IdOrderedOneManyChild {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private IdOrderedOneManyParent explicitParent;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private IdOrderedOneManyParent implicitParent;
|
||||||
|
|
||||||
|
@Version
|
||||||
|
private int optLock;
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IdOrderedOneManyParent getExplicitParent() {
|
||||||
|
return explicitParent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExplicitParent(IdOrderedOneManyParent explicitParent) {
|
||||||
|
this.explicitParent = explicitParent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IdOrderedOneManyParent getImplicitParent() {
|
||||||
|
return implicitParent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setImplicitParent(IdOrderedOneManyParent implicitParent) {
|
||||||
|
this.implicitParent = implicitParent;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,69 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed 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.relations;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
import javax.persistence.OrderBy;
|
||||||
|
import javax.persistence.Version;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class IdOrderedOneManyParent {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy="explicitParent")
|
||||||
|
@OrderBy("id ASC")
|
||||||
|
private List<IdOrderedOneManyChild> explicitChildren =
|
||||||
|
new ArrayList<IdOrderedOneManyChild>();
|
||||||
|
|
||||||
|
@OneToMany(mappedBy="implicitParent")
|
||||||
|
@OrderBy
|
||||||
|
private List<IdOrderedOneManyChild> implicitChildren =
|
||||||
|
new ArrayList<IdOrderedOneManyChild>();
|
||||||
|
|
||||||
|
@Version
|
||||||
|
private int optLock;
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<IdOrderedOneManyChild> getExplicitChildren() {
|
||||||
|
return explicitChildren;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<IdOrderedOneManyChild> getImplicitChildren() {
|
||||||
|
return implicitChildren;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,124 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2006 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed 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.relations;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.persistence.Persistence;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.textui.TestRunner;
|
||||||
|
import org.apache.openjpa.persistence.OpenJPAEntityManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test ordering a one-many field on the primary key of the related entity.
|
||||||
|
*
|
||||||
|
* @author Abe White
|
||||||
|
*/
|
||||||
|
public class TestIdOrderedOneMany
|
||||||
|
extends TestCase {
|
||||||
|
|
||||||
|
private EntityManagerFactory emf;
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
public void setUp() {
|
||||||
|
Map props = new HashMap(System.getProperties());
|
||||||
|
props.put("openjpa.MetaDataFactory", "jpa(Types="
|
||||||
|
+ IdOrderedOneManyParent.class.getName() + ";"
|
||||||
|
+ IdOrderedOneManyChild.class.getName() + ")");
|
||||||
|
emf = Persistence.createEntityManagerFactory("test", props);
|
||||||
|
|
||||||
|
EntityManager em = emf.createEntityManager();
|
||||||
|
em.getTransaction().begin();
|
||||||
|
|
||||||
|
IdOrderedOneManyParent parent = new IdOrderedOneManyParent();
|
||||||
|
parent.setName("parent");
|
||||||
|
em.persist(parent);
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
IdOrderedOneManyChild explicit = new IdOrderedOneManyChild();
|
||||||
|
explicit.setId(3 - i);
|
||||||
|
explicit.setName("explicit" + explicit.getId());
|
||||||
|
explicit.setExplicitParent(parent);
|
||||||
|
parent.getExplicitChildren().add(explicit);
|
||||||
|
em.persist(explicit);
|
||||||
|
|
||||||
|
IdOrderedOneManyChild implicit = new IdOrderedOneManyChild();
|
||||||
|
implicit.setId(100 - i);
|
||||||
|
implicit.setName("implicit" + implicit.getId());
|
||||||
|
implicit.setImplicitParent(parent);
|
||||||
|
parent.getImplicitChildren().add(implicit);
|
||||||
|
em.persist(implicit);
|
||||||
|
}
|
||||||
|
|
||||||
|
em.getTransaction().commit();
|
||||||
|
id = parent.getId();
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tearDown() {
|
||||||
|
if (emf == null)
|
||||||
|
return;
|
||||||
|
try {
|
||||||
|
EntityManager em = emf.createEntityManager();
|
||||||
|
em.getTransaction().begin();
|
||||||
|
em.createQuery("delete from IdOrderedOneManyChild").executeUpdate();
|
||||||
|
em.createQuery("delete from IdOrderedOneManyParent").
|
||||||
|
executeUpdate();
|
||||||
|
em.getTransaction().commit();
|
||||||
|
em.close();
|
||||||
|
emf.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testExplicitOrdering() {
|
||||||
|
EntityManager em = emf.createEntityManager();
|
||||||
|
IdOrderedOneManyParent parent = em.find(IdOrderedOneManyParent.class,
|
||||||
|
id);
|
||||||
|
assertNotNull(parent);
|
||||||
|
assertEquals("parent", parent.getName());
|
||||||
|
assertEquals(3, parent.getExplicitChildren().size());
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
assertEquals(i + 1, parent.getExplicitChildren().get(i).getId());
|
||||||
|
assertEquals("explicit" + (i + 1), parent.getExplicitChildren().
|
||||||
|
get(i).getName());
|
||||||
|
}
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testImplicitOrdering() {
|
||||||
|
EntityManager em = emf.createEntityManager();
|
||||||
|
IdOrderedOneManyParent parent = em.find(IdOrderedOneManyParent.class,
|
||||||
|
id);
|
||||||
|
assertNotNull(parent);
|
||||||
|
assertEquals("parent", parent.getName());
|
||||||
|
assertEquals(3, parent.getExplicitChildren().size());
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
assertEquals(i + 98, parent.getImplicitChildren().get(i).getId());
|
||||||
|
assertEquals("implicit" + (i + 98), parent.getImplicitChildren().
|
||||||
|
get(i).getName());
|
||||||
|
}
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
TestRunner.run(TestIdOrderedOneMany.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue