OPENJPA-782: add test case for embeddable support

using orm xml.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@727791 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2008-12-18 19:08:43 +00:00
parent 90f6d5ff80
commit 78dda5c2b7
7 changed files with 595 additions and 0 deletions

View File

@ -0,0 +1,54 @@
/*
* 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;
import javax.persistence.Embeddable;
public class EmbedXml {
// owned by BasicA via Embed4
// can not contain a collection of basic types if BasicA contains
// a collection of Embed4
protected int intVal1;
protected int intVal2;
protected int intVal3;
public int getIntVal1() {
return intVal1;
}
public void setIntVal1(int intVal1) {
this.intVal1 = intVal1;
}
public int getIntVal2() {
return intVal2;
}
public void setIntVal2(int intVal2) {
this.intVal2 = intVal2;
}
public int getIntVal3() {
return intVal3;
}
public void setIntVal3(int intVal3) {
this.intVal3 = intVal3;
}
}

View File

@ -0,0 +1,60 @@
/*
* 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;
public class Embed_EmbedXml {
protected int intVal1;
protected int intVal2;
protected int intVal3;
protected EmbedXml embed;
public int getIntVal1() {
return intVal1;
}
public void setIntVal1(int intVal1) {
this.intVal1 = intVal1;
}
public int getIntVal2() {
return intVal2;
}
public void setIntVal2(int intVal2) {
this.intVal2 = intVal2;
}
public int getIntVal3() {
return intVal3;
}
public void setIntVal3(int intVal3) {
this.intVal3 = intVal3;
}
public EmbedXml getEmbed() {
return embed;
}
public void setEmbed(EmbedXml embed) {
this.embed = embed;
}
}

View File

@ -0,0 +1,74 @@
/*
* 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;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* CREATE TABLE EntityA_Coll_Embed_Embed (id INTEGER NOT NULL, age INTEGER, name VARCHAR(30), PRIMARY KEY (id))
* CREATE TABLE EntityA_Coll_Embed_Embed_embeds (ENTITYA_COLL_EMBED_EMBED_ID INTEGER, intVal1 INTEGER, intVal2 INTEGER, intVal3 INTEGER, IntVal1x INTEGER, IntVal2x INTEGER, IntVal3x INTEGER)
* @author faywang
*
*/
public class EntityA_Coll_Embed_EmbedXml implements Serializable {
Integer id;
String name;
int age;
protected List<Embed_EmbedXml> embeds = new ArrayList<Embed_EmbedXml>();
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Embed_EmbedXml> getEmbeds() {
return embeds;
}
public void addEmbed(Embed_EmbedXml embed) {
embeds.add(embed);
}
}

View File

@ -0,0 +1,80 @@
/*
* 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;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* CREATE TABLE EntityA_Coll_String (id INTEGER NOT NULL, age INTEGER, name VARCHAR(30), PRIMARY KEY (id))
* CREATE TABLE NickNames_Tbl (ENTITYA_COLL_STRING_ID INTEGER, nicknames1 VARCHAR(20))
* CREATE INDEX I_NCKNTBL_ENTITYA_ ON NickNames_Tbl (ENTITYA_COLL_STRING_ID)
* INSERT INTO EntityA_Coll_String (id, age, name) VALUES (?, ?, ?)
* INSERT INTO NickNames_Tbl (ENTITYA_COLL_STRING_ID, nicknames1) VALUES (?, ?)
* SELECT t0.age, t0.name FROM EntityA_Coll_String t0 WHERE t0.id = ? optimize for 1 row
* SELECT t0.nicknames1 FROM NickNames_Tbl t0 WHERE t0.ENTITYA_COLL_STRING_ID = ?
* @author faywang
*/
public class EntityA_Coll_StringXml implements Serializable {
// contains a collection of basic types
Integer id;
String name;
int age;
protected Set<String> nickNames = new HashSet<String>();
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<String> getNickNames() {
return nickNames;
}
public void addNickName(String nickName) {
nickNames.add(nickName);
}
}

View File

