OPENJPA-1115 Committing code and test updates contributed by Dianne Richards

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@803612 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jeremy Bauer 2009-08-12 17:22:52 +00:00
parent d5f14ebc02
commit 3dda949ce4
7 changed files with 297 additions and 8 deletions

View File

@ -501,7 +501,9 @@ public class ResultSetResult
protected int findObject(Object obj, Joins joins)
throws SQLException {
try {
return getResultSet().findColumn(obj.toString());
String s1 = obj.toString();
s1 = _dict.stripDelimiters(s1);
return getResultSet().findColumn(s1);
} catch (SQLException se) {
return 0;
}

View File

@ -27,8 +27,6 @@ import javax.persistence.Table;
@Entity
@Inheritance
// Note: delimited columnDefinition is not supported on DB2 or Derby
// TODO: Find out what DB supports this and write a specific test case for it
@DiscriminatorColumn(name="\"discr col\"", columnDefinition="VARCHAR(10)")
@Table(name="\"Animal\"")
public class Animal {
@ -39,6 +37,8 @@ public class Animal {
protected String type;
@Column(name="\"animal name\"")
protected String name;
@Column(name="\"animal age\"")
protected int age;
public Animal() {}
@ -87,4 +87,18 @@ public class Animal {
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,98 @@
/*
* 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.delimited.identifiers;
import javax.persistence.ColumnResult;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
@SqlResultSetMapping(name="AnimalResultSet",
entities={@EntityResult(entityClass=Animal2.class,
fields={
@FieldResult(name="type", column="\"animal type\""),
@FieldResult(name="name", column="\"animal name\"")
},
discriminatorColumn="\"discr col\"")},
columns={@ColumnResult(name="\"animal age\"")})
@Entity
@Inheritance
@DiscriminatorColumn(name="\"discr col\"", columnDefinition="VARCHAR(10)")
@Table(name="\"Animal2\"")
public class Animal2 {
@Id
private int id;
protected String type;
protected String name;
public Animal2() {}
public Animal2(int id) {
this.id = id;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.delimited.identifiers;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("Cat")
public class Cat2 extends Animal2 {
public Cat2() {}
public Cat2(int id) {
super(id);
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.delimited.identifiers;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("Dog")
public class Dog2 extends Animal2 {
public Dog2() {}
public Dog2(int id) {
super(id);
}
}

View File

@ -0,0 +1,111 @@
/*
* 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.delimited.identifiers;
import java.util.List;
import javax.persistence.Query;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
public class TestManualDelimIdResultSetAnnotations
extends SQLListenerTestCase {
OpenJPAEntityManager em;
int id = 0;
Animal animal;
Dog dog;
Cat cat;
Animal2 animal2;
Dog2 dog2;
Cat2 cat2;
@Override
public void setUp() throws Exception {
super.setUp(
org.apache.openjpa.persistence.delimited.identifiers.Animal.class,
org.apache.openjpa.persistence.delimited.identifiers.Dog.class,
org.apache.openjpa.persistence.delimited.identifiers.Cat.class,
org.apache.openjpa.persistence.delimited.identifiers.Animal2.class,
org.apache.openjpa.persistence.delimited.identifiers.Dog2.class,
org.apache.openjpa.persistence.delimited.identifiers.Cat2.class
);
assertNotNull(emf);
em = emf.createEntityManager();
assertNotNull(em);
}
private void createCat(int id) {
cat = new Cat(id);
cat.setName("Puff");
cat.setType("cat");
cat.setAge(3);
}
private void createDog(int id) {
dog = new Dog(id);
dog.setName("Spot");
dog.setType("dog");
dog.setAge(9);
}
public void testCreate() {
id++;
createDog(id);
id++;
createCat(id);
em.getTransaction().begin();
em.persist(dog);
em.persist(cat);
em.getTransaction().commit();
runQueries();
}
private void runQueries() {
em.clear();
resultSetQuery();
}
private void resultSetQuery() {
String query =
"SELECT a.id, a.\"animal type\", a.\"animal name\", " +
"a.\"discr col\", a.\"animal age\" " +
"FROM \"Animal\" a ";
Query q = em.createNativeQuery(query,"AnimalResultSet");
List<Object[]> results = (List<Object[]>)q.getResultList();
assertEquals(2,results.size());
for (Object[] result : results) {
assertEquals(2, result.length);
assertTrue(result[0] instanceof Animal2);
assertTrue(result[1] instanceof Integer);
Animal2 animal2 = (Animal2)result[0];
Integer age = (Integer)result[1];
if (animal2.getName().equals("Spot")) {
assertEquals(9, age.intValue());
}
else if (animal2.getName().equals("Puff")) {
assertEquals(3, age.intValue());
}
}
}
}

View File

@ -29,7 +29,7 @@ import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "ITEM")
@Table(name = "I_ITEM")
public class Item implements Serializable {
private static final long serialVersionUID = 489786296539819572L;
@ -39,7 +39,7 @@ public class Item implements Serializable {
public java.math.BigDecimal itemPrice;
public String itemData;
@Column(name = "I_DATA", table = "ITEM")
@Column(name = "I_DATA", table = "I_ITEM")
public String getItemData() {
return itemData;
}
@ -49,7 +49,7 @@ public class Item implements Serializable {
}
@Id
@Column(name = "I_ID", table = "ITEM")
@Column(name = "I_ID", table = "I_ITEM")
@GeneratedValue(strategy = GenerationType.AUTO)
public int getItemId() {
return itemId;
@ -59,7 +59,7 @@ public class Item implements Serializable {
this.itemId = itemId;
}
@Column(name = "I_NAME", table = "ITEM")
@Column(name = "I_NAME", table = "I_ITEM")
public String getItemName() {
return itemName;
}
@ -69,7 +69,7 @@ public class Item implements Serializable {
}
@Basic
@Column(name = "I_PRICE", table = "ITEM")
@Column(name = "I_PRICE", table = "I_ITEM")
public java.math.BigDecimal getItemPrice() {
return itemPrice;
}