diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java index b7560e89b..543dd5825 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/NumberVersionStrategy.java @@ -18,12 +18,12 @@ */ package org.apache.openjpa.jdbc.meta.strats; -import java.util.Map; import java.util.HashMap; +import java.util.Map; -import org.apache.openjpa.meta.JavaTypes; -import org.apache.openjpa.util.InternalException; +import org.apache.openjpa.jdbc.meta.FieldMapping; import org.apache.openjpa.jdbc.schema.Column; +import org.apache.openjpa.meta.JavaTypes; /** * Uses a version number for optimistic versioning. @@ -36,7 +36,8 @@ public class NumberVersionStrategy public static final String ALIAS = "version-number"; private Number _initial = 1; - + private Integer _javaType = null; + /** * Set the initial value for version column. Defaults to 1. */ @@ -56,7 +57,13 @@ public class NumberVersionStrategy } protected int getJavaType() { - return JavaTypes.INT; + if (_javaType == null && vers.getClassMapping().getVersionFieldMapping() != null) { + _javaType = Integer.valueOf(vers.getClassMapping().getVersionFieldMapping().getTypeCode()); + } else { + return JavaTypes.INT; + } + + return _javaType; } protected Object nextVersion(Object version) { diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/BaseEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/BaseEntity.java new file mode 100644 index 000000000..f5231350b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/BaseEntity.java @@ -0,0 +1,30 @@ +/* + * 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.jpql.version.type; + +import javax.persistence.*; + +@MappedSuperclass +abstract class BaseEntity { + + @Version + protected Long version; + + public Long getVersion() { return version; } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ChildVersionEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ChildVersionEntity.java new file mode 100644 index 000000000..0a44a8751 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ChildVersionEntity.java @@ -0,0 +1,33 @@ +/* + * 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.jpql.version.type; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class ChildVersionEntity extends BaseEntity { + + @Id + private int id; + + public int getId() { return id; } + public void setId(int id) { this.id = id; } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/LongVersionEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/LongVersionEntity.java new file mode 100644 index 000000000..c61dc3f21 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/LongVersionEntity.java @@ -0,0 +1,38 @@ +/* + * 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.jpql.version.type; + +import java.io.Serializable; +import java.sql.Timestamp; + +import javax.persistence.*; + +@Entity +public class LongVersionEntity implements Serializable { + @Id + private int id; + + @Version + protected Long version; + + public int getId() { return id; } + public void setId(int id) { this.id = id; } + public Long getVersion() { return version; } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeLongVersionEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeLongVersionEntity.java new file mode 100644 index 000000000..fec896513 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeLongVersionEntity.java @@ -0,0 +1,37 @@ +/* + * 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.jpql.version.type; + +import java.io.Serializable; + +import javax.persistence.*; + +@Entity +public class PrimativeLongVersionEntity implements Serializable { + @Id + private int id; + + @Version + protected long version; + + public int getId() { return id; } + public void setId(int id) { this.id = id; } + public long getVersion() { return version; } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeShortVersionEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeShortVersionEntity.java new file mode 100644 index 000000000..593d9a661 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/PrimativeShortVersionEntity.java @@ -0,0 +1,37 @@ +/* + * 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.jpql.version.type; + +import java.io.Serializable; + +import javax.persistence.*; + +@Entity +public class PrimativeShortVersionEntity implements Serializable { + @Id + private int id; + + @Version + protected short version; + + public int getId() { return id; } + public void setId(int id) { this.id = id; } + public short getVersion() { return version; } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ShortVersionEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ShortVersionEntity.java new file mode 100644 index 000000000..257fc013d --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/ShortVersionEntity.java @@ -0,0 +1,37 @@ +/* + * 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.jpql.version.type; + +import java.io.Serializable; + +import javax.persistence.*; + +@Entity +public class ShortVersionEntity implements Serializable { + @Id + private int id; + + @Version + protected Short version; + + public int getId() { return id; } + public void setId(int id) { this.id = id; } + public Short getVersion() { return version; } +} + diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TestVersionFieldType.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TestVersionFieldType.java new file mode 100644 index 000000000..da4a05659 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TestVersionFieldType.java @@ -0,0 +1,100 @@ +/* + * 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.jpql.version.type; + +import java.sql.Timestamp; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Query; + +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +/** + * Verifies that the version field is of the proper java type + * when returned from a query. See OPENJPA-2435. + */ +public class TestVersionFieldType extends SingleEMFTestCase { + + public void setUp() { + setUp(CLEAR_TABLES, LongVersionEntity.class, + ShortVersionEntity.class, PrimativeLongVersionEntity.class, + PrimativeShortVersionEntity.class, TimestampVersionEntity.class, + BaseEntity.class, ChildVersionEntity.class); + createTestData(); + } + + public void testProjectionVersionReturnType() { + verifyType(LongVersionEntity.class, Long.class); + verifyType(ShortVersionEntity.class, Short.class); + verifyType(PrimativeShortVersionEntity.class, Short.class); + verifyType(PrimativeLongVersionEntity.class, Long.class); + verifyType(ChildVersionEntity.class, Long.class); + verifyType(TimestampVersionEntity.class, Timestamp.class); + } + + public void verifyType(Class cls, Class expectedClsType) { + + EntityManager em = emf.createEntityManager(); + String str = "SELECT o.id, o.version FROM " + cls.getName() + " o"; + Query query = em.createQuery(str); + List objectList = query.getResultList(); + + for (Object[] objects : objectList) { + assertNotNull("Version should not be null.", objects[1]); + assertTrue("Type should be " + expectedClsType.getName() + + ". But it is " + objects[1].getClass(), + objects[1].getClass() == expectedClsType); + } + + em.close(); + } + + public void createTestData() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + + LongVersionEntity lve = new LongVersionEntity(); + lve.setId(9); + em.persist(lve); + + ShortVersionEntity sve = new ShortVersionEntity(); + sve.setId(9); + em.persist(sve); + + PrimativeShortVersionEntity psve = new PrimativeShortVersionEntity(); + psve.setId(9); + em.persist(psve); + + PrimativeLongVersionEntity plve = new PrimativeLongVersionEntity(); + plve.setId(9); + em.persist(plve); + + TimestampVersionEntity tve = new TimestampVersionEntity(); + tve.setId(9); + em.persist(tve); + + ChildVersionEntity ave = new ChildVersionEntity(); + ave.setId(9); + em.persist(ave); + + em.getTransaction().commit(); + em.close(); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TimestampVersionEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TimestampVersionEntity.java new file mode 100644 index 000000000..9ebc88b28 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jpql/version/type/TimestampVersionEntity.java @@ -0,0 +1,38 @@ +/* + * 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.jpql.version.type; + +import java.io.Serializable; +import java.sql.Timestamp; + +import javax.persistence.*; + +@Entity +public class TimestampVersionEntity implements Serializable { + @Id + private int id; + + @Version + protected Timestamp version; + + public int getId() { return id; } + public void setId(int id) { this.id = id; } + public Timestamp getVersion() { return version; } +} +