How to persist Joda Money entities using Ofy v5?

217 visualizzazioni
Passa al primo messaggio da leggere

Drew Spencer

da leggere,
28 apr 2014, 10:54:2628/04/14
a objectify...@googlegroups.com
I'm considering using Joda Money in my app in the near future and was looking for info on how to persist Money objects using objectify.

I found this github repo which seems promising and has recent activity: https://github.com/sappenin/objectify-utils

Does anyone have any experience using it and would you recommend it? It seems like it could be a useful library but I don't want to use it if there may be compatibility issues in the future, etc.

If not then what is the best way to persist money types using Ofy?

Thanks,

Drew

Jeff Schnitzer

da leggere,
28 apr 2014, 11:27:0628/04/14
a objectify...@googlegroups.com


--
You received this message because you are subscribed to the Google Groups "objectify-appengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Fuelling

da leggere,
28 apr 2014, 18:38:1128/04/14
a objectify...@googlegroups.com, je...@infohazard.org
Hey Drew,

I created/maintain objectify-utils, and was planning to update it to Ofy5 -- probably won't be for a few weeks though, at least.

The objectify-utils project was created to (mainly) enable native Datastore Indexing/sorting of both BigDecimal and Joda Money/BigMoney types (using Objectify, of course).  The objectify-utils library offers some other minor benefits, but I would say if you don't need to sort your datastore entities by a "price" or BigDecimal "amount" field, then the Translators provided by objectify should suffice.

Thanks!
david  

To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appengine+unsub...@googlegroups.com.

Drew Spencer

da leggere,
29 apr 2014, 04:49:5529/04/14
a objectify...@googlegroups.com, je...@infohazard.org
Hi David, thanks for the reply.

