Thanks your answers,
Unfortunately I can't use another framework: my goal is (or was?) to
implement an EJB3 compatible container with Guice. (I need it to
construct a test framework.).
In an EJB container the name of the persistence unit is a free
variable. I hope that I understood well your solutions, but I think
it's not enough dynamic. If I would like to use the solution above
than I must use reflection and scan all of my EJB beans, and create as
many binding as many persistence units are used in the code. I hoped
it is possible to do only with Guice and without Java reflection api.
But it could be possible only if the Provider interface contains some
parameter about the EntityManager field and its annotation.
So (as I see) I can't use Guice to solve his problem, because the
Guice API is too simple for me. But please write, if I am wrong.
Thanks
m.
On Mon, Sep 1, 2008 at 10:11 AM, Simone Tripodi
<[EMAIL PROTECTED]> wrote:
>
> Hi Elek,
> as Robbie suggested you - and reading oldest posts here in the group
> you can understand that Robbie always knows the best solution - if you
> choice the second way (and you still need help) I've prepared for you
> the implementation you need:
>
>
> public class PersistenceContextImpl implements PersistenceContext {
>
> private String unitName;
>
> public PersistenceContextImpl(String unitName) {
> this.unitName = unitName;
> }
>
> public Class<? extends Annotation> annotationType() {
> return PersistenceContext.class;
> }
>
> @Override
> public boolean equals(Object obj) {
> if (this == obj) {
> return true;
> }
>
> if ((obj == null) || (obj.getClass() != PersistenceContext.class)) {
> return false;
> }
>
> PersistenceContext other = (PersistenceContext) obj;
> return unitName.equals(other.unitName());
> }
>
> @Override
> public int hashCode() {
> return (127 * "unitName".hashCode() ^ unitName.hashCode());
> }
>
> public String unitName() {
> return unitName;
> }
>
> }
>
>
> Now you're able to bind EntityManager in this way:
>
> binder.bind(EntityManager.class).annotatedWith(new
> PersistenceContextImpl("Test1PU")).toProvider(new
> EntityManagerProvider(directory1));
>
> binder.bind(EntityManager.class).annotatedWith(new
> PersistenceContextImpl("Test2PU")).toProvider(new
> EntityManagerProvider(directory2));
>
> I hope this helps!
> Best regards,
> Simone
>
> 2008/8/31 Robbie Vanbrabant <[EMAIL PROTECTED]>:
>> The easy answer: consider using Warp Persist (http://www.wideplay.com) for
>> guicy persistence. Although support for multiple persistence units is not
>> implemented in 1.0, we have have experimental support for it in the trunk.
>> If you depend on that functionality let me know and we will help you get
>> started.
>>
>> The other answer would be yes, you can do that. Have a look at how Guice
>> implements Names.named(...). You basically have to bind to annotation
>> instances instead of types, and you do that by subclassing the annotation
>> and binding to an instance of that subclass.
>>
>> Hope this helps,
>> Robbie
>>
>> On Sun, Aug 31, 2008 at 6:59 PM, elek <[EMAIL PROTECTED]> wrote:
>>>
>>> Hi,
>>>
>>> Is there any way to inject dynamic values to a class according to the
>>> annotation parameters with Guice?
>>> Eg.: I have the following fields in a class:
>>>
>>> @PersistenceContext(unitName="Test1PU")
>>> EntityManager em1;
>>>
>>> @PersistenceContext(unitName="Test2PU")
>>> EntityManager em2;
>>>
>>> An I would like to inject different EntityManager instances.
>>> Unfortunately I can't access the annotation or the original Field over
>>> the the Provider interface:
>>>
>>>
>>> binder.bind(EntityManager.class).annotatedWith(PersistenceContext.class).toProvider(new
>>> EntityManagerProvider(directory, ???unitName value???));
>>>
>>> Is there any solution for it?
>>>
>>> Thanks:
>>> m.
>>>
>>
>>
>> >
>>
>
>
>
> --
> My LinkedIn profile: http://www.linkedin.com/in/simonetripodi
> My GoogleCode profile: http://code.google.com/u/simone.tripodi/
> My Sourceforge: https://sourceforge.net/users/stripodi
> My Picasa: http://picasaweb.google.com/simone.tripodi/
> My Tube: http://www.youtube.com/user/stripodi
> My Del.icio.us: http://del.icio.us/simone.tripodi
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---