OPENJPA-1442 JPA2 java.sql.Date, java.math.BigDecimal, or java.math.BigInteger types as derived primary keys are not Enhanced correctly

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@893642 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Catalina Wei 2009-12-23 22:33:57 +00:00
parent 775c627b80
commit a0e001e40a
10 changed files with 569 additions and 2 deletions

View File

@ -31,6 +31,8 @@ import java.io.ObjectStreamException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedActionException; import java.security.PrivilegedActionException;
import java.util.ArrayList; import java.util.ArrayList;
@ -2207,6 +2209,10 @@ public class PCEnhancer {
code.checkcast().setType(DateId.class); code.checkcast().setType(DateId.class);
code.invokevirtual().setMethod(DateId.class, "getId", code.invokevirtual().setMethod(DateId.class, "getId",
Date.class, null); Date.class, null);
if (pktype != Date.class) {
// java.sql.Date.class
code.checkcast().setType(pktype);
}
break; break;
case JavaTypes.STRING: case JavaTypes.STRING:
code.aload().setLocal(oid); code.aload().setLocal(oid);
@ -2218,13 +2224,13 @@ public class PCEnhancer {
code.aload().setLocal(oid); code.aload().setLocal(oid);
code.checkcast().setType(BigDecimalId.class); code.checkcast().setType(BigDecimalId.class);
code.invokevirtual().setMethod(BigDecimalId.class, "getId", code.invokevirtual().setMethod(BigDecimalId.class, "getId",
BigDecimalId.class, null); BigDecimal.class, null);
break; break;
case JavaTypes.BIGINTEGER: case JavaTypes.BIGINTEGER:
code.aload().setLocal(oid); code.aload().setLocal(oid);
code.checkcast().setType(BigIntegerId.class); code.checkcast().setType(BigIntegerId.class);
code.invokevirtual().setMethod(BigIntegerId.class, "getId", code.invokevirtual().setMethod(BigIntegerId.class, "getId",
BigIntegerId.class, null); BigInteger.class, null);
break; break;
default: default:
code.aload().setLocal(oid); code.aload().setLocal(oid);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

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.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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}