mirror of https://github.com/apache/openjpa.git
OPENJPA-869 Added support for order column table attribute
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@743594 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1d47f14547
commit
7d0452763a
|
@ -1690,6 +1690,14 @@ public class AnnotationPersistenceMappingParser
|
||||||
*/
|
*/
|
||||||
private void parseJavaxOrderColumn(FieldMapping fm,
|
private void parseJavaxOrderColumn(FieldMapping fm,
|
||||||
javax.persistence.OrderColumn order) {
|
javax.persistence.OrderColumn order) {
|
||||||
|
// If a table name is specified on the annotation and a table
|
||||||
|
// name has not been defined, set the table name to the name
|
||||||
|
// specified. This will be the name of the join table or
|
||||||
|
// collection table.
|
||||||
|
if (!StringUtils.isEmpty(order.table()) &&
|
||||||
|
StringUtils.isEmpty(fm.getMappingInfo().getTableName())) {
|
||||||
|
fm.getMappingInfo().setTableName(order.table());
|
||||||
|
}
|
||||||
|
|
||||||
Column col = new Column();
|
Column col = new Column();
|
||||||
if (!StringUtils.isEmpty(order.name()))
|
if (!StringUtils.isEmpty(order.name()))
|
||||||
|
|
|
@ -1091,6 +1091,15 @@ public class XMLPersistenceMappingParser
|
||||||
if (obj instanceof FieldMapping) {
|
if (obj instanceof FieldMapping) {
|
||||||
FieldMapping fm = (FieldMapping)obj;
|
FieldMapping fm = (FieldMapping)obj;
|
||||||
fm.getMappingInfo().setOrderColumn(col);
|
fm.getMappingInfo().setOrderColumn(col);
|
||||||
|
|
||||||
|
// If a table name is specified on the element and a table
|
||||||
|
// name has not been defined, set the table name to the name
|
||||||
|
// specified. This will be the name of the join table or
|
||||||
|
// collection table.
|
||||||
|
if (!StringUtils.isEmpty(col.getTableName()) &&
|
||||||
|
StringUtils.isEmpty(fm.getMappingInfo().getTableName())) {
|
||||||
|
fm.getMappingInfo().setTableName(col.getTableName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
/*
|
||||||
|
* 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.jdbc.order;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Embeddable;
|
||||||
|
|
||||||
|
@Embeddable
|
||||||
|
public class Bicycle {
|
||||||
|
|
||||||
|
@Column(name="bike_brand")
|
||||||
|
private String brand;
|
||||||
|
|
||||||
|
@Column(name="bike_model")
|
||||||
|
private String model;
|
||||||
|
|
||||||
|
public Bicycle() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Bicycle(String brand, String model) {
|
||||||
|
this.brand = brand;
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBrand(String brand) {
|
||||||
|
this.brand = brand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBrand() {
|
||||||
|
return brand;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModel(String model) {
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj instanceof Bicycle) {
|
||||||
|
Bicycle bike = (Bicycle)obj;
|
||||||
|
return getBrand().equals(bike.getBrand()) &&
|
||||||
|
getModel().equals(bike.getModel());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,108 @@
|
||||||
|
/*
|
||||||
|
* 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.jdbc.order;
|
||||||
|
|
||||||
|
import javax.persistence.Basic;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name="OC_CAR")
|
||||||
|
public class Car {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private Owner owner;
|
||||||
|
|
||||||
|
@Basic
|
||||||
|
private int modelYear;
|
||||||
|
|
||||||
|
@Basic
|
||||||
|
private String make;
|
||||||
|
|
||||||
|
@Basic
|
||||||
|
private String model;
|
||||||
|
|
||||||
|
public Car() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Car(int year, String make, String model){
|
||||||
|
this.modelYear = year;
|
||||||
|
this.make = make;
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModelYear(int year) {
|
||||||
|
this.modelYear = year;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getModelYear() {
|
||||||
|
return modelYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMake(String make) {
|
||||||
|
this.make = make;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMake() {
|
||||||
|
return make;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setModel(String model) {
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getModel() {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOwner(Owner owner) {
|
||||||
|
this.owner = owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Owner getOwner() {
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj instanceof Car) {
|
||||||
|
Car car = (Car)obj;
|
||||||
|
return getId() == car.getId() &&
|
||||||
|
getMake().equals(car.getMake()) &&
|
||||||
|
getModel().equals(car.getModel()) &&
|
||||||
|
getModelYear() == car.getModelYear();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
/*
|
||||||
|
* 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.jdbc.order;
|
||||||
|
|
||||||
|
import javax.persistence.Basic;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Home {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
@Basic
|
||||||
|
private int buildYear;
|
||||||
|
|
||||||
|
public Home() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Home(int year) {
|
||||||
|
this.buildYear = year;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBuildYear(int buildYear) {
|
||||||
|
this.buildYear = buildYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getBuildYear() {
|
||||||
|
return buildYear;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj instanceof Home) {
|
||||||
|
Home home = (Home)obj;
|
||||||
|
return getBuildYear() == home.getBuildYear() &&
|
||||||
|
getId() == home.getId();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
* 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.jdbc.order;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Inheritance;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToMany;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
import javax.persistence.OrderColumn;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
import static javax.persistence.InheritanceType.SINGLE_TABLE;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name="OC_OWNER")
|
||||||
|
@Inheritance(strategy=SINGLE_TABLE)
|
||||||
|
public class Owner extends Person {
|
||||||
|
|
||||||
|
// bidirectional one-to-many w/ default join column
|
||||||
|
@OneToMany(mappedBy="owner", cascade=CascadeType.ALL)
|
||||||
|
@OrderColumn(name="car_o2m_order", table="car_o2m_table")
|
||||||
|
private Collection<Car> cars;
|
||||||
|
|
||||||
|
// unidirectional one-to-many w/ join column
|
||||||
|
@OneToMany(cascade=CascadeType.ALL)
|
||||||
|
@OrderColumn(table="home_o2m_table")
|
||||||
|
private Collection<Home> homes;
|
||||||
|
|
||||||
|
@ManyToMany(cascade=CascadeType.ALL)
|
||||||
|
@OrderColumn(table="widget_m2m_table")
|
||||||
|
private Collection<Widget> widgets;
|
||||||
|
|
||||||
|
// element collection
|
||||||
|
@ElementCollection
|
||||||
|
@OrderColumn(name="bike_coll_order", table="bike_table")
|
||||||
|
private Collection<Bicycle> bikeColl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void setCars(Collection<Car> cars) {
|
||||||
|
this.cars = cars;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Car> getCars() {
|
||||||
|
return cars;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHomes(Collection<Home> homes) {
|
||||||
|
this.homes = homes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Home> getHomes() {
|
||||||
|
return homes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBikeColl(Collection<Bicycle> bikeColl) {
|
||||||
|
this.bikeColl = bikeColl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Bicycle> getBikeColl() {
|
||||||
|
return bikeColl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWidgets(Collection<Widget> widgets) {
|
||||||
|
this.widgets = widgets;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Widget> getWidgets() {
|
||||||
|
return widgets;
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,6 +22,9 @@ import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DatabaseMetaData;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -38,6 +41,9 @@ import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
|
||||||
import org.apache.openjpa.jdbc.meta.ClassMapping;
|
import org.apache.openjpa.jdbc.meta.ClassMapping;
|
||||||
import org.apache.openjpa.jdbc.meta.FieldMapping;
|
import org.apache.openjpa.jdbc.meta.FieldMapping;
|
||||||
import org.apache.openjpa.jdbc.schema.Column;
|
import org.apache.openjpa.jdbc.schema.Column;
|
||||||
|
import org.apache.openjpa.jdbc.schema.Sequence;
|
||||||
|
import org.apache.openjpa.jdbc.schema.Table;
|
||||||
|
import org.apache.openjpa.jdbc.sql.DBDictionary;
|
||||||
import org.apache.openjpa.lib.meta.MetaDataSerializer;
|
import org.apache.openjpa.lib.meta.MetaDataSerializer;
|
||||||
import org.apache.openjpa.meta.ClassMetaData;
|
import org.apache.openjpa.meta.ClassMetaData;
|
||||||
import org.apache.openjpa.meta.MetaDataRepository;
|
import org.apache.openjpa.meta.MetaDataRepository;
|
||||||
|
@ -57,7 +63,9 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
super.setUp(DROP_TABLES,
|
super.setUp(DROP_TABLES,
|
||||||
Person.class, Player.class, BattingOrder.class,
|
Person.class, Player.class, BattingOrder.class,
|
||||||
Trainer.class, Game.class, Inning.class,
|
Trainer.class, Game.class, Inning.class,
|
||||||
Course.class, Student.class);
|
Course.class, Student.class,
|
||||||
|
Owner.class, Bicycle.class, Car.class, Home.class,
|
||||||
|
Widget.class);
|
||||||
try {
|
try {
|
||||||
createQueryData();
|
createQueryData();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -639,10 +647,13 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
new Object[] { elems[6], elems[7], elems[8]}, "id",
|
new Object[] { elems[6], elems[7], elems[8]}, "id",
|
||||||
bte.getId());
|
bte.getId());
|
||||||
|
|
||||||
// This test is disabled until INDEX projection supports element collections
|
// This validator is disabled until INDEX projection supports element
|
||||||
|
// collections
|
||||||
// validateIndexAndValues(em, "BaseTestEntity", "collelems", 10,
|
// validateIndexAndValues(em, "BaseTestEntity", "collelems", 10,
|
||||||
// new Object[] { elems[0], elems[1], elems[2]});
|
// new Object[] { elems[0], elems[1], elems[2]} "id",
|
||||||
|
// bte.getId());
|
||||||
|
|
||||||
|
em.close();
|
||||||
try {
|
try {
|
||||||
if (emf1 != null)
|
if (emf1 != null)
|
||||||
cleanupEMF(emf1);
|
cleanupEMF(emf1);
|
||||||
|
@ -652,18 +663,116 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Validates the use of the table attribute on OrderColumn
|
* Validates the use of the table attribute on OrderColumn with
|
||||||
|
* o2o, o2m, m2m, and collection table - with and without join
|
||||||
|
* tables.
|
||||||
*/
|
*/
|
||||||
public void verifyOrderColumnTable() {
|
public void testOrderColumnTable() {
|
||||||
/*
|
|
||||||
* If the relationship is a many-to-many or unidirectional one-to-many
|
OpenJPAEntityManagerSPI em = emf.createEntityManager();
|
||||||
* relationship, the table is the join table for the relationship. If
|
|
||||||
* the relationship is a bidirectional one-to-many or unidirectional
|
validateOrderColumnTable(emf, Owner.class, "cars",
|
||||||
* one-to-many mapped by a join column, the table is the primary
|
"OC_CAR", "car_o2m_order");
|
||||||
* table for the entity on the mapny side of the relationship. If the
|
|
||||||
* ordering is for a collection of elements, the table is the
|
validateOrderColumnTable(emf, Owner.class, "homes",
|
||||||
* collection table for the element collection.
|
"home_o2m_table", "homes_ORDER");
|
||||||
|
|
||||||
|
validateOrderColumnTable(emf, Owner.class, "bikeColl",
|
||||||
|
"bike_table", "bike_coll_order");
|
||||||
|
|
||||||
|
validateOrderColumnTable(emf, Owner.class, "widgets",
|
||||||
|
"widget_m2m_table", "widgets_ORDER");
|
||||||
|
|
||||||
|
Owner owner = new Owner();
|
||||||
|
Collection<Car> cars = new ArrayList<Car>();
|
||||||
|
Collection<Home> homes = new ArrayList<Home>();
|
||||||
|
Collection<Bicycle> bicycles = new ArrayList<Bicycle>();
|
||||||
|
Collection<Widget> widgets = new ArrayList<Widget>();
|
||||||
|
Collection<Owner> owners = new ArrayList<Owner>();
|
||||||
|
owner.setCars(cars);
|
||||||
|
owner.setHomes(homes);
|
||||||
|
owner.setBikeColl(bicycles);
|
||||||
|
owner.setWidgets(widgets);
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++){
|
||||||
|
Car car = new Car(2000 + 1, "Make"+i, "Model"+i);
|
||||||
|
car.setOwner(owner);
|
||||||
|
cars.add(car);
|
||||||
|
|
||||||
|
Home home = new Home(2000 + i);
|
||||||
|
homes.add(home);
|
||||||
|
|
||||||
|
Bicycle bike = new Bicycle("Brand"+i, "Model"+i);
|
||||||
|
bicycles.add(bike);
|
||||||
|
|
||||||
|
Widget widget = new Widget("Name"+i);
|
||||||
|
widgets.add(widget);
|
||||||
|
widget.setOwners(owners);
|
||||||
|
}
|
||||||
|
|
||||||
|
Object[] carArr = cars.toArray();
|
||||||
|
Object[] homeArr = homes.toArray();
|
||||||
|
Object[] bikeArr = bicycles.toArray();
|
||||||
|
Object[] widgetArr = widgets.toArray();
|
||||||
|
|
||||||
|
em.getTransaction().begin();
|
||||||
|
em.persist(owner);
|
||||||
|
em.getTransaction().commit();
|
||||||
|
String oid = owner.getId();
|
||||||
|
|
||||||
|
em.clear();
|
||||||
|
|
||||||
|
// Run queries to ensure the query component uses the correct tables
|
||||||
|
validateIndexAndValues(em, "Owner", "cars", 0,
|
||||||
|
carArr, "id",
|
||||||
|
oid);
|
||||||
|
|
||||||
|
validateIndexAndValues(em, "Owner", "homes", 0,
|
||||||
|
homeArr, "id",
|
||||||
|
oid);
|
||||||
|
|
||||||
|
validateIndexAndValues(em, "Owner", "widgets", 0,
|
||||||
|
widgetArr, "id",
|
||||||
|
oid);
|
||||||
|
|
||||||
|
// This validator is disabled until INDEX projection supports element
|
||||||
|
// collections
|
||||||
|
// validateIndexAndValues(em, "Owner", "bikeColl", 0,
|
||||||
|
// bikeArr, "id",
|
||||||
|
// oid);
|
||||||
|
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates the use of the table attribute defined in XML
|
||||||
*/
|
*/
|
||||||
|
public void testOrderColumnTableXML() {
|
||||||
|
|
||||||
|
OpenJPAEntityManagerFactorySPI emf1 =
|
||||||
|
(OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
|
||||||
|
createEntityManagerFactory("TableTest",
|
||||||
|
"org/apache/openjpa/persistence/jdbc/order/" +
|
||||||
|
"order-persistence-5.xml");
|
||||||
|
|
||||||
|
OpenJPAEntityManagerSPI em = emf1.createEntityManager();
|
||||||
|
|
||||||
|
validateOrderColumnTable(emf1, BaseTestEntity.class, "one2Melems",
|
||||||
|
"xml_o2m_table", "one2MOrder");
|
||||||
|
|
||||||
|
validateOrderColumnTable(emf1, BaseTestEntity.class, "m2melems",
|
||||||
|
"xml_m2m_table", "m2morder");
|
||||||
|
|
||||||
|
validateOrderColumnTable(emf1, BaseTestEntity.class, "collelems",
|
||||||
|
"xml_coll_table", "collelems_ORDER");
|
||||||
|
|
||||||
|
em.close();
|
||||||
|
try {
|
||||||
|
if (emf1 != null)
|
||||||
|
cleanupEMF(emf1);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -699,6 +808,8 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
idx0 = (Student)qry.getSingleResult();
|
idx0 = (Student)qry.getSingleResult();
|
||||||
assertNotNull(idx0);
|
assertNotNull(idx0);
|
||||||
assertEquals(idx0, students[10]);
|
assertEquals(idx0, students[10]);
|
||||||
|
|
||||||
|
em.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -727,6 +838,8 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
Long idx = (Long)qry.getSingleResult();
|
Long idx = (Long)qry.getSingleResult();
|
||||||
assertNotNull(idx);
|
assertNotNull(idx);
|
||||||
assertEquals((Long)idx, (Long)1L);
|
assertEquals((Long)idx, (Long)1L);
|
||||||
|
|
||||||
|
em.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -735,14 +848,15 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
*/
|
*/
|
||||||
public void testOrderColumnOrderBy() {
|
public void testOrderColumnOrderBy() {
|
||||||
|
|
||||||
|
OpenJPAEntityManagerFactorySPI emf1 = null;
|
||||||
|
OpenJPAEntityManagerSPI em = null;
|
||||||
try {
|
try {
|
||||||
OpenJPAEntityManagerFactorySPI emf1 =
|
emf1 = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
|
||||||
(OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.
|
|
||||||
createEntityManagerFactory("ObOcTest",
|
createEntityManagerFactory("ObOcTest",
|
||||||
"org/apache/openjpa/persistence/jdbc/order/" +
|
"org/apache/openjpa/persistence/jdbc/order/" +
|
||||||
"order-persistence-3.xml");
|
"order-persistence-3.xml");
|
||||||
|
|
||||||
OpenJPAEntityManagerSPI em = emf1.createEntityManager();
|
em = emf1.createEntityManager();
|
||||||
|
|
||||||
ObOcEntity ent = new ObOcEntity();
|
ObOcEntity ent = new ObOcEntity();
|
||||||
List<Integer> intList = new ArrayList<Integer>();
|
List<Integer> intList = new ArrayList<Integer>();
|
||||||
|
@ -755,9 +869,19 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
|
|
||||||
em.close();
|
em.close();
|
||||||
|
em = null;
|
||||||
fail("An exception should have been thrown.");
|
fail("An exception should have been thrown.");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
assertException(e, ArgumentException.class);
|
assertException(e, ArgumentException.class);
|
||||||
|
} finally {
|
||||||
|
if (em != null)
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (emf1 != null)
|
||||||
|
cleanupEMF(emf1);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -810,6 +934,13 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
assertNotNull(oc);
|
assertNotNull(oc);
|
||||||
assertEquals(oc.getName(),"collelems_ORDER");
|
assertEquals(oc.getName(),"collelems_ORDER");
|
||||||
assertEquals(oc.getBase(), 10);
|
assertEquals(oc.getBase(), 10);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (emf1 != null)
|
||||||
|
cleanupEMF(emf1);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -852,6 +983,7 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
em.persist(courseA);
|
em.persist(courseA);
|
||||||
em.persist(courseB);
|
em.persist(courseB);
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
|
em.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateIndexAndValues(OpenJPAEntityManagerSPI em,
|
private void validateIndexAndValues(OpenJPAEntityManagerSPI em,
|
||||||
|
@ -899,6 +1031,18 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
assertTrue(oc.getName().equalsIgnoreCase(columnName));
|
assertTrue(oc.getName().equalsIgnoreCase(columnName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateOrderColumnTable(
|
||||||
|
OpenJPAEntityManagerFactorySPI emf1,
|
||||||
|
Class clazz, String fieldName, String tableName,
|
||||||
|
String columnName) {
|
||||||
|
Column oc = getOrderColumn(emf1, clazz, fieldName);
|
||||||
|
// Verify the oc has the correct table name
|
||||||
|
assertTrue(oc.getTableName().equalsIgnoreCase(tableName));
|
||||||
|
// Verify the table exists in the db
|
||||||
|
assertTrue(tableAndColumnExists(emf1, null, tableName, null,
|
||||||
|
columnName));
|
||||||
|
}
|
||||||
|
|
||||||
private void validateOrderColumnContiguous(
|
private void validateOrderColumnContiguous(
|
||||||
OpenJPAEntityManagerFactorySPI emf1, Class clazz, String fieldName,
|
OpenJPAEntityManagerFactorySPI emf1, Class clazz, String fieldName,
|
||||||
boolean contiguous) {
|
boolean contiguous) {
|
||||||
|
@ -934,6 +1078,44 @@ public class TestOrderColumn extends SingleEMFTestCase {
|
||||||
assertEquals(insertable, !oc.getFlag(Column.FLAG_DIRECT_INSERT));
|
assertEquals(insertable, !oc.getFlag(Column.FLAG_DIRECT_INSERT));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to verify a table was created for the given name and schema
|
||||||
|
*/
|
||||||
|
private boolean tableAndColumnExists(OpenJPAEntityManagerFactorySPI emf1,
|
||||||
|
OpenJPAEntityManagerSPI em, String tableName, String schemaName,
|
||||||
|
String columnName) {
|
||||||
|
JDBCConfiguration conf = (JDBCConfiguration) emf1.getConfiguration();
|
||||||
|
DBDictionary dict = conf.getDBDictionaryInstance();
|
||||||
|
OpenJPAEntityManagerSPI em1 = em;
|
||||||
|
|
||||||
|
// If no em supplied, create one
|
||||||
|
if (em1 == null) {
|
||||||
|
em1 = emf1.createEntityManager();
|
||||||
|
}
|
||||||
|
Connection conn = (Connection)em1.getConnection();
|
||||||
|
try {
|
||||||
|
DatabaseMetaData dbmd = conn.getMetaData();
|
||||||
|
// (meta, catalog, schemaName, tableName, conn)
|
||||||
|
Column[] cols = dict.getColumns(dbmd, null, null,
|
||||||
|
tableName, columnName, conn);
|
||||||
|
if (cols != null && cols.length == 1) {
|
||||||
|
Column col = cols[0];
|
||||||
|
String colName = col.getName();
|
||||||
|
if (col.getTableName().equalsIgnoreCase(tableName) &&
|
||||||
|
(schemaName == null ||
|
||||||
|
col.getSchemaName().equalsIgnoreCase(schemaName)) &&
|
||||||
|
colName.equalsIgnoreCase(columnName))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
fail("Unable to get column information.");
|
||||||
|
} finally {
|
||||||
|
if (em == null) {
|
||||||
|
em1.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Closes a specific entity manager factory and cleans up
|
* Closes a specific entity manager factory and cleans up
|
||||||
* associated tables.
|
* associated tables.
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
/*
|
||||||
|
* 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.jdbc.order;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import javax.persistence.Basic;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.ManyToMany;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Widget {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(generator="uuid-hex")
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
@Basic
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@ManyToMany(mappedBy="widgets")
|
||||||
|
private Collection<Owner> owners;
|
||||||
|
|
||||||
|
public Widget() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Widget(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOwners(Collection<Owner> owners) {
|
||||||
|
this.owners = owners;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Owner> getOwners() {
|
||||||
|
return owners;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj instanceof Widget) {
|
||||||
|
Widget widget = (Widget)obj;
|
||||||
|
return getId().equals(widget.getId()) &&
|
||||||
|
getName().equals(widget.getName());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
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.
|
||||||
|
-->
|
||||||
|
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
|
||||||
|
version="2.0">
|
||||||
|
<package>
|
||||||
|
org.apache.openjpa.persistence.jdbc.order
|
||||||
|
</package>
|
||||||
|
<entity name="BaseTestTableElement" class="BaseTestElement"
|
||||||
|
access="PROPERTY">
|
||||||
|
<table name="BTTEL"/>
|
||||||
|
<attributes>
|
||||||
|
<id name="id">
|
||||||
|
<generated-value/>
|
||||||
|
</id>
|
||||||
|
<basic name="name"/>
|
||||||
|
</attributes>
|
||||||
|
</entity>
|
||||||
|
<entity name="BaseTestTableEntity" class="BaseTestEntity" access="FIELD">
|
||||||
|
<table name="BTNNEN"/>
|
||||||
|
<attributes>
|
||||||
|
<id name="id">
|
||||||
|
<generated-value/>
|
||||||
|
</id>
|
||||||
|
<one-to-many name="one2Melems">
|
||||||
|
<order-column name="one2MOrder" table="xml_o2m_table" />
|
||||||
|
<cascade>
|
||||||
|
<cascade-all/>
|
||||||
|
</cascade>
|
||||||
|
</one-to-many>
|
||||||
|
|
||||||
|
<many-to-many name="m2melems">
|
||||||
|
<order-column name="m2morder" table="xml_m2m_table" />
|
||||||
|
<cascade>
|
||||||
|
<cascade-all/>
|
||||||
|
</cascade>
|
||||||
|
</many-to-many>
|
||||||
|
|
||||||
|
<element-collection name="collelems">
|
||||||
|
<order-column table="xml_coll_table" />
|
||||||
|
</element-collection>
|
||||||
|
|
||||||
|
</attributes>
|
||||||
|
</entity>
|
||||||
|
</entity-mappings>
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
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.
|
||||||
|
-->
|
||||||
|
<persistence
|
||||||
|
xmlns="http://java.sun.com/xml/ns/persistence"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
|
||||||
|
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
|
||||||
|
version="2.0" >
|
||||||
|
<persistence-unit name="TableTest" transaction-type="RESOURCE_LOCAL">
|
||||||
|
<description>PU for order column testing</description>
|
||||||
|
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
|
||||||
|
<mapping-file>org/apache/openjpa/persistence/jdbc/order/order-orm-3.xml</mapping-file>
|
||||||
|
<class>org.apache.openjpa.persistence.jdbc.order.BaseTestEntity</class>
|
||||||
|
<class>org.apache.openjpa.persistence.jdbc.order.BaseTestElement</class>
|
||||||
|
<properties>
|
||||||
|
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
|
||||||
|
</properties>
|
||||||
|
</persistence-unit>
|
||||||
|
</persistence>
|
Loading…
Reference in New Issue