Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Enum support TopLink 10.1.3

476702Jun 2 2006 — edited Jun 27 2008
I am trying to figure out the level of support for Java 1.5 enums in TopLink 10.1.3.0.0, using the regular TopLink API. The only reference I have found on this forum has been regarding the EJB/JPA APIs, and said that full support for enums was still forthcoming.

All I'm interested in doing is persisting an enum field to the database and having the meaningful enum name stored rather than some random ID. Is this feature available, or might it be in a near release?

Thanks,
Patrick Vinograd

Comments

Shaun Smith-Oracle
Hi Patrick,

We don't have specific support for Java 5 enums in 10.1.3.0.0. One way to handle them is with a transformation mapping. You can use the Enum api to convert to/from the text name of the enum to an instance and back again pretty easily and don't have to hard wire any values. Below's an example of the classes you can use to complete a transformation mapping for an enum BookCategory type which is an attribute of a Book.

--Shaun

The Enum class:
public enum BookCategory {MYSTERY, SCIENCE_FICTION, BIOGRAPHY}
-------------------------------
The Field Transformer for the CATEGORY field:
public class BookCategoryFieldTransformer extends FieldTransformerAdapter {

	@Override
	public Object buildFieldValue(Object object, String fieldName, Session session) {
		Book book = (Book)object;
		BookCategory category = book.getCategory();
		if (category !=  null) {
			return category.name();
		} else {
			return null;
		}
	}

}
-----------------------------

The category AttributeTransformer:
// the database field CATEGORY contains a string obtained from an Enum using the name() method
public class BookCategoryAttributeTransformer extends AttributeTransformerAdapter {

	@Override
	public Object buildAttributeValue(Record record, Object object, Session session) {
		return BookCategory.valueOf((String)record.get("CATEGORY"));
	}
}
476702
Shaun,

Thank you, that was exactly what I was looking for.

-Patrick
Shaun Smith-Oracle
Hi Patrick,

One more thing. If you're using the TopLink Workbench be sure to run it on JDK 5 or you'll get errors trying to load your Java 5 compiled classes in to a pre-5 VM. You can set this up by setting the JAVA_HOME variable in the setenv.cmd/sh script in the toplink/bin folder of your TopLink installation directory.

Shaun
476702
In general this transformation mechanism is working fine. However, I have a class that has two fields that are of the same enum type. Each field maps to its own column in the database.

The problem is that I don't see a way of defining an AttributeTransformer for this attribute, because it is ambiguous which of the fields is being mapped in a given call to buildAttributeValue. I.e. which column name do I look at to get the data for generating the enum. Any ideas?

Thanks,
Patrick
625894
I use oracle.toplink.mappings.converters.Converter in our project. It works the same as TransformMapping and can meet your need.


First you create an implementation of Converter with exactly the same code in a TransformMapping

public class EnumConverter implements Converter {

public Object convertDataValueToObjectValue(Object arg0, Session arg1) {
....
}

public Object convertObjectValueToDataValue(Object arg0, Session arg1) {
.....
}

........
}

Then use DirectToFieldMapping and set the converter to the Mapping.

mapping.setConverter(new EnumConverter());



Regards,
Harper
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 25 2008
Added on Jun 2 2006
5 comments
1,734 views