Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom primary key type generate wrong code #1051

Open
passos opened this issue Aug 6, 2020 · 0 comments
Open

Custom primary key type generate wrong code #1051

passos opened this issue Aug 6, 2020 · 0 comments

Comments

@passos
Copy link

passos commented Aug 6, 2020

== Summary ==
When primary key is a custom type, the generated code for updateKeyAfterInsert and getKey will use String as return type instead of the actual custom type, which will cause compile error.

-- code to reproduce --

@Entity
public class Example {
    @Convert(converter = UUIDConverter.class, columnType = String.class)
    @Id
    private UUID id;

    static class UUIDConverter implements PropertyConverter<UUID, String> {
        @Override
        public UUID convertToEntityProperty(String databaseValue) {
            return UUID.fromString(databaseValue);
        }

        @Override
        public String convertToDatabaseValue(UUID entityProperty) {
            return entityProperty.toString();
        }
    }
}

the generated ExampleDao would be

public class ExampleDao extends AbstractDao<Example, String> {
    ... ...

    @Override
    protected final String updateKeyAfterInsert(Example entity, long rowId) {
        return entity.getId(); // this won't compile because entity.getId() returns UUID
    }
    
    @Override
    public String getKey(Example entity) {
        if(entity != null) {
            return entity.getId(); // this won't compile because entity.getId() returns UUID
        } else {
            return null;
        }
    }

    ... ...
}

The problem is the custom type was not considered when set up the value for pkType of Entity, which is processed in Entity.java

    void init2ndPass() {
        ... ...

        if (propertiesPk.size() == 1) {
            pkProperty = propertiesPk.get(0);
            pkType = schema.mapToJavaTypeNullable(pkProperty.getPropertyType());
        } else {
            pkType = "Void";
        }

        ... ...
    }

Also, if Entity was generated programmatically, for example

        Entity entity = schema.addEntity("CustomPkTypeEntity");
        entity.addStringProperty("id").customType("java.util.UUID", "org.greenrobot.greendao.daotest.customtype.UuidConverter").primaryKey();
        entity.addIntProperty("value");

then the generated Entity code would be

    private UUID id;
    public CustomPkTypeEntity(String id) { // this won't compile
        this.id = id;
    }

which String id should be UUID id.

Please see PR #1053 for fix details

passos added a commit to passos/greenDAO that referenced this issue Aug 6, 2020
fix for issue greenrobot#1051
When primary key is a custom type, the generated code for updateKeyAfterInsert and getKey will return String but the actual primary key is not.
passos added a commit to passos/greenDAO that referenced this issue Aug 11, 2020
fix for issue greenrobot#1051
When primary key is a custom type, the generated code for updateKeyAfterInsert and getKey will return String but the actual primary key is not.
@passos passos changed the title Customize primary key type doesn't work Custom primary key type generate wrong code Aug 11, 2020
passos added a commit to passos/greenDAO that referenced this issue Aug 11, 2020
fix for issue greenrobot#1051
When primary key is a custom type, the generated code for updateKeyAfterInsert and getKey will return String but the actual primary key is not.
passos added a commit to passos/greenDAO that referenced this issue Aug 11, 2020
When primary key is a custom type, the generated code for updateKeyAfterInsert and getKey will return String but the actual primary key is not.
see greenrobot#1051 for details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant