morphia: Cascading Generics are not correctly mapped

Describe the bug Similar to https://github.com/MorphiaOrg/morphia/issues/1481

When having cascaded generics the type is not correctly mapped.

To Reproduce See code example under Additional context

Expected behavior UUID is mapped to UUID instead of Binary

  • Server Version: 4.0.17
  • Driver Version: 4.0.5
  • Morphia Version: 2.0.1

Additional context

@Entity
public class GenericEntity<T> {
	@Id
	protected T id;
	protected T test;
	protected UUID test2;

	public T getId() {
		return id;
	}

	public void setId(T id) {
		this.id = id;
	}

	public T getTest() {
		return test;
	}

	public void setTest(T test) {
		this.test = test;
	}

	public UUID getTest2() {
		return test2;
	}

	public void setTest2(UUID test2) {
		this.test2 = test2;
	}
}
@Entity
public class SpecificEntity<ID> extends GenericEntity<ID> {
	private long number;

	public long getNumber() {
		return number;
	}

	public void setNumber(long number) {
		this.number = number;
	}
}
@Entity
public class MoreSpecificEntity extends SpecificEntity<UUID> {
	private long number2;

	public long getNumber2() {
		return number2;
	}

	public void setNumber2(long number2) {
		this.number2 = number2;
	}
}
	public static void main(String[] args) {

		CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
				CodecRegistries.fromProviders(new UuidCodecProvider(UuidRepresentation.STANDARD)),
				MongoClientSettings.getDefaultCodecRegistry());

		final Datastore datastore = Morphia.createDatastore(
				MongoClients.create(MongoClientSettings.builder()
						.applyConnectionString(new ConnectionString("mongodb://127.0.0.1/"))
						.uuidRepresentation(UuidRepresentation.STANDARD).codecRegistry(codecRegistry).build()),
				"morphia_example");

		datastore.getMapper().mapPackage("de.test");

		datastore.ensureIndexes();

		MoreSpecificEntity beforeDB = new MoreSpecificEntity();
		beforeDB.setId(UUID.randomUUID());
		beforeDB.setTest(UUID.randomUUID());
		beforeDB.setTest2(UUID.randomUUID());
		beforeDB.setNumber(13);
		beforeDB.setNumber2(14);
		datastore.save(beforeDB);

		MoreSpecificEntity fromDB = datastore.find(MoreSpecificEntity.class).filter(Filters.eq("_id", beforeDB.getId()))
				.first();

		System.out.println(fromDB);

		datastore.getDatabase().getCollection("specificEntity").deleteMany(new BasicDBObject());
	}

image

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 25 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Piffle. Ok. I’ll dig in to it. Thanks.

ok. i had to fix the toString(). I see it. pretty sure this was working, too. disappointing. i’ll dig further.

I’ve been working it pretty hard the last several nights. I had a nice clean fix but it made test runs 5x slower. So I’m taking the lessons learned and trying a simpler fix. Hopefully the next day or two will set it done.