Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Enums with Generic Classes
    primarykey
    data
    text
    <p>I am switching gears from the interpreted world and I am trying to replicate a pattern I have used in the passed with great success but in Java.</p> <p>The short of it is I am making a custom DAO (not using Hibernate or anything like that...ultimately it will be generic to handle different database engines (SQL and NoSQL). For breaking into I am just using a MySQL/ Prepared Statements.</p> <p>I want to basically define a DataMapper object that is responsible for mapping the field of a record to the fields in a database. I am trying to encapsulate any redundant logic for implementation details of transforming a type to the format the database expects. In the DataMapper I am trying to utilize an enum to list the fields:</p> <pre><code>public abstract class AbstractDataMapper&lt;X&gt; { public static abstract class Field&lt;Y&gt; { protected String name; public Field(String name) { this.name = name; } public String getName() { return this.name; } public abstract void setValue(PreparedStatement stmt, int parameterIndex, Y value) throws SQLException; public abstract Y getValue(ResultSet rset) throws SQLException; } public static class IntegerField extends AbstractDataMapper.Field&lt;Integer&gt; { public IntegerField(String name) { super(name); } @Override public void setValue(PreparedStatement stmt, int parameterIndex, Integer value) throws SQLException { stmt.setInt(parameterIndex, value.intValue()); } @Override public Integer getValue(ResultSet rset) throws SQLException { return new Integer(rset.getInt(this.name)); } } } public class UserDataMapper extends AbstractDataMapper&lt;UserDataRecord&gt;{ public static enum Field { entryId(new AbstractDataMapper.IntegerField("entryid")); AbstractDataMapper.Field&lt;?&gt; field = null; Field(AbstractDataMapper.Field&lt;?&gt; field) { this.field = field; } public AbstractDataMapper.Field&lt;?&gt; getField() { return this.field; } public String getName() { return this.field.getName(); } } </code></pre> <p>Obviously I tried to truncate the example to the meaningful parts. The client code for exercising this </p> <pre><code> public int insert(UserDataRecord user) { ... final String query = "INSERT INTO users SET " + UserDataMapper.Field.entryId.getName() + " = ? "; stmt = conn.prepareStatement(query); UserDataMapper.Field.entryId.getField() .setValue(stmt, 1, user.getEntryId()); } </code></pre> <p>The issue I am encountering is with the last line. I am trying to use the generic object associated with the <code>entryId</code> enum to use it's special setValue function which handles getting the Integer into the proper format. My IDE is giving me the following error: The method setValue(PreparedStatement, int, capture#13-of ?) in the type AbstractDataMapper.Field is not applicable for the arguments (PreparedStatement, int, Integer).</p> <p>I am sure I could get into a series of crazy typecasts...but at that point it would probably just be easier to expose the translation details.</p> <p>Any ideas on getting this to work or a modification to this pattern that is better? </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.
 

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