Skip to content

Instantly share code, notes, and snippets.

@magro
Created December 16, 2010 00:01
Show Gist options
  • Save magro/742807 to your computer and use it in GitHub Desktop.
Save magro/742807 to your computer and use it in GitHub Desktop.
A hello world that shows how to use morphia with an immutable id type
import java.net.UnknownHostException;
import org.apache.commons.lang.builder.ToStringBuilder;
import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.annotations.Entity;
import com.google.code.morphia.annotations.Id;
import com.google.code.morphia.converters.SimpleValueConverter;
import com.google.code.morphia.converters.TypeConverter;
import com.google.code.morphia.mapping.MappedField;
import com.google.code.morphia.mapping.MappingException;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
/**
* Hello morphia, can we use immutable id user types? Yes, we can.
*/
public class HelloMorphia {
public static void main( final String[] args ) throws UnknownHostException, MongoException {
final Morphia morphia = new Morphia();
final Datastore ds = morphia.createDatastore( new Mongo( "localhost" ), "myDB" );
morphia.getMapper().getConverters().addConverter( new MyIdConverter() );
final MyClass myClass = new MyClass( new MyId( 42L ), "HelloMorphia" );
ds.save( myClass );
final MyClass found = ds.get(MyClass.class, myClass.id);
System.out.println( "found " + ToStringBuilder.reflectionToString( found ));
}
/**
* The converter for the immutable id must implement {@link SimpleValueConverter}
* so that <code>Mapper.readMappedField</code> uses this converter during
* updateKeyInfo/postSaveAction. Alternatively the id field could be annotated with
* {@link com.google.code.morphia.annotations.Property}.
*/
static class MyIdConverter extends TypeConverter implements SimpleValueConverter {
public MyIdConverter() {
super( MyId.class );
}
@Override
public Object decode( final Class targetClass, final Object fromDBObject, final MappedField optionalExtraInfo ) throws MappingException {
return new MyId( Long.parseLong( (String )fromDBObject ) );
}
@Override
public Object encode( final Object value, final MappedField optionalExtraInfo ) {
return value.toString();
}
}
public static class MyId {
private final Long _id;
public MyId(final long id) {
_id = Long.valueOf( id );
}
@Override
public String toString() {
return _id.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( _id == null ) ? 0 : _id.hashCode() );
return result;
}
@Override
public boolean equals( final Object obj ) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( getClass() != obj.getClass() ) {
return false;
}
final MyId other = (MyId) obj;
if ( _id == null ) {
if ( other._id != null ) {
return false;
}
}
else if ( !_id.equals( other._id ) ) {
return false;
}
return true;
}
}
@Entity
public static class MyClass {
@Id MyId id;
String name;
public MyClass() {
}
public MyClass( final MyId id, final String name ) {
super();
this.id = id;
this.name = name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment