mirror of https://github.com/apache/openjpa.git
OPENJPA-2435: Version field in a projection always returned as an Integer. Merged 2.1.x changes to trunk.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1533289 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bb743b5f15
commit
83dab92168
|
@ -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,6 +36,7 @@ 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) {
|
||||
|
|
|
@ -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; }
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
|
|
@ -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; }
|
||||
}
|
||||
|
|
@ -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; }
|
||||
}
|
||||
|
|
@ -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; }
|
||||
}
|
||||
|
|
@ -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; }
|
||||
}
|
||||
|
|
@ -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<Object[]> 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();
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
|
Loading…
Reference in New Issue