From 024faa31d75a55a619b1e59ff40b7523c24c4c84 Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Tue, 5 Feb 2008 21:53:39 +0000 Subject: [PATCH] OPENJPA-509 Fix & Test for insertion of MappedSuperClass-Entity-Embedded domain model insertion failure. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@618794 13f79535-47bb-0310-9956-ffa450edef68 --- .../jdbc/meta/strats/EmbedFieldStrategy.java | 3 +- .../openjpa/persistence/embed/Address.java | 78 +++++++++++++++++++ .../openjpa/persistence/embed/BaseEntity.java | 42 ++++++++++ .../openjpa/persistence/embed/Geocode.java | 48 ++++++++++++ .../persistence/embed/TestEmbedded.java | 46 +++++++++++ 5 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Address.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/BaseEntity.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Geocode.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedFieldStrategy.java index e792d1180..016d53d97 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/EmbedFieldStrategy.java @@ -165,7 +165,8 @@ public class EmbedFieldStrategy FieldMapping[] fields = field.getEmbeddedMapping().getFieldMappings(); for (int i = 0; i < fields.length; i++) if (!Boolean.TRUE.equals(fields[i].isCustomInsert(em, store))) - fields[i].insert(em, store, rm); + if (!fields[i].isPrimaryKey()) + fields[i].insert(em, store, rm); if (field.getColumnIO().isInsertable(0, true)) setNullIndicatorColumn(sm, row); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Address.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Address.java new file mode 100644 index 000000000..a5ba20141 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Address.java @@ -0,0 +1,78 @@ +/* + * 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.*; + +/** + * An entity that extends a MappedSuperClass and embeds an entity. + * + * @author Pinaki Poddar + * + */ +@Entity +public class Address extends BaseEntity { + protected String streetAddress, city, state; + + Geocode geocode; + + @Embedded + public Geocode getGeocode() { + return geocode; + } + + public void setGeocode(Geocode geocode) { + this.geocode = geocode; + } + + public String getStreetAddress() { + return streetAddress; + } + + public void setStreetAddress(String streetAddress) { + this.streetAddress = streetAddress; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + protected Integer zip; + + public Integer getZip() { + return zip; + } + + public void setZip(Integer zip) { + this.zip = zip; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/BaseEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/BaseEntity.java new file mode 100644 index 000000000..621dd95b6 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/BaseEntity.java @@ -0,0 +1,42 @@ +/* + * 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.*; + +/** + * Mapped Super Class using auto-generated identity. + * + * @author Pinaki Poddar + * + */ +@MappedSuperclass +public class BaseEntity { + protected Long id; + + @Id + @GeneratedValue + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} \ No newline at end of file diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Geocode.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Geocode.java new file mode 100644 index 000000000..e926b24c5 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Geocode.java @@ -0,0 +1,48 @@ +/* + * 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.*; + +/** + * An embedded entity. + * + * @author Pinaki Poddar + * + */ +@Embeddable +public class Geocode extends BaseEntity { + float latitude, longtitude; + + public float getLatitude() { + return latitude; + } + + public void setLatitude(float latitude) { + this.latitude = latitude; + } + + public float getLongtitude() { + return longtitude; + } + + public void setLongtitude(float longtitude) { + this.longtitude = longtitude; + } +} \ No newline at end of file diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java new file mode 100644 index 000000000..24a54647c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java @@ -0,0 +1,46 @@ +/* + * 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.EntityManager; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestEmbedded extends SingleEMFTestCase { + public void setUp() { + super.setUp(BaseEntity.class, Address.class, Geocode.class, + CLEAR_TABLES); + } + + public void testInsertEmbedded() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + Address a = new Address(); + a.setStreetAddress("123 Main St"); + a.setCity("Chicago"); + a.setState("IL"); + a.setZip(60606); + Geocode g = new Geocode(); + g.setLatitude(1.0f); + g.setLongtitude(2.0f); + a.setGeocode(g); + em.persist(a); + em.getTransaction().commit(); + } + +} \ No newline at end of file