mirror of https://github.com/apache/openjpa.git
[OPENJPA-2780] reverse customizer adds @Enumerated annotation as expected
This commit is contained in:
parent
cb20dd6b95
commit
75999ad5d9
|
@ -267,7 +267,7 @@ public class AnnotationPersistenceMappingSerializer
|
||||||
AnnotationBuilder ab) {
|
AnnotationBuilder ab) {
|
||||||
List<Column> cols = null;
|
List<Column> cols = null;
|
||||||
if (secondaryInfo != null)
|
if (secondaryInfo != null)
|
||||||
cols = (List<Column>) secondaryInfo.getSecondaryTableJoinColumns
|
cols = secondaryInfo.getSecondaryTableJoinColumns
|
||||||
(table);
|
(table);
|
||||||
|
|
||||||
boolean print = (cols != null && cols.size() > 0) ||
|
boolean print = (cols != null && cols.size() > 0) ||
|
||||||
|
@ -322,11 +322,11 @@ public class AnnotationPersistenceMappingSerializer
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
ValueMappingInfo info = field.getValueInfo();
|
ValueMappingInfo info = field.getValueInfo();
|
||||||
List<Column> cols = (List<Column>) info.getColumns();
|
List<Column> cols = info.getColumns();
|
||||||
if (cols == null || cols.size() == 0)
|
if (cols == null || cols.size() == 0)
|
||||||
return false;
|
return false;
|
||||||
ValueMappingInfo info2 = field2.getValueInfo();
|
ValueMappingInfo info2 = field2.getValueInfo();
|
||||||
List<Column> cols2 = (List<Column>) info2.getColumns();
|
List<Column> cols2 = info2.getColumns();
|
||||||
if (cols2 == null || cols2.size() != cols.size())
|
if (cols2 == null || cols2.size() != cols.size())
|
||||||
return true;
|
return true;
|
||||||
if (cols.size() != 1)
|
if (cols.size() != 1)
|
||||||
|
@ -467,7 +467,7 @@ public class AnnotationPersistenceMappingSerializer
|
||||||
* Determine if the field is a lob.
|
* Determine if the field is a lob.
|
||||||
*/
|
*/
|
||||||
private boolean isLob(FieldMapping field) {
|
private boolean isLob(FieldMapping field) {
|
||||||
for (Column col : (List<Column>) field.getValueInfo().getColumns())
|
for (Column col : field.getValueInfo().getColumns())
|
||||||
if (col.getType() == Types.BLOB || col.getType() == Types.CLOB)
|
if (col.getType() == Types.BLOB || col.getType() == Types.CLOB)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
|
@ -479,14 +479,17 @@ public class AnnotationPersistenceMappingSerializer
|
||||||
private TemporalType getTemporal(FieldMapping field) {
|
private TemporalType getTemporal(FieldMapping field) {
|
||||||
if (field.getDeclaredTypeCode() != JavaTypes.DATE
|
if (field.getDeclaredTypeCode() != JavaTypes.DATE
|
||||||
&& field.getDeclaredTypeCode() != JavaTypes.CALENDAR)
|
&& field.getDeclaredTypeCode() != JavaTypes.CALENDAR)
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
DBDictionary dict = ((JDBCConfiguration) getConfiguration())
|
DBDictionary dict = ((JDBCConfiguration) getConfiguration())
|
||||||
.getDBDictionaryInstance();
|
.getDBDictionaryInstance();
|
||||||
int def = dict.getJDBCType(field.getTypeCode(), false);
|
int def = dict.getJDBCType(field.getTypeCode(), false);
|
||||||
for (Column col : (List<Column>) field.getValueInfo().getColumns()) {
|
for (Column col : field.getValueInfo().getColumns()) {
|
||||||
if (col.getType() == def)
|
if (col.getType() == def) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
switch (col.getType()) {
|
switch (col.getType()) {
|
||||||
case Types.DATE:
|
case Types.DATE:
|
||||||
return TemporalType.DATE;
|
return TemporalType.DATE;
|
||||||
|
@ -503,10 +506,14 @@ public class AnnotationPersistenceMappingSerializer
|
||||||
* Return enum type for the field.
|
* Return enum type for the field.
|
||||||
*/
|
*/
|
||||||
protected EnumType getEnumType(FieldMapping field) {
|
protected EnumType getEnumType(FieldMapping field) {
|
||||||
if (field.getDeclaredTypeCode() != JavaTypes.OBJECT)
|
if (field.getDeclaredTypeCode() != JavaTypes.OBJECT
|
||||||
|
&& field.getDeclaredTypeCode() != JavaTypes.ENUM)
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
if (!(field.getHandler() instanceof EnumValueHandler))
|
}
|
||||||
|
if (!(field.getHandler() instanceof EnumValueHandler)) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
return ((EnumValueHandler) field.getHandler()).getStoreOrdinal()
|
return ((EnumValueHandler) field.getHandler()).getStoreOrdinal()
|
||||||
? EnumType.ORDINAL : EnumType.STRING;
|
? EnumType.ORDINAL : EnumType.STRING;
|
||||||
}
|
}
|
||||||
|
@ -516,24 +523,27 @@ public class AnnotationPersistenceMappingSerializer
|
||||||
*/
|
*/
|
||||||
private void serializeColumns(MappingInfo info, ColType type,
|
private void serializeColumns(MappingInfo info, ColType type,
|
||||||
String secondary, AnnotationBuilder ab, Object meta) {
|
String secondary, AnnotationBuilder ab, Object meta) {
|
||||||
List<Column> cols = (List<Column>) info.getColumns();
|
List<Column> cols = info.getColumns();
|
||||||
if (cols == null)
|
if (cols == null) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
AnnotationBuilder abContainer = ab;
|
AnnotationBuilder abContainer = ab;
|
||||||
if (cols.size() > 1) {
|
if (cols.size() > 1) {
|
||||||
Class grpType = type.getColumnGroupAnnotationType();
|
Class<? extends Annotation> grpType = type.getColumnGroupAnnotationType();
|
||||||
if (null != grpType) {
|
if (null != grpType) {
|
||||||
AnnotationBuilder abGrp = newAnnotationBuilder(grpType);
|
AnnotationBuilder abGrp = newAnnotationBuilder(grpType);
|
||||||
if (null == ab)
|
if (null == ab) {
|
||||||
addAnnotation(abGrp, meta);
|
addAnnotation(abGrp, meta);
|
||||||
else
|
} else {
|
||||||
ab.add(null, abGrp);
|
ab.add(null, abGrp);
|
||||||
|
}
|
||||||
abContainer = abGrp;
|
abContainer = abGrp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Column col : cols)
|
for (Column col : cols) {
|
||||||
serializeColumn(col, type, secondary,
|
serializeColumn(col, type, secondary,
|
||||||
info.getUnique() != null, abContainer, meta);
|
info.getUnique() != null, abContainer, meta);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -637,7 +647,7 @@ public class AnnotationPersistenceMappingSerializer
|
||||||
*/
|
*/
|
||||||
private List<QueryResultMapping> getQueryResultMappings(ClassMetaData cm) {
|
private List<QueryResultMapping> getQueryResultMappings(ClassMetaData cm) {
|
||||||
if (_results == null || _results.isEmpty())
|
if (_results == null || _results.isEmpty())
|
||||||
return (List<QueryResultMapping>) Collections.EMPTY_LIST;
|
return Collections.EMPTY_LIST;
|
||||||
|
|
||||||
List<QueryResultMapping> result = null;
|
List<QueryResultMapping> result = null;
|
||||||
for (int i = 0; i < _results.size(); i++) {
|
for (int i = 0; i < _results.size(); i++) {
|
||||||
|
@ -779,7 +789,7 @@ public class AnnotationPersistenceMappingSerializer
|
||||||
protected class MappingSerializationComparator
|
protected class MappingSerializationComparator
|
||||||
extends SerializationComparator {
|
extends SerializationComparator {
|
||||||
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* Copyright © NORD/LB Norddeutsche Landesbank Girozentrale, Hannover - Alle Rechte vorbehalten -
|
||||||
|
*/
|
||||||
|
package org.apache.openjpa.jira2780;
|
||||||
|
|
||||||
|
public enum Jira2780Enum {
|
||||||
|
A, B, C;
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* Copyright © NORD/LB Norddeutsche Landesbank Girozentrale, Hannover - Alle Rechte vorbehalten -
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* 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.jira2780;
|
||||||
|
|
||||||
|
import org.apache.openjpa.jdbc.meta.FieldMapping;
|
||||||
|
import org.apache.openjpa.jdbc.meta.PropertiesReverseCustomizer;
|
||||||
|
import org.apache.openjpa.jdbc.meta.strats.EnumValueHandler;
|
||||||
|
|
||||||
|
public class Jira2780ReverseCustomizer extends PropertiesReverseCustomizer {
|
||||||
|
@Override
|
||||||
|
public void customize(FieldMapping field) {
|
||||||
|
super.customize(field);
|
||||||
|
if (field.getDeclaredType().isEnum()) {
|
||||||
|
EnumValueHandler enumValueHandler = new EnumValueHandler();
|
||||||
|
enumValueHandler.setStoreOrdinal(false);
|
||||||
|
field.setHandler(enumValueHandler);
|
||||||
|
// As a work-around for the error, we can set the type code to
|
||||||
|
// OBJECT to generate the @Enumerated annotation.
|
||||||
|
// field.setDeclaredTypeCode(JavaTypes.OBJECT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,113 @@
|
||||||
|
/*
|
||||||
|
* 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.jira2780;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.Query;
|
||||||
|
|
||||||
|
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
|
||||||
|
import org.apache.openjpa.jdbc.meta.ReverseMappingTool;
|
||||||
|
import org.apache.openjpa.lib.util.Files;
|
||||||
|
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests the added useSchemaElement functionality of the
|
||||||
|
* ReverseMappingTool and CodeGenerator classes.
|
||||||
|
*
|
||||||
|
* @author Austin Dorenkamp (ajdorenk)
|
||||||
|
*/
|
||||||
|
public class TestJira2780ReverseCustomizer extends SingleEMFTestCase {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
File f = new File("target/orm.xml");
|
||||||
|
|
||||||
|
// Make sure to clean up orm.xml from a prior run
|
||||||
|
if (f.exists()) {
|
||||||
|
assertTrue(f.delete());
|
||||||
|
}
|
||||||
|
setSupportedDatabases(org.apache.openjpa.jdbc.sql.DerbyDictionary.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPersistenceUnitName(){
|
||||||
|
return "rev-mapping-jira2780-pu";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGettersAndSetters() throws Exception {
|
||||||
|
|
||||||
|
JDBCConfiguration conf = (JDBCConfiguration) emf.getConfiguration();
|
||||||
|
|
||||||
|
EntityManager em = emf.createEntityManager();
|
||||||
|
|
||||||
|
em.getTransaction().begin();
|
||||||
|
|
||||||
|
Query q = em.createNativeQuery("CREATE TABLE JIRA2780.ABC (ID INTEGER PRIMARY KEY, TEST_ENUM VARCHAR(1))");
|
||||||
|
try {
|
||||||
|
q.executeUpdate();
|
||||||
|
em.getTransaction().commit();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
em.getTransaction().rollback();
|
||||||
|
System.out.println(t.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
final String clsName = "Abc";
|
||||||
|
ReverseMappingTool.Flags flags = new ReverseMappingTool.Flags();
|
||||||
|
flags.metaDataLevel = "none";
|
||||||
|
flags.generateAnnotations = true;
|
||||||
|
flags.packageName = getClass().getPackage().getName();
|
||||||
|
flags.directory = Files.getFile("./target", null);
|
||||||
|
flags.customizer = new Jira2780ReverseCustomizer();
|
||||||
|
Properties customProps = new Properties();
|
||||||
|
customProps.put(flags.packageName + "." + clsName + ".testEnum.type"
|
||||||
|
, Jira2780Enum.class.getName());
|
||||||
|
flags.customizer.setConfiguration(customProps);
|
||||||
|
ReverseMappingTool.run(conf, new String[0], flags, null);
|
||||||
|
|
||||||
|
/* Now that the tool has been run, we will test it by reading the generated files */
|
||||||
|
File abc = new File(Files.getPackageFile(flags.directory, flags.packageName, false)
|
||||||
|
, clsName + ".java");
|
||||||
|
String currLine = null, prevLine;
|
||||||
|
try (Scanner inFile = new Scanner(abc)) {
|
||||||
|
while (inFile.hasNextLine()) {
|
||||||
|
prevLine = currLine;
|
||||||
|
currLine = inFile.nextLine();
|
||||||
|
if (currLine.isEmpty() || !currLine.contains("Jira2780Enum testEnum")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (prevLine.contains("@Enumerated(EnumType.STRING)")) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
fail("@Enumerated annotation has not been injected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
fail(clsName + ".java not generated under ./target by ReverseMappingTool");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete file to clean up workspace
|
||||||
|
assertTrue(abc.delete());
|
||||||
|
}
|
||||||
|
}
|
|
@ -487,6 +487,11 @@
|
||||||
<property name="openjpa.jdbc.Schemas" value="USCHEMA.USCHANTBL"/>
|
<property name="openjpa.jdbc.Schemas" value="USCHEMA.USCHANTBL"/>
|
||||||
</properties>
|
</properties>
|
||||||
</persistence-unit>
|
</persistence-unit>
|
||||||
|
<persistence-unit name="rev-mapping-jira2780-pu">
|
||||||
|
<properties>
|
||||||
|
<property name="openjpa.jdbc.Schemas" value="JIRA2780.ABC"/>
|
||||||
|
</properties>
|
||||||
|
</persistence-unit>
|
||||||
|
|
||||||
<persistence-unit name="puDefault" transaction-type="RESOURCE_LOCAL">
|
<persistence-unit name="puDefault" transaction-type="RESOURCE_LOCAL">
|
||||||
<mapping-file>META-INF/pudefaults-orm.xml</mapping-file>
|
<mapping-file>META-INF/pudefaults-orm.xml</mapping-file>
|
||||||
|
|
Loading…
Reference in New Issue