Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Converter is not called on load operation
Converter is not called on load operation [message #903046] Tue, 21 August 2012 13:53 Go to next message
Lars Drießnack is currently offline Lars Drießnack
Messages: 9
Registered: August 2012
Junior Member
Hi,

I'm trying to generate a value of an entity while persisting it (insertedAt Date). The converter works fine and generates the value but when I'm loading the entity the value of insertedAt is allways null. On database the value exists.

I debugged everything and it just seems that the converter is not called while the process of loading something from the database. The whole field is not loaded.

Entity:
@MappedSuperclass
public abstract class BaseObject implements Serializable {

private static final long serialVersionUID = -1387727537841488003L;

...

@Column(nullable = false)
@Converter(name = "insertedAtConverter", converterClass =           ....InsertedAtConverter.class)
@Convert("insertedAtConverter")
private DateTime insertedAt;

...


Converter:
public class InsertedAtConverter implements Converter {

private static Logger logger = Logger.getLogger(InsertedAtConverter.class);

private static final long serialVersionUID = -5174445313301568350L;

@Override
public Object convertObjectValueToDataValue(Object objectValue, Session  arg1) {
logger.debug("InsertedAtConverter called.");

if (objectValue == null) {
	return DateTime.now().toString();
} else if (objectValue instanceof DateTime) {
	return ((DateTime) objectValue).toString();
} else
	throw new IllegalStateException("Converstion exception, value is not of org.joda.time.DateTime type or null.");
}

@Override
public Object convertDataValueToObjectValue(Object dataValue, Session arg1) {
logger.debug("DateTimeConverter called.");

if (dataValue instanceof String)
     return DateTime.parse((String) dataValue);
else
 throw new IllegalStateException("Converstion exception, value is not of String type.");
}

@Override
public void initialize(DatabaseMapping arg0, Session arg1) {

}

@Override
public boolean isMutable() {
	return false;
}
}


Any suggestions?

kind regars,
Lars
Re: Converter is not called on load operation [message #905010 is a reply to message #903046] Wed, 29 August 2012 10:52 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

This is not what converters are for, you are not converting a value, you are initializing it. Instead initialize it in your model/application or using a PrePersist event.


James : Wiki : Book : Blog : Twitter
Previous Topic:Dynamic EclipseLink JPA Clarification
Next Topic:JPA-RS hooks and/or HATEOAS?
Goto Forum:
  


Current Time: Tue Jul 29 21:14:07 EDT 2014

Powered by FUDForum. Page generated in 0.01543 seconds