Help with Custom Dao

1,633 views
Skip to first unread message

glenviewjeff

unread,
Apr 21, 2011, 10:29:09 AM4/21/11
to ORMLite Users
I would be really grateful for an example and more detailed
documentation with regard to creating a custom Dao. I've tried it on
my own and I'm getting a ClassCastException.

My custom Dao looks like this:

public interface ItemDao extends Dao<Item, Integer> {
public int create(Item anItem) throws SQLException;
}

public class ItemDaoImpl extends BaseDaoImpl<Item, Integer> implements
ItemDao {
public ItemDaoImpl(ConnectionSource aConnectionSource) throws
SQLException {
super(aConnectionSource, Item.class);
}

@Override
public int create(Item anItem) throws SQLException {
return super.create(anItem);
}
}

I tried to use it like this:
ItemDaoImpl it = DaoManager.createDao(myConnectionSource,
Item.class);
if (it.create2(anItem) != 1) {

but get this exception:
java.lang.ClassCastException: com.j256.ormlite.dao.BaseDaoImpl$1
cannot be cast to
com.zoomabug.flashcards.item_and_category.ItemDaoImpl

This actually makes sense to me because I don't know how the
DaoManager would magically know to create a ItemDaoImpl instead of
BaseDaoImpl.

What am I missing? Thanks so much in advance for your help.

Jeff

glenviewjeff

unread,
Apr 21, 2011, 10:32:47 AM4/21/11
to ORMLite Users
Sorry, just after I posted this, I discovered how to do it correctly.
I'm not sure why I expected there to be some magic here, but still it
would be nice if there were an example:

ItemDaoImpl it = new ItemDaoImpl(myConnectionSource);

glenviewjeff

unread,
Apr 21, 2011, 10:46:32 AM4/21/11
to ORMLite Users
Sorry to vacillate, but I realized here that if I use the new
operator, then I lose the caching feature from the DaoManager class.
Is there a better way?

Gray Watson

unread,
Apr 21, 2011, 11:00:41 AM4/21/11
to ormlit...@googlegroups.com
On Apr 21, 2011, at 10:46 AM, glenviewjeff wrote:

> Sorry to vacillate, but I realized here that if I use the new
> operator, then I lose the caching feature from the DaoManager class.
> Is there a better way?

There is a better way Jeff but it seems to be very poorly documented. Sorry about that.

There is a doaClass field on the @DatabaseTable annotation where you specify the class of the dao to use for that table:

http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/table/DatabaseTable.html#daoClass%28%29

There's a _very_ meager mention of it in the manual and nothing in the section on the DaoManager unfortunately.

http://ormlite.com/docs/database-table

gray

jc

unread,
Apr 21, 2011, 11:03:32 AM4/21/11
to ORMLite Users
On Apr 21, 9:46 am, glenviewjeff <j...@theaxelrods.com> wrote:
> Sorry to vacillate, but I realized here that if I use the new
> operator, then I lose the caching feature from the DaoManager class.
> Is there a better way?
>
You can specify your custom "daoClass" with the DatabaseTable
annotation and the DaoManager will create your Dao instead:
http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/table/DatabaseTable.html

For your case, it would be:
@DatabaseTable(daoClass=ItemDaoImpl.class)

glenviewjeff

unread,
Apr 21, 2011, 11:06:52 AM4/21/11
to ORMLite Users
Hi Gray:

Thanks for the quick response! I was actually figuring this out as
you responded based on this thread (http://groups.google.com/group/
ormlite-user/browse_thread/thread/ecdde67a61a006cf/c14ea76be6ec4ab9)

However, when I added the annotation as follows:

@DatabaseTable(tableName = "Items", daoClass = ItemDaoImpl.class)
public class Item implements ItemInterface { ...

I get the following exception:
java.sql.SQLException: Could not find public constructor with
ConnectionSource, DatabaseTableConfig parameters in class
class ...ItemDaoImpl

Any ideas what I'm doing wrong?


On Apr 21, 10:00 am, Gray Watson <256....@gmail.com> wrote:
> On Apr 21, 2011, at 10:46 AM, glenviewjeff wrote:
>
> > Sorry to vacillate, but I realized here that if I use the new
> > operator, then I lose the caching feature from the DaoManager class.
> > Is there a better way?
>
> There is a better way Jeff but it seems to be very poorly documented.  Sorry about that.
>
> There is a doaClass field on the @DatabaseTable annotation where you specify the class of the dao to use for that table:
>
>    http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/table/Databa...

jc

unread,
Apr 21, 2011, 11:22:42 AM4/21/11
to ORMLite Users
>
> I get the following exception:
> java.sql.SQLException: Could not find public constructor with
> ConnectionSource, DatabaseTableConfig parameters in class
> class ...ItemDaoImpl
>
> Any ideas what I'm doing wrong?
>

You need to override the constructors from BaseDaoImpl as well for it
to work. To be specific, the DaoManager is trying to call the one in
your error. Just add this to your dao:

public ItemDaoImpl(ConnectionSource connectionSource,
DatabaseTableConfig<T> tableConfig) throws SQLException {
super(connectionSource, tableConfig);
}

glenviewjeff

unread,
Apr 21, 2011, 11:42:19 AM4/21/11
to ORMLite Users
This worked, thanks! I'll try to look up why "dummy" constructors
have to be implemented instead of the compiler using the superclass's
constructor by default.

Gray Watson

unread,
Apr 21, 2011, 11:44:59 AM4/21/11
to ormlit...@googlegroups.com
On Apr 21, 2011, at 11:42 AM, glenviewjeff wrote:

> This worked, thanks! I'll try to look up why "dummy" constructors
> have to be implemented instead of the compiler using the superclass's
> constructor by default.

That's part of the Java spec. Constructors are not inherited from the base class unless you define none of them in the superclass.
gray

Evan Ruff

unread,
May 12, 2012, 12:34:29 PM5/12/12
to ormlit...@googlegroups.com
Hey Guys,

Sorry to bring up an old thread, but would it be possible to change this slightly? In my scenario, my model is decoupled from the database implementation. I would like to use a custom DAO, but having to import the class into my model creates a dependency on my Android Project that I'd like to avoid. Any way we could have a daoName field added to the @DatabaseTable that took in the class name as a string? Then just do a Class.forName on the DaoManager.createDao ? Would this create a problem?

Thanks,

E

Evan Ruff

unread,
May 12, 2012, 12:53:31 PM5/12/12
to ormlit...@googlegroups.com
Yeah so I went ahead and made some changes to the classes to accommodate this. 

Any comment? Unit tests seem to be fine, is this going to blow up on me somewhere?

Thanks,

E
daoName.zip

jc

unread,
May 14, 2012, 12:36:07 PM5/14/12
to ORMLite Users
On May 12, 11:53 am, Evan Ruff <evan.r...@gmail.com> wrote:
>
> Any comment? Unit tests seem to be fine, is this going to blow up on me
> somewhere?
>
If you don't want your model class to have the dependency on your
custom Dao implementations, you may better off creating your
TableConfig objects at runtime or use the new config file (see the
DatabaseConfigUtil) so you don't have to define the custom Dao class
within the annotation.

http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#SEC41

jc

unread,
May 14, 2012, 12:38:55 PM5/14/12
to ORMLite Users
On May 14, 11:36 am, jc <jroma...@gmail.com> wrote:
>
> TableConfig objects at runtime or use the new config file (see the
> DatabaseConfigUtil) so you don't have to define the custom Dao class
> within the annotation.
>
> http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#SEC41

That last suggestion to use the config file should have include "if
you are running on android", hopefully that didn't cause any
confusion..

Carl

unread,
Feb 18, 2013, 7:25:47 PM2/18/13
to ormlit...@googlegroups.com


On Monday, May 14, 2012 6:36:07 PM UTC+2, jc wrote:
If you don't want your model class to have the dependency on your
custom Dao implementations, you may better off creating your
TableConfig objects at runtime or use the new config file (see the
DatabaseConfigUtil) so you don't have to define the custom Dao class
within the annotation.

http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#SEC41

Could you give an example on how to specify a the dao classes while invoking writeconfigfile ?

Cheers,
Carl.
Reply all
Reply to author
Forward
0 new messages