diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Discriminator.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Discriminator.java index b5f4612b8..5a41b30ff 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Discriminator.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/Discriminator.java @@ -33,6 +33,7 @@ import org.apache.openjpa.jdbc.sql.Select; import org.apache.openjpa.kernel.OpenJPAStateManager; import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.lib.util.Localizer; +import org.apache.openjpa.meta.JavaTypes; import org.apache.openjpa.meta.MetaDataContext; import org.apache.openjpa.meta.MetaDataModes; import org.apache.openjpa.meta.MetaDataRepository; @@ -64,6 +65,8 @@ public class Discriminator private Index _idx = null; private boolean _subsLoaded = false; private Object _value = null; + + private int _javaType = -1; /** * Constructor. Supply owning mapping. @@ -413,4 +416,21 @@ public class Discriminator public String toString() { return _mapping + ""; } + + public void setJavaType(int javaType) { + _javaType = javaType; + } + + public int getJavaType() { + if (_javaType == -1) { + ClassMapping superMapping = _mapping.getPCSuperclassMapping(); + + if (superMapping != null && + superMapping.getDiscriminator() != null) { + _javaType = superMapping.getDiscriminator().getJavaType(); + } + } + + return _javaType; + } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/DiscriminatorMappingInfo.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/DiscriminatorMappingInfo.java index be2444329..86ba6c5ea 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/DiscriminatorMappingInfo.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/DiscriminatorMappingInfo.java @@ -20,6 +20,7 @@ package org.apache.openjpa.jdbc.meta; import java.lang.reflect.Modifier; +import org.apache.commons.lang.StringUtils; import org.apache.openjpa.jdbc.meta.strats.NoneDiscriminatorStrategy; import org.apache.openjpa.jdbc.meta.strats.SuperclassDiscriminatorStrategy; import org.apache.openjpa.jdbc.meta.strats.ValueMapDiscriminatorStrategy; @@ -28,6 +29,7 @@ import org.apache.openjpa.jdbc.schema.Index; import org.apache.openjpa.jdbc.schema.SchemaGroup; import org.apache.openjpa.jdbc.schema.Table; import org.apache.openjpa.lib.util.Localizer; +import org.apache.openjpa.meta.JavaTypes; import org.apache.openjpa.util.MetaDataException; /** @@ -45,7 +47,7 @@ public class DiscriminatorMappingInfo (DiscriminatorMappingInfo.class); private String _value = null; - + /** * Raw discriminator value string. */ @@ -66,28 +68,20 @@ public class DiscriminatorMappingInfo public Object getValue(Discriminator discrim, boolean adapt) { if (discrim.getValue() != null) return discrim.getValue(); - if (_value == null) + if (StringUtils.isEmpty(_value)) { return discrim.getMappingRepository().getMappingDefaults(). getDiscriminatorValue(discrim, adapt); - - if (_value.length() > 0 && (_value.charAt(0) == '-' - || Character.isDigit(_value.charAt(0)))) { - try { - if (_value.indexOf('.') == -1) - return new Integer(_value); - return new Double(_value); - } catch (RuntimeException re) { - throw new MetaDataException(_loc.get("bad-discrim-value", - discrim.getClassMapping(), _value)); - } } - if ("null".equalsIgnoreCase(_value)) - return Discriminator.NULL; - - // strip quotes - if (_value.length() > 0 && _value.charAt(0) == '\'') - return _value.substring(1, _value.length() - 1); - return _value; + + switch(discrim.getJavaType()) { + case JavaTypes.INT: + return Integer.valueOf(_value); + case JavaTypes.CHAR: + return Character.valueOf(_value.charAt(_value.indexOf('\'')+1)); + case JavaTypes.STRING: + default: + return _value; + } } /** diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingDefaultsImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingDefaultsImpl.java index ea29a0c34..decd977b2 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingDefaultsImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingDefaultsImpl.java @@ -455,14 +455,47 @@ public class MappingDefaultsImpl return null; } + /** + * Provides a default value for the given Discriminator. + * + *

+ * The type of the object returned relies on the javaType field being set on + * the Discriminator which is provided. + * + * + * + * + * + * + *
JavaType + * Default value + *
{@link JavaTypes.INT} The hashcode of the entity name
{@link JavaTypes.CHAR}The first character of the entity name + *
{@link JavaTypes.STRING}The entity name
+ * + * @param disc The discriminator that needs a default value + * @param adapt + * + * @return A new object containing the generated Discriminator value. + */ public Object getDiscriminatorValue(Discriminator disc, boolean adapt) { if (!adapt && !defaultMissingInfo()) return null; // WARNING: CHANGING THIS WILL INVALIDATE EXISTING DATA IF DEFAULTING // MISSING MAPPING INFO - return Strings.getClassName(disc.getClassMapping(). - getDescribedType()); + + String alias = Strings.getClassName(disc.getClassMapping() + .getTypeAlias()); + + switch (disc.getJavaType()) { + case JavaTypes.INT: + return new Integer(alias.hashCode()); + case JavaTypes.CHAR: + return new Character(alias.charAt(0)); + case JavaTypes.STRING: + default: + return alias; + } } public String getTableName(ClassMapping cls, Schema schema) { diff --git a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java index 6a7eef168..850337059 100644 --- a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java +++ b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/AnnotationPersistenceMappingParser.java @@ -20,6 +20,7 @@ package org.apache.openjpa.persistence.jdbc; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Modifier; import java.sql.Types; import java.util.ArrayList; import java.util.Arrays; @@ -57,6 +58,7 @@ import org.apache.openjpa.jdbc.conf.JDBCConfiguration; import org.apache.openjpa.jdbc.kernel.EagerFetchModes; import org.apache.openjpa.jdbc.meta.ClassMapping; import org.apache.openjpa.jdbc.meta.ClassMappingInfo; +import org.apache.openjpa.jdbc.meta.Discriminator; import org.apache.openjpa.jdbc.meta.FieldMapping; import org.apache.openjpa.jdbc.meta.MappingInfo; import org.apache.openjpa.jdbc.meta.MappingRepository; @@ -262,6 +264,12 @@ public class AnnotationPersistenceMappingParser case DISCRIM_VAL: cm.getDiscriminator().getMappingInfo().setValue (((DiscriminatorValue) anno).value()); + if (Modifier.isAbstract(cm.getDescribedType(). + getModifiers()) && getLog().isInfoEnabled()) { + getLog().info( + _loc.get("discriminator-on-abstract-class", cm + .getDescribedType().getName())); + } break; case INHERITANCE: parseInheritance(cm, (Inheritance) anno); @@ -525,18 +533,22 @@ public class AnnotationPersistenceMappingParser col.setName(dcol.name()); if (!StringUtils.isEmpty(dcol.columnDefinition())) col.setTypeName(dcol.columnDefinition()); + Discriminator discrim = cm.getDiscriminator(); switch (dcol.discriminatorType()) { case CHAR: col.setJavaType(JavaTypes.CHAR); + discrim.setJavaType(JavaTypes.CHAR); break; case INTEGER: col.setJavaType(JavaTypes.INT); if (dcol.length() != 31) col.setSize(dcol.length()); + discrim.setJavaType(JavaTypes.INT); break; default: col.setJavaType(JavaTypes.STRING); col.setSize(dcol.length()); + discrim.setJavaType(JavaTypes.STRING); } cm.getDiscriminator().getMappingInfo().setColumns (Arrays.asList(new Column[]{ col })); diff --git a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java index 7ffc3b40b..6b8c132ce 100644 --- a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java +++ b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/XMLPersistenceMappingParser.java @@ -18,6 +18,7 @@ */ package org.apache.openjpa.persistence.jdbc; +import java.lang.reflect.Modifier; import java.sql.Types; import java.util.ArrayList; import java.util.Arrays; @@ -197,6 +198,7 @@ public class XMLPersistenceMappingParser ret = startSecondaryTable(attrs); break; case DISCRIM_COL: + parseDiscriminatorColumn(attrs); _discCol = parseColumn(attrs); ret = true; break; @@ -314,20 +316,21 @@ public class XMLPersistenceMappingParser cm.getMappingInfo().setColumns(_supJoinCols); if (_discCol != null) { - DiscriminatorMappingInfo dinfo = cm.getDiscriminator(). - getMappingInfo(); - if (_discType != null) { - switch (_discType) { - case CHAR: - _discCol.setJavaType(JavaTypes.CHAR); - break; - case INTEGER: - _discCol.setJavaType(JavaTypes.INT); - break; - default: - _discCol.setJavaType(JavaTypes.STRING); - break; - } + DiscriminatorMappingInfo dinfo = cm.getDiscriminator() + .getMappingInfo(); + switch (_discType) { + case CHAR: + _discCol.setJavaType(JavaTypes.CHAR); + cm.getDiscriminator().setJavaType(JavaTypes.CHAR); + break; + case INTEGER: + _discCol.setJavaType(JavaTypes.INT); + cm.getDiscriminator().setJavaType(JavaTypes.INT); + break; + default: + _discCol.setJavaType(JavaTypes.STRING); + cm.getDiscriminator().setJavaType(JavaTypes.STRING); + break; } dinfo.setColumns(Arrays.asList(new Column[]{ _discCol })); } @@ -439,6 +442,13 @@ public class XMLPersistenceMappingParser ClassMapping cm = (ClassMapping) currentElement(); cm.getDiscriminator().getMappingInfo().setValue(val); + + if (Modifier.isAbstract(cm.getDescribedType().getModifiers()) + && getLog().isInfoEnabled()) { + getLog().info( + _loc.get("discriminator-on-abstract-class", cm + .getDescribedType().getName())); + } } /** @@ -916,4 +926,12 @@ public class XMLPersistenceMappingParser TRUE, FALSE } + + private void parseDiscriminatorColumn(Attributes attrs) { + String val = attrs.getValue("discriminator-type"); + if (val != null) { + _discType = Enum.valueOf(DiscriminatorType.class, val); + } + + } } diff --git a/openjpa-persistence-jdbc/src/main/resources/org/apache/openjpa/persistence/jdbc/localizer.properties b/openjpa-persistence-jdbc/src/main/resources/org/apache/openjpa/persistence/jdbc/localizer.properties index 88b356ad7..fbf606b21 100644 --- a/openjpa-persistence-jdbc/src/main/resources/org/apache/openjpa/persistence/jdbc/localizer.properties +++ b/openjpa-persistence-jdbc/src/main/resources/org/apache/openjpa/persistence/jdbc/localizer.properties @@ -48,3 +48,7 @@ not-embedded: Attempt to declare mapping overrides on non-embedded field "{0}". no-gen-table: No generated table found at "{0}". empty-unique-column: A unique constraint "{0}" specified in mapping of class \ "{1}" includes an empty column. +discriminator-on-abstract-class: A discriminator value has been specified for \ + the abstract class "{0}". The discriminator will never be used and may be \ + safely removed. + \ No newline at end of file diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/CharAbstractEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/CharAbstractEntity.java new file mode 100644 index 000000000..5dd5ff754 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/CharAbstractEntity.java @@ -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.discriminator; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +@Entity +@Inheritance(strategy=InheritanceType.SINGLE_TABLE) +@DiscriminatorColumn(name="DISC_COL", discriminatorType=DiscriminatorType.CHAR) +public abstract class CharAbstractEntity { + + @Id + @GeneratedValue + private long id; + + @Column(length=16) + private String name; + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/CharLeafEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/CharLeafEntity.java new file mode 100644 index 000000000..163ef3f6d --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/CharLeafEntity.java @@ -0,0 +1,36 @@ +/* + * 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.discriminator; + +import javax.persistence.Entity; + +@Entity(name="chrLeaf") +public class CharLeafEntity + extends CharRootEntity { + + private int subInt; + + public int getSubInt() { + return subInt; + } + + public void setSubInt(int subInt) { + this.subInt = subInt; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/CharRootEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/CharRootEntity.java new file mode 100644 index 000000000..a179a116a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/CharRootEntity.java @@ -0,0 +1,27 @@ +/* + * 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.discriminator; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity +@DiscriminatorValue("R") +public class CharRootEntity extends CharAbstractEntity { +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/IntegerAbstractEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/IntegerAbstractEntity.java new file mode 100644 index 000000000..3b866f530 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/IntegerAbstractEntity.java @@ -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.discriminator; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +@Entity +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +@DiscriminatorColumn(name = "DISC_COL", + discriminatorType = DiscriminatorType.INTEGER) +public abstract class IntegerAbstractEntity { + + @Id + @GeneratedValue + private long id; + + @Column(length=16) + private String name; + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/IntegerLeafEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/IntegerLeafEntity.java new file mode 100644 index 000000000..f88ef7037 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/IntegerLeafEntity.java @@ -0,0 +1,36 @@ +/* + * 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.discriminator; + +import javax.persistence.Entity; + +@Entity(name="intLeaf") +public class IntegerLeafEntity + extends IntegerRootEntity { + + private int subInt; + + public int getSubInt() { + return subInt; + } + + public void setSubInt(int subInt) { + this.subInt = subInt; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/IntegerRootEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/IntegerRootEntity.java new file mode 100644 index 000000000..2d3a58d7a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/IntegerRootEntity.java @@ -0,0 +1,28 @@ +/* + * 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.discriminator; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity +@DiscriminatorValue("10101") +public class IntegerRootEntity extends IntegerAbstractEntity { + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/StringAbstractEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/StringAbstractEntity.java new file mode 100644 index 000000000..b4d20f105 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/StringAbstractEntity.java @@ -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.discriminator; + +import javax.persistence.Column; +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +@Entity +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +@DiscriminatorColumn(name = "DISC_COL", + discriminatorType = DiscriminatorType.STRING) +public abstract class StringAbstractEntity { + + @Id + @GeneratedValue + private long id; + + @Column(length = 16) + private String name; + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/StringLeafEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/StringLeafEntity.java new file mode 100644 index 000000000..e23355919 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/StringLeafEntity.java @@ -0,0 +1,36 @@ +/* + * 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.discriminator; + +import javax.persistence.Entity; + +@Entity(name="strLeaf") +public class StringLeafEntity + extends StringRootEntity { + + private int subInt; + + public int getSubInt() { + return subInt; + } + + public void setSubInt(int subInt) { + this.subInt = subInt; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/StringRootEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/StringRootEntity.java new file mode 100644 index 000000000..153f4e63a --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/StringRootEntity.java @@ -0,0 +1,27 @@ +/* + * 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.discriminator; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity +@DiscriminatorValue("StringRoot") +public class StringRootEntity extends StringAbstractEntity { +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/TestDiscriminatorTypes.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/TestDiscriminatorTypes.java new file mode 100644 index 000000000..65f407eab --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/discriminator/TestDiscriminatorTypes.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.discriminator; + +import javax.persistence.EntityManager; + +import org.apache.openjpa.jdbc.meta.ClassMapping; +import org.apache.openjpa.jdbc.meta.Discriminator; +import org.apache.openjpa.meta.JavaTypes; +import org.apache.openjpa.persistence.OpenJPAPersistence; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestDiscriminatorTypes extends SingleEMFTestCase { + + protected void setUp() { + super.setUp(CharAbstractEntity.class, CharLeafEntity.class, + CharRootEntity.class, IntegerAbstractEntity.class, + IntegerLeafEntity.class, IntegerRootEntity.class, + StringAbstractEntity.class, StringLeafEntity.class, + StringRootEntity.class); + } + + public void testCharDiscriminators() { + EntityManager em = emf.createEntityManager(); // load types + + Discriminator discrim = getMapping("CharAbstractEntity") + .getDiscriminator(); + assertEquals(new Character('C'), discrim.getValue()); // Generated + assertEquals(JavaTypes.CHAR, discrim.getJavaType()); + + discrim = getMapping("chrLeaf").getDiscriminator(); + assertEquals(new Character('c'), discrim.getValue()); + assertEquals(JavaTypes.CHAR, discrim.getJavaType()); + + discrim = getMapping("CharRootEntity").getDiscriminator(); + assertEquals(new Character('R'), discrim.getValue()); + assertEquals(JavaTypes.CHAR, discrim.getJavaType()); + + em.close(); + } + + public void testIntDiscriminators() { + EntityManager em = emf.createEntityManager(); // load the types + + Discriminator discrim = getMapping("IntegerAbstractEntity") + .getDiscriminator(); + assertEquals(new Integer("IntegerAbstractEntity".hashCode()), discrim + .getValue()); // Generated value + assertEquals(JavaTypes.INT, discrim.getJavaType()); + + discrim = getMapping("intLeaf").getDiscriminator(); + assertEquals(new Integer("intLeaf".hashCode()), discrim.getValue()); + assertEquals(JavaTypes.INT, discrim.getJavaType()); + + discrim = getMapping("IntegerRootEntity").getDiscriminator(); + assertEquals(new Integer(10101), discrim.getValue()); + assertEquals(JavaTypes.INT, discrim.getJavaType()); + + em.close(); + } + + public void testStringDiscriminators() { + EntityManager em = emf.createEntityManager(); // load the types + Discriminator discrim = getMapping("StringAbstractEntity") + .getDiscriminator(); + assertEquals("StringAbstractEntity", discrim.getValue()); // Generated + assertEquals(JavaTypes.STRING, discrim.getJavaType()); + + discrim = getMapping("strLeaf").getDiscriminator(); + assertEquals("strLeaf", discrim.getValue()); + assertEquals(JavaTypes.STRING, discrim.getJavaType()); + + discrim = getMapping("StringRootEntity").getDiscriminator(); + assertEquals("StringRoot", discrim.getValue()); + assertEquals(JavaTypes.STRING, discrim.getJavaType()); + em.close(); + } + + private ClassMapping getMapping(String name) { + return (ClassMapping) OpenJPAPersistence.cast(emf).getConfiguration() + .getMetaDataRepositoryInstance().getMetaData(name, + getClass().getClassLoader(), true); + } +}