@ -0,0 +1,225 @@
/*
* 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;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestEmbeddableXml extends SingleEMFTestCase {
public int numEmbeddables = 1;
public int numBasicTypes = 1;
public int ID = 1;
public void setUp() {
setUp(CLEAR_TABLES);
}
@Override
protected String getPersistenceUnitName() {
return "embed-pu";
}
public void testEntityA_Coll_StringXml() {
createEntityA_Coll_StringXml();
queryEntityA_Coll_StringXml();
findEntityA_Coll_StringXml();
}
public void testEntityA_Coll_Embed_Embed() {
createEntityA_Coll_Embed_EmbedXml();
queryEntityA_Coll_Embed_EmbedXml();
findEntityA_Coll_Embed_EmbedXml();
}
/*
* Create EntityA_Coll_StringXml
*/
public void createEntityA_Coll_StringXml() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
createEntityA_Coll_StringXml(em, ID);
tran.begin();
em.flush();
tran.commit();
em.close();
}
public void createEntityA_Coll_StringXml(EntityManager em, int id) {
EntityA_Coll_StringXml a = new EntityA_Coll_StringXml();
a.setId(id);
a.setName("a" + id);
a.setAge(id);
for (int i = 0; i < numBasicTypes; i++)
a.addNickName("nickName_" + id + i);
em.persist(a);
}
/*
* Create EntityA_Coll_Embed_EmbedXml
*/
public void createEntityA_Coll_Embed_EmbedXml() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
createEntityA_Coll_Embed_EmbedXml(em, ID);
tran.begin();
em.flush();
tran.commit();
em.close();
}
public void createEntityA_Coll_Embed_EmbedXml(EntityManager em, int id) {
EntityA_Coll_Embed_EmbedXml a = new EntityA_Coll_Embed_EmbedXml();
a.setId(id);
a.setName("a" + id);
a.setAge(id);
for (int i = 0; i < numEmbeddables; i++) {
Embed_EmbedXml embed = createEmbed_EmbedXml(em, id, i);
a.addEmbed(embed);
}
em.persist(a);
}
public Embed_EmbedXml createEmbed_EmbedXml(EntityManager em, int id, int idx) {
Embed_EmbedXml embed = new Embed_EmbedXml();
embed.setIntVal1(id * 100 + idx * 10 + 1);
embed.setIntVal2(id * 100 + idx * 10 + 2);
embed.setIntVal3(id * 100 + idx * 10 + 3);
EmbedXml embed1 = createEmbedXml(id, idx);
embed.setEmbed(embed1);
return embed;
}
public EmbedXml createEmbedXml(int id, int idx) {
EmbedXml embed = new EmbedXml();
embed.setIntVal1(id * 100 + idx * 10 + 4);
embed.setIntVal2(id * 100 + idx * 10 + 5);
embed.setIntVal3(id * 100 + idx * 10 + 6);
return embed;
}
/*
* Find EntityA_Coll_StringXml
*/
public void findEntityA_Coll_StringXml() {
EntityManager em = emf.createEntityManager();
EntityA_Coll_StringXml a = em.find(EntityA_Coll_StringXml.class, ID);
checkEntityA_Coll_StringXml(a);
em.close();
}
/*
* Find EntityA_Coll_Embed_EmbedXml
*/
public void findEntityA_Coll_Embed_EmbedXml() {
EntityManager em = emf.createEntityManager();
EntityA_Coll_Embed_EmbedXml a = em.find(EntityA_Coll_Embed_EmbedXml.class, ID);
checkEntityA_Coll_Embed_EmbedXml(a);
em.close();
}
/*
* check EntityA_Coll_String
*/
public void checkEntityA_Coll_StringXml(EntityA_Coll_StringXml a) {
int id = a.getId();
String name = a.getName();
int age = a.getAge();
assertEquals(1, id);
assertEquals("a" + id ,name);
assertEquals(1, age);
Set<String> nickNames = a.getNickNames();
for (String nickName : nickNames)
assertEquals("nickName_" + id + "0", nickName);
}
/*
* check EntityA_Coll_Embed_EmbedXml
*/
public void checkEntityA_Coll_Embed_EmbedXml(EntityA_Coll_Embed_EmbedXml a) {
int id = a.getId();
String name = a.getName();
int age = a.getAge();
assertEquals(1, id);
assertEquals("a" + id ,name);
assertEquals(1, age);
List<Embed_EmbedXml> embeds = a.getEmbeds();
for (Embed_EmbedXml embed : embeds)
checkEmbed_EmbedXml(embed);
}
public void checkEmbed_EmbedXml(Embed_EmbedXml embed) {
int intVal1 = embed.getIntVal1();
int intVal2 = embed.getIntVal2();
int intVal3 = embed.getIntVal3();
assertEquals(101, intVal1);
assertEquals(102, intVal2);
assertEquals(103, intVal3);
EmbedXml embed1 = embed.getEmbed();
checkEmbedXml(embed1);
}
public void checkEmbedXml(EmbedXml embed) {
int intVal1 = embed.getIntVal1();
int intVal2 = embed.getIntVal2();
int intVal3 = embed.getIntVal3();
assertEquals(104, intVal1);
assertEquals(105, intVal2);
assertEquals(106, intVal3);
}
/*
* Query EntityA_Coll_StringXml
*/
public void queryEntityA_Coll_StringXml() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select a from EntityA_Coll_StringXml a");
List<EntityA_Coll_StringXml> as = q.getResultList();
for (EntityA_Coll_StringXml a : as) {
checkEntityA_Coll_StringXml(a);
}
tran.commit();
em.close();
}
/*
* Query EntityA_Coll_Embed_Embed
*/
public void queryEntityA_Coll_Embed_EmbedXml() {
EntityManager em = emf.createEntityManager();
EntityTransaction tran = em.getTransaction();
tran.begin();
Query q = em.createQuery("select a from EntityA_Coll_Embed_EmbedXml a");
List<EntityA_Coll_Embed_EmbedXml> as = q.getResultList();
for (EntityA_Coll_Embed_EmbedXml a : as) {
checkEntityA_Coll_Embed_EmbedXml(a);
}
tran.commit();
em.close();
}
}

