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) {
|
||||
List<Column> cols = null;
|
||||
if (secondaryInfo != null)
|
||||
cols = (List<Column>) secondaryInfo.getSecondaryTableJoinColumns
|
||||
cols = secondaryInfo.getSecondaryTableJoinColumns
|
||||
(table);
|
||||
|
||||
boolean print = (cols != null && cols.size() > 0) ||
|
||||
|
@ -322,11 +322,11 @@ public class AnnotationPersistenceMappingSerializer
|
|||
return true;
|
||||
|
||||
ValueMappingInfo info = field.getValueInfo();
|
||||
List<Column> cols = (List<Column>) info.getColumns();
|
||||
List<Column> cols = info.getColumns();
|
||||
if (cols == null || cols.size() == 0)
|
||||
return false;
|
||||
ValueMappingInfo info2 = field2.getValueInfo();
|
||||
List<Column> cols2 = (List<Column>) info2.getColumns();
|
||||
List<Column> cols2 = info2.getColumns();
|
||||
if (cols2 == null || cols2.size() != cols.size())
|
||||
return true;
|
||||
if (cols.size() != 1)
|
||||
|
@ -467,7 +467,7 @@ public class AnnotationPersistenceMappingSerializer
|
|||
* Determine if the field is a lob.
|
||||
*/
|
||||
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)
|
||||
return true;
|
||||
return false;
|
||||
|
@ -479,14 +479,17 @@ public class AnnotationPersistenceMappingSerializer
|
|||
private TemporalType getTemporal(FieldMapping field) {
|
||||
if (field.getDeclaredTypeCode() != JavaTypes.DATE
|
||||
&& field.getDeclaredTypeCode() != JavaTypes.CALENDAR)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
DBDictionary dict = ((JDBCConfiguration) getConfiguration())
|
||||
.getDBDictionaryInstance();
|
||||
int def = dict.getJDBCType(field.getTypeCode(), false);
|
||||
for (Column col : (List<Column>) field.getValueInfo().getColumns()) {
|
||||
if (col.getType() == def)
|
||||
for (Column col : field.getValueInfo().getColumns()) {
|
||||
if (col.getType() == def) {
|
||||
continue;
|
||||
}
|
||||
switch (col.getType()) {
|
||||
case Types.DATE:
|
||||
return TemporalType.DATE;
|
||||
|
@ -503,10 +506,14 @@ public class AnnotationPersistenceMappingSerializer
|
|||
* Return enum type for the field.
|
||||
*/
|
||||
protected EnumType getEnumType(FieldMapping field) {
|
||||
if (field.getDeclaredTypeCode() != JavaTypes.OBJECT)
|
||||
if (field.getDeclaredTypeCode() != JavaTypes.OBJECT
|
||||
&& field.getDeclaredTypeCode() != JavaTypes.ENUM)
|
||||
{
|
||||
return null;
|
||||
if (!(field.getHandler() instanceof EnumValueHandler))
|
||||
}
|
||||
if (!(field.getHandler() instanceof EnumValueHandler)) {
|
||||
return null;
|
||||
}
|
||||
return ((EnumValueHandler) field.getHandler()).getStoreOrdinal()
|
||||
? EnumType.ORDINAL : EnumType.STRING;
|
||||
}
|
||||
|
@ -516,24 +523,27 @@ public class AnnotationPersistenceMappingSerializer
|
|||
*/
|
||||
private void serializeColumns(MappingInfo info, ColType type,
|
||||
String secondary, AnnotationBuilder ab, Object meta) {
|
||||
List<Column> cols = (List<Column>) info.getColumns();
|
||||
if (cols == null)
|
||||
List<Column> cols = info.getColumns();
|
||||
if (cols == null) {
|
||||
return;
|
||||
}
|
||||
AnnotationBuilder abContainer = ab;
|
||||
if (cols.size() > 1) {
|
||||
Class grpType = type.getColumnGroupAnnotationType();
|
||||
Class<? extends Annotation> grpType = type.getColumnGroupAnnotationType();
|
||||
if (null != grpType) {
|
||||
AnnotationBuilder abGrp = newAnnotationBuilder(grpType);
|
||||
if (null == ab)
|
||||
if (null == ab) {
|
||||
addAnnotation(abGrp, meta);
|
||||
else
|
||||
} else {
|
||||
ab.add(null, abGrp);
|
||||
}
|
||||
abContainer = abGrp;
|
||||
}
|
||||
}
|
||||
for (Column col : cols)
|
||||
for (Column col : cols) {
|
||||
serializeColumn(col, type, secondary,
|
||||
info.getUnique() != null, abContainer, meta);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -637,7 +647,7 @@ public class AnnotationPersistenceMappingSerializer
|
|||
*/
|
||||
private List<QueryResultMapping> getQueryResultMappings(ClassMetaData cm) {
|
||||
if (_results == null || _results.isEmpty())
|
||||
return (List<QueryResultMapping>) Collections.EMPTY_LIST;
|
||||
return Collections.EMPTY_LIST;
|
||||
|
||||
List<QueryResultMapping> result = null;
|
||||
for (int i = 0; i < _results.size(); i++) {
|
||||
|
@ -779,7 +789,7 @@ public class AnnotationPersistenceMappingSerializer
|
|||
protected class MappingSerializationComparator
|
||||
extends SerializationComparator {
|
||||
|
||||
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@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"/>
|
||||
</properties>
|
||||
</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">
|
||||
<mapping-file>META-INF/pudefaults-orm.xml</mapping-file>
|
||||
|
|
Loading…
Reference in New Issue