Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7733345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:57:52+00:00 2026-06-01T06:57:52+00:00

I’m using ormlite for database interaction in java. Works fine. Now I want to

  • 0

I’m using ormlite for database interaction in java. Works fine.

Now I want to create a generic entity class. Perhaps I’ll start with a bit of code to explain it.

AbstractId is something like a base class for all ids:

public abstract class AbstractId {
    public AbstractId(String id) {
        this.id = id;    
    }

    public String value() {
        return this.id;
    }
}

Extended to:

   public class SampleId extends AbstractId {
        public SampleId(String id) {
            super(id);
        }
    }

Now I have a abstract base entity to save some typing 😉

public abstract class AbstractEntity<T> {

    @DatabaseField(id = true, columnName = "id")
    private T      id;

    @DatabaseField(columnName = "name", canBeNull = false)
    private String name;

    public AbstractEntity() {
    }
}

which is extended by a sample entity:

@DatabaseTable(tableName = "sample")
public class SampleEntity<T extends AbstractId> extends AbstractEntity<T> {

    @DatabaseField(canBeNull = false, columnName = "key")
    private String otherValue;

    public SampleEntity() {
    }
}

So how can i persist T? The entity instance is created by

SampleEntity<SampleId> entity = new SampleEntity<>() // (*)

I tried to create a new BaseDataType and setting the DatabaseField to use this as persisterClass:

public class AbstractIdPersister extends BaseDataType {

    public AbstractIdPersister(SqlType sqlType, Class<?>[] 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);
    }

}

Didn’t work … of course..

Does anyone has any idea how to solve this problem? Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:57:53+00:00Added an answer on June 1, 2026 at 6:57 am

    So given your hierarchy, ORMLite is going to have a problem when it needs to create an instance of your abstract class from the associated database field. That’s what the sqlArgToJava method is trying to do. In the DATE_LONG type, for example, the date is stored in the database as a long time milliseconds, so the sqlArgToJava method is:

    public Object sqlArgToJava(FieldType fieldType, Object sqlArg, int columnPos) {
        // create a Date object
        return new Date((Long) sqlArg);
    }
    

    Obviously there is no way to create an instance of AbstractId. You are going to have to make a persister for each of the subclasses instead.

    If you are good with reflection then you could do something like the following but it’s a hack for sure. I think creating a persister for each of your concrete classes is the way to go.

    try {
        // find the type of the field, and look for a String constructor
        return fieldType.getType().getConstructor(String.class).newInstance(sqlArg);
    } catch (Exception e) {
        throw SqlExceptionUtil.create("could not call the string constructor on "
            + fieldType.getType(), e);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.