View File

@ -101,4 +101,16 @@
<mapping-file>org/apache/openjpa/persistence/annotations/xml/orm.xml</mapping-file>
</persistence-unit>
<persistence-unit name="embed-pu">
<mapping-file>org/apache/openjpa/persistence/embed/embed-orm.xml</mapping-file>
<class>org.apache.openjpa.persistence.embed.EntityA_Coll_StringXml</class>
<class>org.apache.openjpa.persistence.embed.EntityA_Coll_Embed_EmbedXml</class>
<class>org.apache.openjpa.persistence.embed.Embed_EmbedXml</class>
<class>org.apache.openjpa.persistence.embed.EmbedXml</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)"/>
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- AtestCatalog metadata in xml -->
<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>embeddableXml</package>
<entity name="EntityA_Coll_StringXml"
class="org.apache.openjpa.persistence.embed.EntityA_Coll_StringXml"
access="FIELD">
<table name="EntityA_Coll_String_XML" />
<attributes>
<id name="id">
</id>
<basic name="name">
<column length="30" />
</basic>
<basic name="age" fetch="LAZY">
</basic>
<element-collection name="nickNames">
<column name="nickName_xml" length="20"/>
<collection-table name="NickNames_xml">
<join-column name="A_ID_xml" nullable="false"/>
<unique-constraint>
<column-name>A_ID_xml</column-name>
<column-name>nickName_xml</column-name>
</unique-constraint>
</collection-table>
</element-collection>
</attributes>
</entity>
<entity name="EntityA_Coll_Embed_EmbedXml"
class="org.apache.openjpa.persistence.embed.EntityA_Coll_Embed_EmbedXml"
access="FIELD">
<table name="EntityA_Coll_Embed_EmbedXML" />
<attributes>
<id name="id">
</id>
<basic name="name">
<column length="30" />
</basic>
<basic name="age" fetch="LAZY">
</basic>
<element-collection name="embeds">
</element-collection>
</attributes>
</entity>
<embeddable class="org.apache.openjpa.persistence.embed.Embed_EmbedXml"
access="FIELD">
<attributes>
<basic name="intVal1">
</basic>
<basic name="intVal2">
</basic>
<basic name="intVal3">
</basic>
<embedded name="embed">
<attribute-override name="intVal1">
<column name="embed_intVal1"/>
</attribute-override>
<attribute-override name="intVal2">
<column name="embed_intVal2"/>
</attribute-override>
<attribute-override name="intVal3">
<column name="embed_intVal3"/>
</attribute-override>
</embedded>
</attributes>
</embeddable>
<embeddable class="org.apache.openjpa.persistence.embed.EmbedXml"
access="FIELD">
<attributes>
<basic name="intVal1">
</basic>
<basic name="intVal2">
</basic>
<basic name="intVal3">
</basic>
</attributes>
</embeddable>
</entity-mappings>