Skip to content

Instantly share code, notes, and snippets.

@hlship
Created February 10, 2010 16:14
Show Gist options
  • Save hlship/300480 to your computer and use it in GitHub Desktop.
Save hlship/300480 to your computer and use it in GitHub Desktop.
public static void contributeValueEncoderSource(
MappedConfiguration<Class, ValueEncoderFactory> configuration,
final Session session)
{
ValueEncoder<Posting> postingValueEncoder = new ValueEncoder<Posting>()
{
@Override
public String toClient(Posting value)
{
return String.format("%s:%s", value.getBlog().getName(), value
.getExternalId());
}
@Override
public Posting toValue(String clientValue)
{
int colon = clientValue.indexOf(':');
String blogName = clientValue.substring(0, colon);
String externalId = clientValue.substring(colon + 1);
Query query = session
.createQuery("from Posting where blog.name = :blogName and externalId = :externalId");
query.setString("blogName", blogName).setString("externalId",
externalId);
return (Posting) query.uniqueResult();
}
};
configuration.override(Posting.class, GenericValueEncoderFactory.create(postingValueEncoder));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment