mirror of https://github.com/apache/openjpa.git
additional test for embeddable, elementcollection and lob
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@805573 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4e6a98f634
commit
30a7da0ba2
|
@ -0,0 +1,196 @@
|
|||
/*
|
||||
* 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.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.AttributeOverride;
|
||||
import javax.persistence.AttributeOverrides;
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Lob;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import javax.persistence.Transient;
|
||||
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.CollectionTable;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="TBL3C")
|
||||
public class EntityA_Embed_Complex implements Serializable {
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
@Column(length=30)
|
||||
String name;
|
||||
|
||||
@Basic(fetch=FetchType.LAZY)
|
||||
int age;
|
||||
|
||||
@ElementCollection(fetch=FetchType.EAGER)
|
||||
@CollectionTable(name="NickNames_Tbl")
|
||||
@Column(name="nicknames1", length=20)
|
||||
protected Set<String> nickNames = new HashSet<String>();
|
||||
|
||||
@ElementCollection
|
||||
@Enumerated(EnumType.ORDINAL)
|
||||
protected List<CreditRating> cr = new ArrayList<CreditRating>();
|
||||
|
||||
@ElementCollection
|
||||
@Temporal(TemporalType.DATE)
|
||||
protected List<Timestamp> ts = new ArrayList<Timestamp>();
|
||||
|
||||
@ElementCollection
|
||||
@Lob
|
||||
protected List<String> lobs = new ArrayList<String>();
|
||||
|
||||
protected Embed_Embed embed;
|
||||
|
||||
@ElementCollection
|
||||
protected List<Embed_Embed> embeds = new ArrayList<Embed_Embed>();
|
||||
|
||||
@ElementCollection(fetch=FetchType.EAGER)
|
||||
@CollectionTable(name="EMBED1ToOneS2") // use default join column name
|
||||
@AttributeOverrides({
|
||||
@AttributeOverride(name="name1", column=@Column(name="EMB_NAME1")),
|
||||
@AttributeOverride(name="name2", column=@Column(name="EMB_NAME2")),
|
||||
@AttributeOverride(name="name3", column=@Column(name="EMB_NAME3"))
|
||||
})
|
||||
protected Set<Embed_ToOne> embed1s = new HashSet<Embed_ToOne>();
|
||||
|
||||
private transient Integer transientJavaValue;
|
||||
|
||||
@Transient
|
||||
private Integer transientValue;
|
||||
|
||||
|
||||
/*
|
||||
* Getters/Setters
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
public List<CreditRating> getCreditRating() {
|
||||
return cr;
|
||||
}
|
||||
|
||||
public void addCreditRating(CreditRating c) {
|
||||
cr.add(c);
|
||||
}
|
||||
|
||||
public List<Timestamp> getTimestamps() {
|
||||
return ts;
|
||||
}
|
||||
|
||||
public void addTimestamp(Timestamp t) {
|
||||
ts.add(t);
|
||||
}
|
||||
|
||||
public List<String> getLobs() {
|
||||
return lobs;
|
||||
}
|
||||
|
||||
public void addLob(String lob) {
|
||||
lobs.add(lob);
|
||||
}
|
||||
|
||||
public enum CreditRating { POOR, GOOD, EXCELLENT };
|
||||
|
||||
public Embed_Embed getEmbed() {
|
||||
return embed;
|
||||
}
|
||||
|
||||
public void setEmbed(Embed_Embed embed) {
|
||||
this.embed = embed;
|
||||
}
|
||||
|
||||
public List<Embed_Embed> getEmbeds() {
|
||||
return embeds;
|
||||
}
|
||||
|
||||
public void addEmbed(Embed_Embed embed) {
|
||||
embeds.add(embed);
|
||||
}
|
||||
|
||||
public Set<Embed_ToOne> getEmbed1ToOnes() {
|
||||
return embed1s;
|
||||
}
|
||||
|
||||
public void addEmbed1ToOnes(Embed_ToOne embed1) {
|
||||
embed1s.add(embed1);
|
||||
}
|
||||
|
||||
public Integer getTransientJavaValue() {
|
||||
return this.transientJavaValue;
|
||||
}
|
||||
|
||||
public void setTransientJavaValue(Integer transientJavaValue) {
|
||||
this.transientJavaValue = transientJavaValue;
|
||||
}
|
||||
|
||||
public Integer getTransientValue() {
|
||||
return this.transientValue;
|
||||
}
|
||||
|
||||
public void setTransientValue(Integer transientValue) {
|
||||
this.transientValue = transientValue;
|
||||
}
|
||||
}
|
|
@ -19,8 +19,10 @@
|
|||
package org.apache.openjpa.persistence.embed;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -32,11 +34,13 @@ import javax.persistence.EntityTransaction;
|
|||
import javax.persistence.Query;
|
||||
|
||||
|
||||
import org.apache.openjpa.lib.log.Log;
|
||||
import org.apache.openjpa.persistence.ArgumentException;
|
||||
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||
|
||||
public class TestEmbeddable extends SingleEMFTestCase {
|
||||
|
||||
private static final Calendar cal = new GregorianCalendar();
|
||||
private static final Integer timeHash = new Integer(cal.hashCode());
|
||||
public int numEmbeddables = 1;
|
||||
public int numBasicTypes = 1;
|
||||
public int numProgramManagers = 2;
|
||||
|
@ -86,7 +90,8 @@ public class TestEmbeddable extends SingleEMFTestCase {
|
|||
VicePresident.class, EntityA_Embed_MappedToOne.class,
|
||||
Embed_MappedToOne.class, Embed_MappedToOneCascadeDelete.class,
|
||||
EntityA_Embed_MappedToOneCascadeDelete.class, EntityB2.class,
|
||||
Book.class, Listing.class, Seller.class, DROP_TABLES);
|
||||
Book.class, Listing.class, Seller.class,
|
||||
EntityA_Embed_Complex.class, CLEAR_TABLES);
|
||||
}
|
||||
|
||||
public void testEntityA_Coll_String() {
|
||||
|
@ -185,6 +190,7 @@ public class TestEmbeddable extends SingleEMFTestCase {
|
|||
public void testEmbeddableContainingRelationWithGeneratedKey() {
|
||||
createEmbeddableContainingRelationWithGeneratedKey();
|
||||
}
|
||||
|
||||
/*
|
||||
* Create EntityA_Coll_String
|
||||
*/
|
||||
|
@ -2524,7 +2530,8 @@ public class TestEmbeddable extends SingleEMFTestCase {
|
|||
}
|
||||
tran.commit();
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void createEmbeddableContainingRelationWithGeneratedKey() {
|
||||
EntityManager em = emf.createEntityManager();
|
||||
EntityTransaction tran = em.getTransaction();
|
||||
|
@ -2548,6 +2555,236 @@ public class TestEmbeddable extends SingleEMFTestCase {
|
|||
assertNotNull(seller);
|
||||
assertTrue(seller.getId() != 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* EntityA_Embed_Complex routines
|
||||
*/
|
||||
public void testEntityA_Embed_Complex() {
|
||||
getLog().trace("testEntityA_Embed_Complex() - entered");
|
||||
createEntityA_Embed_Complex(null);
|
||||
queryEntityA_Embed_Complex(null);
|
||||
findEntityA_Embed_Complex(null);
|
||||
}
|
||||
|
||||
public void testEntityA_Embed_Complex2() {
|
||||
getLog().trace("testEntityA_Embed_Complex2() - entered");
|
||||
EntityManager em = emf.createEntityManager();
|
||||
em.clear();
|
||||
createEntityA_Embed_Complex(em);
|
||||
em.clear();
|
||||
//queryEntityA_Embed_Complex(em);
|
||||
//em.clear();
|
||||
findEntityA_Embed_Complex(em);
|
||||
em.clear();
|
||||
updateEntityA_Embed_Complex(em);
|
||||
em.clear();
|
||||
findEntityA_Embed_Complex(em);
|
||||
em.clear();
|
||||
removeEntityA_Embed_Complex(em);
|
||||
em.close();
|
||||
}
|
||||
|
||||
public void createEntityA_Embed_Complex(EntityManager em) {
|
||||
Boolean emClose = false;
|
||||
if (em == null) {
|
||||
em = emf.createEntityManager();
|
||||
emClose = true;
|
||||
}
|
||||
em.getTransaction().begin();
|
||||
createEntityA_Embed_Complex(em, ID);
|
||||
//em.getTransaction().begin();
|
||||
em.flush();
|
||||
em.getTransaction().commit();
|
||||
if (emClose) {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void createEntityA_Embed_Complex(EntityManager em, int id) {
|
||||
getLog().trace("createEntityA_Embed_Complex() - entered");
|
||||
EntityA_Embed_Complex a = new EntityA_Embed_Complex();
|
||||
a.setId(id);
|
||||
a.setName("a" + id);
|
||||
a.setAge(id);
|
||||
for (int i = 0; i < numBasicTypes; i++) {
|
||||
a.addNickName("nickName_" + id + i);
|
||||
}
|
||||
a.addCreditRating(EntityA_Embed_Complex.CreditRating.POOR);
|
||||
a.addTimestamp(new Timestamp(cal.getTimeInMillis()));
|
||||
a.addLob("lob_0");
|
||||
a.setEmbed(createEmbed_Embed(em, numEmbeddables, 0));
|
||||
for (int i = 0; i < numEmbeddables; i++) {
|
||||
Embed_Embed embed = createEmbed_Embed(em, id, i);
|
||||
a.addEmbed(embed);
|
||||
}
|
||||
for (int i = 0; i < numEmbeddables; i++) {
|
||||
Embed_ToOne embed = createEmbed_ToOne(em, i+id);
|
||||
a.addEmbed1ToOnes(embed);
|
||||
}
|
||||
a.setTransientJavaValue(timeHash);
|
||||
a.setTransientValue(timeHash);
|
||||
em.persist(a);
|
||||
}
|
||||
|
||||
public void checkEntityA_Embed_Complex(EntityA_Embed_Complex a) {
|
||||
getLog().trace("checkEntityA_Embed_Complex() - entered");
|
||||
int id = a.getId();
|
||||
String name = a.getName();
|
||||
int age = a.getAge();
|
||||
assertEquals(ID, id);
|
||||
assertEquals("a" + id ,name);
|
||||
assertEquals(ID, age);
|
||||
Set<String> nickNames = a.getNickNames();
|
||||
for (String nickName : nickNames) {
|
||||
assertEquals("nickName_" + id + "0", nickName);
|
||||
}
|
||||
List<EntityA_Embed_Complex.CreditRating> cr = a.getCreditRating();
|
||||
for (EntityA_Embed_Complex.CreditRating c : cr) {
|
||||
assertEquals("POOR", c.toString());
|
||||
}
|
||||
List<Timestamp> tstamps = a.getTimestamps();
|
||||
for (Timestamp ts : tstamps) {
|
||||
assertNotEquals(0, ts.getTime());
|
||||
}
|
||||
List<String> lobs = a.getLobs();
|
||||
int i = 0;
|
||||
for (String lob : lobs) {
|
||||
assertEquals("lob_" + i++, lob);
|
||||
}
|
||||
Embed_Embed embedded = a.getEmbed();
|
||||
checkEmbed_Embed(embedded);
|
||||
List<Embed_Embed> embeds = a.getEmbeds();
|
||||
for (Embed_Embed embed : embeds) {
|
||||
checkEmbed_Embed(embed);
|
||||
}
|
||||
Set<Embed_ToOne> embedOnes = a.getEmbed1ToOnes();
|
||||
for (Embed_ToOne embed : embedOnes) {
|
||||
checkEmbed_ToOne(embed);
|
||||
}
|
||||
assertNotEquals(a.getTransientJavaValue(), timeHash);
|
||||
assertNotEquals(a.getTransientValue(), timeHash);
|
||||
}
|
||||
|
||||
public void findEntityA_Embed_Complex(EntityManager em) {
|
||||
Boolean emClose = false;
|
||||
getLog().trace("findEntityA_Embed_Complex() - entered");
|
||||
if (em == null) {
|
||||
em = emf.createEntityManager();
|
||||
emClose = true;
|
||||
}
|
||||
EntityA_Embed_Complex a = em.find(EntityA_Embed_Complex.class, ID);
|
||||
checkEntityA_Embed_Complex(a);
|
||||
if (emClose) {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void updateEntityA_Embed_Complex(EntityManager em) {
|
||||
Boolean emClose = false;
|
||||
getLog().trace("updateEntityA_Embed_Complex() - entered");
|
||||
if (em == null) {
|
||||
em = emf.createEntityManager();
|
||||
emClose = true;
|
||||
}
|
||||
em.getTransaction().begin();
|
||||
EntityA_Embed_Complex a = em.find(EntityA_Embed_Complex.class, ID);
|
||||
checkEntityA_Embed_Complex(a);
|
||||
for (int i = 1; i < numEmbeddables; i++) {
|
||||
a.addLob("lob_" + i);
|
||||
}
|
||||
a.setTransientJavaValue(2009);
|
||||
a.setTransientValue(2009);
|
||||
em.persist(a);
|
||||
em.flush();
|
||||
em.getTransaction().commit();
|
||||
if (emClose) {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void removeEntityA_Embed_Complex(EntityManager em) {
|
||||
Boolean emClose = false;
|
||||
getLog().trace("removeEntityA_Embed_Complex() - entered");
|
||||
if (em == null) {
|
||||
em = emf.createEntityManager();
|
||||
emClose = true;
|
||||
}
|
||||
em.getTransaction().begin();
|
||||
EntityA_Embed_Complex a = em.find(EntityA_Embed_Complex.class, ID);
|
||||
checkEntityA_Embed_Complex(a);
|
||||
em.remove(a);
|
||||
em.flush();
|
||||
em.getTransaction().commit();
|
||||
em.clear();
|
||||
a = em.find(EntityA_Embed_Complex.class, ID);
|
||||
assertNull("Entity should no longer exist", a);
|
||||
if (emClose) {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void queryEntityA_Embed_Complex(EntityManager em) {
|
||||
Boolean emClose = false;
|
||||
getLog().trace("queryEntityA_Embed_Complex() - entered");
|
||||
if (em == null) {
|
||||
em = emf.createEntityManager();
|
||||
emClose = true;
|
||||
}
|
||||
String[] query = {
|
||||
"select e from " +
|
||||
" EntityA_Embed_Complex a " +
|
||||
" , in (a.nickNames) e order by a.id",
|
||||
"select e from " +
|
||||
" EntityA_Embed_Complex a " +
|
||||
" , in (a.nickNames) e order by a.id",
|
||||
"select e from " +
|
||||
" EntityA_Embed_Complex a " +
|
||||
" , in (a.nickNames) e order by e",
|
||||
"select a from " +
|
||||
" EntityA_Embed_Complex a " +
|
||||
" WHERE a.nickNames IS EMPTY order by a",
|
||||
"select a from " +
|
||||
" EntityA_Embed_Complex a " +
|
||||
" WHERE exists (select n from EntityA_Embed_Complex a, " +
|
||||
" in (a.nickNames) n where n like '%1') " +
|
||||
" order by a",
|
||||
};
|
||||
List rs = null;
|
||||
for (int i = 0; i < query.length; i++) {
|
||||
rs = em.createQuery(query[i]).getResultList();
|
||||
switch (i) {
|
||||
case 0:
|
||||
case 1:
|
||||
case 2:
|
||||
assertTrue(rs.size() > 0);
|
||||
Object obj = rs.get(0);
|
||||
assertTrue(obj instanceof String);
|
||||
break;
|
||||
case 3:
|
||||
case 4:
|
||||
assertTrue(rs.size() == 0);
|
||||
}
|
||||
em.clear();
|
||||
}
|
||||
em.getTransaction().begin();
|
||||
Query q = em.createQuery("select a from EntityA_Embed_Complex a");
|
||||
List<EntityA_Embed_Complex> as = q.getResultList();
|
||||
for (EntityA_Embed_Complex a : as) {
|
||||
checkEntityA_Embed_Complex(a);
|
||||
}
|
||||
em.getTransaction().commit();
|
||||
if (emClose) {
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal convenience method for getting the OpenJPA logger
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private Log getLog() {
|
||||
return emf.getConfiguration().getLog("Tests");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue