Note that there are some explanatory texts on larger screens.

plurals
  1. POOrmlite: Use generic as type for id
    primarykey
    data
    text
    <p>I'm using ormlite for database interaction in java. Works fine.</p> <p>Now I want to create a generic entity class. Perhaps I'll start with a bit of code to explain it.</p> <p>AbstractId is something like a base class for all ids:</p> <pre><code>public abstract class AbstractId { public AbstractId(String id) { this.id = id; } public String value() { return this.id; } } </code></pre> <p>Extended to: </p> <pre><code> public class SampleId extends AbstractId { public SampleId(String id) { super(id); } } </code></pre> <p>Now I have a abstract base entity to save some typing ;)</p> <pre><code>public abstract class AbstractEntity&lt;T&gt; { @DatabaseField(id = true, columnName = "id") private T id; @DatabaseField(columnName = "name", canBeNull = false) private String name; public AbstractEntity() { } } </code></pre> <p>which is extended by a sample entity:</p> <pre><code>@DatabaseTable(tableName = "sample") public class SampleEntity&lt;T extends AbstractId&gt; extends AbstractEntity&lt;T&gt; { @DatabaseField(canBeNull = false, columnName = "key") private String otherValue; public SampleEntity() { } } </code></pre> <p>So how can i persist T? The entity instance is created by </p> <pre><code>SampleEntity&lt;SampleId&gt; entity = new SampleEntity&lt;&gt;() // (*) </code></pre> <p>I tried to create a new BaseDataType and setting the DatabaseField to use this as persisterClass:</p> <pre><code>public class AbstractIdPersister extends BaseDataType { public AbstractIdPersister(SqlType sqlType, Class&lt;?&gt;[] classes) { super(sqlType, classes); } @Override public Object parseDefaultString(FieldType fieldType, String defaultStr) throws SQLException { return null; } @Override public Object javaToSqlArg(FieldType fieldType, Object obj) { AbstractId abstractId = (AbstractId) obj; return abstractId.value(); } @Override public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) throws SQLException { // HOW DO I GET THE GENERIC TYPE DEFINED ABOVE (*) } @Override public Object resultToSqlArg(FieldType fieldType, DatabaseResults results, int columnPos) throws SQLException { return results.getString(columnPos); } } </code></pre> <p>Didn't work ... of course..</p> <p>Does anyone has any idea how to solve this problem? Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload