diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java index 2a30d10ea..e204868b4 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java @@ -31,6 +31,8 @@ import java.io.ObjectStreamException; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.math.BigDecimal; +import java.math.BigInteger; import java.security.AccessController; import java.security.PrivilegedActionException; import java.util.ArrayList; @@ -2207,6 +2209,10 @@ public class PCEnhancer { code.checkcast().setType(DateId.class); code.invokevirtual().setMethod(DateId.class, "getId", Date.class, null); + if (pktype != Date.class) { + // java.sql.Date.class + code.checkcast().setType(pktype); + } break; case JavaTypes.STRING: code.aload().setLocal(oid); @@ -2218,13 +2224,13 @@ public class PCEnhancer { code.aload().setLocal(oid); code.checkcast().setType(BigDecimalId.class); code.invokevirtual().setMethod(BigDecimalId.class, "getId", - BigDecimalId.class, null); + BigDecimal.class, null); break; case JavaTypes.BIGINTEGER: code.aload().setLocal(oid); code.checkcast().setType(BigIntegerId.class); code.invokevirtual().setMethod(BigIntegerId.class, "getId", - BigIntegerId.class, null); + BigInteger.class, null); break; default: code.aload().setLocal(oid); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EBigDecimalID.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EBigDecimalID.java new file mode 100644 index 000000000..762486c55 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EBigDecimalID.java @@ -0,0 +1,56 @@ +/* + * 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.derivedid; + +import java.math.BigDecimal; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +@Entity +public class EBigDecimalID { + @Id + @Column(precision=20,scale=5) + private BigDecimal id; + + private String name; + + public EBigDecimalID() {} + + public EBigDecimalID(BigDecimal id) { + this.id = id; + this.name = "BigDecimalID "+id; + } + + public BigDecimal getId() { + return id; + } + + public void setId(BigDecimal id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EBigIntegerID.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EBigIntegerID.java new file mode 100644 index 000000000..f29119125 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EBigIntegerID.java @@ -0,0 +1,55 @@ +/* + * 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.derivedid; + +import java.math.BigInteger; + +import javax.persistence.Entity; +import javax.persistence.Id; +@Entity +public class EBigIntegerID { + @Id + private BigInteger id; + + private String name; + + public EBigIntegerID() {} + + + public EBigIntegerID(BigInteger id) { + this.id = id; + this.name = "BigIntegerID "+id; + } + + public BigInteger getId() { + return id; + } + + public void setId(BigInteger id) { + this.id = id; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDBigDecimalID.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDBigDecimalID.java new file mode 100644 index 000000000..9f892ffa8 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDBigDecimalID.java @@ -0,0 +1,55 @@ +/* + * 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.derivedid; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class EDBigDecimalID { + @Id + @OneToOne + private EBigDecimalID rid; + + private String name; + + public EDBigDecimalID() {} + + public EDBigDecimalID(EBigDecimalID rid) { + this.rid = rid; + this.name = "Rel BigDecimalID "+rid.getId(); + } + + public EBigDecimalID getRid() { + return rid; + } + + public void setRid(EBigDecimalID rid) { + this.rid = rid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDBigIntegerID.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDBigIntegerID.java new file mode 100644 index 000000000..0760af780 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDBigIntegerID.java @@ -0,0 +1,55 @@ +/* + * 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.derivedid; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class EDBigIntegerID { + @Id + @OneToOne + private EBigIntegerID rid; + + private String name; + + public EDBigIntegerID() {} + + public EDBigIntegerID(EBigIntegerID rid) { + this.rid = rid; + this.name = "Rel BigIntegerID "+rid.getId(); + } + + public EBigIntegerID getRid() { + return rid; + } + + public void setRid(EBigIntegerID rid) { + this.rid = rid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDDateID.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDDateID.java new file mode 100644 index 000000000..ef5116a5a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDDateID.java @@ -0,0 +1,55 @@ +/* + * 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.derivedid; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class EDDateID { + @Id + @OneToOne + private EDateID rid; + + private String name; + + public EDDateID() {} + + public EDDateID(EDateID rid) { + this.rid = rid; + this.name = "Rel DateID "+rid.getId(); + } + + public EDateID getRid() { + return rid; + } + + public void setRid(EDateID rid) { + this.rid = rid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDSQLDateID.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDSQLDateID.java new file mode 100644 index 000000000..d427d5a98 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDSQLDateID.java @@ -0,0 +1,55 @@ +/* + * 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.derivedid; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class EDSQLDateID { + @Id + @OneToOne + private ESQLDateID rid; + + private String name; + + public EDSQLDateID() {} + + public EDSQLDateID(ESQLDateID rid) { + this.rid = rid; + this.name = "Rel SQLDateID "+rid.getId(); + } + + public ESQLDateID getRid() { + return rid; + } + + public void setRid(ESQLDateID rid) { + this.rid = rid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDateID.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDateID.java new file mode 100644 index 000000000..2bffe32f9 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/EDateID.java @@ -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.derivedid; + +import java.util.Date; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class EDateID { + @Id + private Date id; + private String name; + + public EDateID() {} + + public EDateID(Date id) { + this.id = id; + this.name = "DateID "+id; + } + + public Date getId() { + return id; + } + + public void setId(Date id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/ESQLDateID.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/ESQLDateID.java new file mode 100644 index 000000000..74daf00cb --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/ESQLDateID.java @@ -0,0 +1,53 @@ +/* + * 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.derivedid; + +import java.sql.Date; + +import javax.persistence.Entity; +import javax.persistence.Id; +@Entity +public class ESQLDateID { + @Id + private Date id; + private String name; + + public ESQLDateID() {} + + public ESQLDateID(Date id) { + this.id = id; + this.name = "SQLDateID "+id; + } + + public Date getId() { + return id; + } + + public void setId(Date id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/TestDerivedIdentity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/TestDerivedIdentity.java new file mode 100644 index 000000000..472344e36 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/derivedid/TestDerivedIdentity.java @@ -0,0 +1,123 @@ +/* + * 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.derivedid; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.Date; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import org.apache.openjpa.persistence.test.SQLListenerTestCase; + +public class TestDerivedIdentity extends SQLListenerTestCase { + + public void setUp() { + setUp(EBigDecimalID.class, EDBigDecimalID.class, + EBigIntegerID.class, EDBigIntegerID.class, + EDateID.class, EDDateID.class, + ESQLDateID.class, EDSQLDateID.class, + CLEAR_TABLES); + populate(); + } + + public void populate() { + EntityManager em = emf.createEntityManager(); + + for (int i = 0; i < 2; i++) { + long time = (long) (System.currentTimeMillis() / 1000)+i; + BigDecimal did = new BigDecimal(time); + EBigDecimalID e1 = new EBigDecimalID(did); + EDBigDecimalID e2 = new EDBigDecimalID(e1); + em.persist(e1); + em.persist(e2); + + int time2 = (int) (System.currentTimeMillis() / 1000)+i; + BigInteger iid = new BigInteger(Integer.toString(time2)); + EBigIntegerID e3 = new EBigIntegerID(iid); + EDBigIntegerID e4 = new EDBigIntegerID(e3); + em.persist(e3); + em.persist(e4); + + Date id = new Date(time); + EDateID e5 = new EDateID(id); + EDDateID e6 = new EDDateID(e5); + em.persist(e5); + em.persist(e6); + + if (i == 0) { + java.sql.Date sid = new java.sql.Date(time); + ESQLDateID e7 = new ESQLDateID(sid); + EDSQLDateID e8 = new EDSQLDateID(e7); + em.persist(e7); + em.persist(e8); + } + } + + em.getTransaction().begin(); + em.getTransaction().commit(); + em.close(); + } + + public void testDerivedIdentity() { + EntityManager em = emf.createEntityManager(); + Query query = null; + String str[] = { + "select e from EDDateID e", + "select e from EDBigDecimalID e", + "select e from EDBigIntegerID e", + "select e from EDSQLDateID e", + "select e from EDDateID e join fetch e.rid", + "select e from EDBigDecimalID e join fetch e.rid", + "select e from EDBigIntegerID e join fetch e.rid", + "select e from EDSQLDateID e join fetch e.rid", + }; + for (int i = 0; i < str.length; i++) { + query = em.createQuery(str[i]); + List rs = query.getResultList(); + assertTrue(rs.size() > 0); + for (int j = 0; j < rs.size(); j++) { + Object e = rs.get(j); + String name = null; + Object oid = null; + if (e instanceof EDDateID) { + name = ((EDDateID)e).getName(); + oid = ((EDDateID)e).getRid().getId(); + } else if (e instanceof EDBigDecimalID) { + name = ((EDBigDecimalID)e).getName(); + oid = ((EDBigDecimalID)e).getRid().getId(); + } else if (e instanceof EDBigIntegerID) { + name = ((EDBigIntegerID)e).getName(); + oid = ((EDBigIntegerID)e).getRid().getId(); + } else if (e instanceof EDSQLDateID) { + name = ((EDSQLDateID)e).getName(); + oid = ((EDSQLDateID)e).getRid().getId(); + } + //System.out.println(name); + //System.out.println(oid.toString()); + assertTrue(name.startsWith("Rel")); + } + } + + em.close(); + } +} +