So just to make sure I have this clear in my head, to simply store Money/BigMoney types using Objectify I just have to enable the translator as Jeff mentioned above, and Objectify will do the work in converting them back and forth on save and get..., but if I want to add an index on one of those fields I can use objectify-utils for that (once it's updated). Is that correct?

Thanks

Drew

Jeff Schnitzer

da leggere,
29 apr 2014, 12:58:0429/04/14
a objectify...@googlegroups.com
The Objectify version (as noted in javadocs) just stores money as its
toString() representation, eg "$123.45 USD". This will work and allow
equality searching but does not allow for inequality matches.
Inequality matches are a little confusing anyways since how do you
compare USD to CDN or whatnot? But it could make sense to store as,
say, a decimal value if you only use USD.

I don't have a need for inequality matching on money types so the
simple string conversion is what I use. I haven't looked at David's
solution but it's probably more clever.

Jeff
>>>> an email to objectify-appen...@googlegroups.com.
>>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
> --
> You received this message because you are subscribed to the Google Groups
> "objectify-appengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to objectify-appen...@googlegroups.com.

Huseyn Guliyev

da leggere,
29 apr 2014, 18:04:5629/04/14
a objectify...@googlegroups.com, je...@infohazard.org
if one currency, rather than decimal, i suggest to store currency as long (cents, pennies).

>>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
> --
> You received this message because you are subscribed to the Google Groups
> "objectify-appengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an
Il messaggio è stato eliminato
Il messaggio è stato eliminato

Jeff Schnitzer

da leggere,
15 lug 2015, 00:31:4715/07/15
a Michel Stéphane Bruno, objectify...@googlegroups.com
Are you sure you don't have multiple versions of Objectify on your classpath (mvn clean to be certain)? Maybe multiple versions of Joda Money? Are you sure that you are always using the same ObjectifyFactory object instance - maybe you somehow created more than one?

The joda money translators work well - I use them very heavily.

Jeff



On Tue, Jul 14, 2015 at 7:42 PM, Michel Stéphane Bruno <sbru...@gmail.com> wrote:
Hi Jeff,

I did just what you suggested. I used the static method:

JodaMoneyTranslators.add(ObjectifyService.factory());

to register Joda translators in a class that implements ServletContextListener, the same place where I register all my entities. Then I have some Money fields in my entities without any annotation. I am able to save the entities in the datastore, but when I try to retrieve the entities from the datastore, I get the error message org.joda.money/Money must have a no-arg constructor.


Am I missing something?


Thanks.

Michel Stéphane Bruno

da leggere,
15 lug 2015, 00:32:2515/07/15
a objectify...@googlegroups.com, je...@infohazard.org
Hi Jeff,

I did just what you suggested. I used the static method:

JodaMoneyTranslators.add(ObjectifyService.factory());

to register Joda translators in a class that implements ServletContextListener, the same place where I register all my entities. Then I have some Money fields in my entities without any annotation. I am able to save the entities in the datastore, but when I try to retrieve the entities from the datastore, I get the error message org.joda.money/Money must have a no-arg constructor.


Am I missing something?


Thanks.


On Tuesday, April 29, 2014 at 12:58:04 PM UTC-4, Jeff Schnitzer wrote:

Michel Stéphane Bruno

da leggere,
15 lug 2015, 00:32:2515/07/15
a objectify...@googlegroups.com, je...@infohazard.org
Hi David,

I tried to use your objectify-utils library, but it seems that the class JodaTranslatorFatory.java is missing. I can't find it anywhere, and even when I register all the translators in your package and I annotate my Money fields with your Money annotation, I still cannot retrieve entities that have a Money field from the datastore: I always get that org.joda.money.Money must have a no-arg constructor.

I tried using the built-in Joda in Objectify and removed all the objectify-utils annotation and used the JodaMoneyTranslators.add(ObjectifyService.factory()) static method to register all Joda-related translators, I still get the same error.

What am I doing wrong? Can you provide step-by-step instructions on how to enable translators and how/whether to annotate Money fields to get this to work?

Thanks,
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.

Michel Stéphane Bruno

da leggere,
15 lug 2015, 00:53:1715/07/15
a Jeff Schnitzer, objectify...@googlegroups.com
I do not see what I do wrong. See below the class where I register all my entities and the translators (I omitted the import statements for the entities):

import com.googlecode.objectify.ObjectifyService;

import com.googlecode.objectify.impl.translate.opt.joda.JodaMoneyTranslators;


public class OfyHelper implements ServletContextListener {


@Override

public void contextDestroyed(ServletContextEvent arg0) {

}


@Override

public void contextInitialized(ServletContextEvent arg0) {

// This will be invoked as part of a warmup request, or the first user

// request if no warmup request was invoked

ObjectifyService.register(FinancialInstitution.class);

ObjectifyService.register(AccountHolder.class);

ObjectifyService.register(Account.class);

ObjectifyService.register(AccountHTG.class);

ObjectifyService.register(AccountUSD.class);

ObjectifyService.register(Card.class);

ObjectifyService.register(Merchant.class);

ObjectifyService.register(LedgerEntry.class);

ObjectifyService.register(Transaction.class);

JodaMoneyTranslators.add(ObjectifyService.factory());

}


}

On my classpath, I have objectify 5.1.5.jar, joda-money-0.10.0.jar, guava-18, joda-time-2.8.1.

I cleaned the project and recompiled. Same error...

Jeff Schnitzer

da leggere,
15 lug 2015, 00:56:3615/07/15
a objectify...@googlegroups.com
There's an order dependence with all custom translators. You must add the JodaMoneyTranslators *before* registering classes. During registration, the available translators are used to construct a metamodel of your entities.

This is documented in a number of places, but not in the javadocs for JodaMoneyTranslators. I'll add it there too.

Jeff

Michel Stéphane Bruno

da leggere,
15 lug 2015, 13:27:4015/07/15
a objectify...@googlegroups.com, je...@infohazard.org
Yes. Thank you. That was indeed the issue. I also realised that I was not using the same factory object to register classes and the translators.

It now works. I haven't seen where in the documentation that it is mentioned that translators should be registered before classes. I think it's a good idea to put it all in the same place like you said.

Thanks a mil!
Rispondi a tutti
Rispondi all'autore
Inoltra
0 nuovi messaggi