Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Help with Converter API

The converter is only given the value, not the source object.  If you really
need the object you can use a TransformationMapping, or translate the value
in property access get/set methods.

You could also instead use an event for debugging.



zaque wrote:
> 
> Hi,
> 
> I am working with an older database that has very little in the way of  
> constraints.   Sadly, just about anything can be null.  So far, the  
> powers that be are very reluctant to make any changes to the schema or  
> the column constraints and so as a consequence I'm looking at ways to  
> work around some of these issues as "elegantly" as possible.
> 
> We have some columns which represent Booleans (0 or 1) but many of  
> these are often left NULL.  I wrote a little converter that will take  
> 'NULL' and set the returned Boolean to FALSE and this works just fine  
> so far.  Once a entity with a converted Boolean field is persisted,  
> the value that was once NULL is now '0' and that is totally  
> acceptable.  I then decorate my Boolean fields like this and NULL  
> values are successfully converted to Boolean.FALSE:
> 
> @Convert("Global.NullToBooleanFalse") //This converter is defined in  
> my eclipselink-orm.xml
> Boolean posted;
> 
> Boolean isPosted() {
>     return this.posted;
> }
> 
> This saves me from having to do a lot of null checks and saves me from  
> having to implement life-cycle events (@PostLoad) on Entities to clean  
> up these types of issues.
> 
> What I would like to do is add the ability to log information about  
> the Entity the converted field was a member of.  That way when  
> debugging any strange behavior in the Db I could at least have a  
> record of which entities were affected by the converters.   Can  
> anybody shed some light on the Converter API and wether or not I can  
> retrieve information from the 'parent' entity for the field I am   
> converting?   Via the 'Session' or the 'DatabaseMapping'?   Is it even  
> possible with this API - perhaps another route in EclipseLink is  
> required?
> 
> Or perhaps there are better options for defining default values for  
> NULL's coming out of the DB?
> 
> See converter source inline below if you're interested.
> 
> Thanks in advance!
> 
> -zach
> 
> ----
> 
> 	public Object convertDataValueToObjectValue(Object data, Session  
> session) {
> 		if (data == null) {
> 			logger.fine("DB->JAVA Converting NULL to Boolean.FALSE");
> 			return Boolean.FALSE;
> 		}
> 		if (data instanceof Number) {
> 			int dataValue = ((Number)data).intValue();
> 			switch (dataValue) {
> 				case 0: return Boolean.FALSE;
> 				case 1: return Boolean.TRUE;
> 				default:
> 					throw new IllegalArgumentException("Found java.lang.Number value  
> that is not 0 or 1: [data = " + data + "]");
> 			}
> 		}
> 		
> 		throw new IllegalArgumentException("Non-java.lang.Number value : ["  
> + data.getClass() + "] " + data);
> 	}
> 
> 	public Object convertObjectValueToDataValue(Object value, Session  
> session) {
> 		if (value instanceof Boolean) {
> 			return value == Boolean.FALSE ? new Integer(0) : new Integer(1);
> 		}
> 		else if (value == null) {
> 			logger.fine("JAVA->DB Converting NULL to Integer(0)");
> 			return new Integer(0);
> 		}
> 		
> 		throw new IllegalArgumentException("Boolean expeted.  Received : ["  
> + value.getClass() + "] " + value);
> 	}
> 
> 	public void initialize(DatabaseMapping dbMap, Session session) {
> 	// not using any DatabaseMapping
> 	}
> 
> 	public boolean isMutable() {
> 		return false;
> 	}
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Help-with-Converter-API-tp19977736p19994436.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top