Federico Minarelli

+ Follow
since Jan 16, 2010
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Federico Minarelli

James Sutherland wrote:I think you are probably making things more complicated than they need to be. Since JPA (EclipseLink) already maintains an object cache, you can probably do away with your Cache. That should simplify things considerably.

Just call find() with the id to find the entity, and if it is in the cache, it will not access the database. You will only need to call merge() if you have changed a detached object.

Hi and thanks for answering!
As far as I understood, shared cache is enabled by default in EclipseLink. To make sure to activate it, I added to my persistence.xml. Shouldn't then the result of each query be automatically cached?
I've changed my Cache class to query directly the DB (throught DAOs'methods) instead of storing the values into Maps, but with a FINE log level I can see the SQL SELECT queries get executed every time I call a findXXXCreateIfAbsent() method.. What is wrong in this approach?

thanks again for your help! Bye
Hello everybody!
I hope you can help me to get a better understanding of how Persistence Contexts get propagated... I spent a lot of time with this problem but am still not sure of what happens behind the scenes..
Here follows a short description of what I have done..

I am using EJB 3.1 together with JPA2 (eclipseLink) and have the following Entities (simplified):

They model the fact that a Testsuite groups several Tests which fall under a specific name (e.g.: "Standard testsuite"). Every time a Testsuite is executed on a Server, a file is produced that looks something like this:

Testsuite: Standard testsuite
Executed on: Server1
Started at: 2011-10-10 10:11:24

TEST-1024 "Test with name X": Success
TEST-1025 "Test whit another name": Failure

At this point, other Entities come into play:

The idea is that an output File as the one above describes a TestsuiteRun, which has an association to the TestSuite that got executed (its name is Unique) together with a collection of TestRuns, each corresponding to a Test. For brevity, in the pseudo-code above I've omitted to specify that most of the associations are bidirectional, i.e. a Test knows all of its TestRuns (and viceversa); a Machine knows all of the TestsuiteRuns which got executed on it; a TestRun knows the TestsuiteRun it belongs to, and so on..

Now, my application parses a file at a time with the twofold aim of:

  • inserting (if they have not been already stored) new Testsuites/Tests/Server into the DB
  • creating a TestsuiteRun object together with all the related TestRuns.

  • For better performances at point 1), I use a cache object, which gets initialized with pre-existing data from the DB and is responsible for adding new records when it is the case. Here is a reduced version of it:

    All of the DAOs use a Transaction-scoped EntityManager:

    As you can see, I use an extended PersistenceContext and every time I read or write to the DB I use em.merge() in order to keep the entities managed. I have read in the book "Pro JPA 2" that in situations like this the PC gets propagated (in my case from Cache to TestDAO): this should mean that doing testDao.persist(t) within retrieveTestCreateIfNotPresent() should be enough to make sure t remains managed (hence the em.merge() in this case could be omitted)?

    The situation is more unclear when the file parser comes into play. The code which does the job is something like:

    Being the TestsuiteRunBuilder declared as follows:

    Also this class uses an Extended PC and such a bean should live only in the time span which goes from the beginning of the file parsing to the insertion into the DB (build is annotated with Remove). Of course the associations of TestsuiteRun are instructed to propagate merge..
    You can see that I've marked Cache as Singleton, as I'd like to let the cache remain in memory also for the next file..
    The application seems to work, but I am not really sure why.. What happens for example when I parse the second file? In what relation is the PersistenceContext of the new (the 2nd) TestsuiteRunBuilder object with that of Cache (which should be still "alive")?

    I am really, really confused... What shall I change in this design? Isn't there a way to make things easier?

    Thanks a lot for your help!


    Thanks for your suggestion Tim.. Unfortunately the solution to the problem was another.. I solved it after reading here: http://download.oracle.com/javaee/6/api/javax/faces/component/UISelectMany.html and here: http://stackoverflow.com/questions/3822058/jsf-2-0-use-enum-in-selectmany-menu/3823776#3823776. In short, i needed to explicitly define a converter for my selectManyCheckbox:

    12 years ago
    any idea? I am getting really crazy with this problem...
    I have tried to re-create a simplified version of the project from scratch in Netbeans:

    1) New Web application project.
    2) I have created 2 entities "TestRun" and "Tracker". For TestRun.java I typed the following code:

    And in Tracker.java:

    I then generated automatically getters and setters for both. Then I right clicked my project: "new-> JSF Pages from entity classes..", I choosed my 2 entities and I got some a lot of staff free.
    In testRun/create.xhtml the form looks now (after adding a couple of lines of code) like this:

    getItemsAvailableSelectMany returns a list of SelectItems which is built from the DB:

    I started the app, added 2 Trackers (no problems) and then tried to add a TestRun with an associated tracker. The result?

    of course..

    What is wrong in my code? I hope you can help me.. Thanks!
    12 years ago
    Hello everybody! I am new both to JSF and JPA and hope you can help me with a problem I couldn't solve even after several hours of debugging and google searches..

    I have 2 entities which are in a ManyToMany relationship:

    They model the fact that a TestRun can be associated with several trackers (and viceversa).

    I then have 2 Managed Beans (generated by NetBeans 7.0) called respectively TestRunController and TrackerController: each such bean has an aggregation to an Entity. I show here the relevant code for just one of them, as they are almost identical:

    I added into the DB a couple of Trackers and finally created a Facelet for the insertion of a TestRun. This facelet uses a selectManyCheckbox which should allow to choose which trackers are associated with a testrun:

    The form gets properly created and displayed and I am even able to create a TestRun record when I don't select any Tracker. But as soon as I select a Tracker, I get the error "Validation Error: Value is not valid" (I think this should correspond to javax.faces.component.UISelectMany.INVALID).

    I read in internet (and in this forum as well) that this problem is sometimes connected with a missing overriding of equals(), but this should not be my case... Furthermore, I noticed (while debugging) that such method gets never invoked after the form is sent. The same happens with TrackerControllerConverter#getAsObject(), which according to several posts should be another cause for the problem...

    I am relly confused and hope you can help me..

    Bye and... THANKS!
    12 years ago
    Hi! I have the same question.. We would like to organise a study group for the OCPJP in my company and I'd be glad to hear some experiences/suggestions...

    Thanks a lot! Bye

    Junilu Lacar wrote:Hi all,
    I was just wondering if anybody out there has organized or participated in a study group for the certification exam. If you have, would you mind sharing your experience? What worked and what didn't? What would you have done to make the group study time more effective?

    hi all!
    I think I solved my problem but I´d kindly like to ask your suggestions about another point. "Head 1st Design Pattern" suggests to implement the "State Pattern" by defining a State interface:

    And each class implementing State looks something like this:

    The FSM looks like this:

    Now, I`d like to distinguish between Transitory States and Final States (states which cannot be leaved). Imagine the FSM represents a computation: it would be nice to store the computation`s result into a FinalState (eg: Success/Error).. Hence, a FinalState would be similar to a State, with the addition of 2 more methods:

    Let`s imagine for a moment that something like this would be the solution:

    The FSM should have now 2 new States:

    The method StateAImpl#onEventY() would now look like this:

    And finally, a client of the FSM would have to do something like this:

    Now my question: what do you think of this design? I think it could be done much better: both the use of the instanceof operator (inheritance) and the if (fsm.getCurrentState().equals(fsm.getSuccessState()){} (what if I have 1000 final states possible?) are for me "bad smells".. The problem is: until now I couldn't find a better solution.. May you help me with new ideas please?

    Thanks a lot! Bye,
    Hi everybody! I would like to ask your suggestions on how to best solve the following problem.

    I have to implement something which actually is a finite state machine, in which the transitions are driven by several messages (unfortunately such messages don't share any common interface, and they have each a lot of fields whose values can lead to different states). The problem is very similar to that exposed in the chapter of "Head 1st Design Patterns" explaining the State pattern. The biggest difference with such example is that, in my case, I will have really a lot of States each driven by a different value of a message's field.

    For example, consider that the state machine models a RescueTaxi (that is not actually my case, but I tried to make an example which could be as similar as possible to mine). By "rescue taxi" I mean a taxi which can also act as an ambulance (I used a bit of fantasy here! ). Possible incoming messages for a RescueTaxi are TransportOrder with fields [StartPosition, DestinationPosition, ID, Status={Startable, Started, Cancelled}], which represent an Order for a Taxi to drive a passeger from X to Y, EmergencyOrder [StartPosition, DestinationPosition, ID, Status={Startable, Started, Cancelled}, Priority={High, Low, Medium}] and so on. (Please ignore the fact that the messages in this example are almost identical, in the real case they share only a few fields, and I have no control over such messages). Now, my RescueTaxi state machine can have a lot of states, for example:
    -Initial (Taxi is parked somewhere and waits for an order)
    - TransportOrderStartableReceived (taxi sends an ACK for the order or decides to refuse the job)
    - TransportOrderStartedReceived (taxi can now drive towards its StartPosition)
    - TransportOrderCancelled (taxi is notified about the Transport Order's cancellation and drives back to the parking (Initial state))
    - EmergencyOrderStartableReceived((taxi sends an ack for the emergency order)
    - EmergencyOrderStartedLowPriorityReceived (taxi can drive at normal speed towards its start postion)
    - EmergencyOrderStartedMediumPriorityReceived (taxi is allowed to drive fast to the start position, but without using the alarm)
    - EmergencyOrderStartedHighPriorityReceived (taxi is allowed drive fast towards its start position and to switch the alarm on), etc..

    Now, if I follow the State pattern alone, I will need all the above mentioned states (and much more!).. On the other side, in general, building a State hierarchy is not a solution: i.e. imagine that the RescueTaxi has to react differently to an EmergencyOrder 's cancellation (e.g according to the priority and the current state it can decide to drive the patient to its destination): in such case, having just an EmergencyOrderStartedReceived state would lead to something like a switch (priority) {} to choose which will be the next state (thus erasing the benefits of the State pattern)...

    My question is hence: what would be in your opinion a good solution to this kind of problems?

    Thank you very much!
    Hi everybody!
    I am facing a problem which I think cannot be solved too easily, but I will try to share it with you in the hope someboby may give me a good idea..

    I have the following situation:

    Several classes implement this interface each listening to a specific kind of message. Such listeners are registered at the application startup (through Spring), and each time a message of type T arrives (via JMS), the corresponding receiveMsg(T msg) is invoked by a Message dispatcher.
    Now, I want to extend these listeners to act as MsgMatcher<T> instead of simple "listeners", but without changing anything in their original implementation .. The difference between listeners and matcher is that the latest can "parse" the message content and decide if a given message has to be discarded or dispatched to a client. Of course, I would like to promote my Listeners to Matchers without changing any line of legacy code (too dangerous! ).. So I thought of Decorator pattern:
    I would like to have something like this:

    So, every time (in new code) I want to promote a listener to matcher I will do:

    Of course, this solution won't work, since the message dispatcher will not get its reference to the listener updated (will invoke the receiveMsg() of MsgListener). Hence, I am afraid I have to intervene somewhere at the root of the problem (Spring startup?), my question is how?
    As I told, I have to cope with a huge project where every change can be dangerous, and I would like to minimize the side effects of such changes..

    Do you have any suggestions?

    thanks a lot! Bye!

    Steve Luke wrote:One thought is to replace the LatchGuardedMessage with a BlockingQueue. When you wait for a message you use either the take() or the poll(...) methods to get the next message out of the queue. When a new message comes in you add it to the queue with put(...). That way, if two messages come in consecutively the waiter will be sure to get the first message first, and not lose the second message.

    hi steve, and thanks for your reply! I thought about BlockingQueue but I used latches in the end because I am dealing with multiple subscribers: a poll() will let the message be delivered only to the first registered subscriber.. Do you know how could solve this?

    thanks again! Bye!
    Hi everybody!
    I am trying to figure out the best possible approach to implement a publish/subscribe mechanism which should work more or less like this:
    Several Subscribers (1 Thread per Subscriber) subscribe their interest for a given topic T and a given kind of message M by calling initListenerForTopic(T topic) followed by waitNewMessage(T topic). Such threads get blocked until a publisher (i.e., a separate Thread) publishes a message having the specified topic T.

    I don't like my solution at all, above all because it is not really thread safe..

    I used latches to block the subscriber Threads, here is how it looks like:

    The most evident problem is marked by ***: the desired behavior would be that, as soon as the latch gets opened, the message retrieved by getMessage() is that set by the call of setMessage() which caused the latch to open. Nevertheless, this is not guaranteed to happen. In fact, two successive calls to setMessage() may let the subscriber retrieve another message.. In other words, what I marked by *** should happen atomically, and this is not the case...
    I am quite sure there are much better ways to achieve this behavior.. What would you advise?

    Thanks a lot!
    Finally I solved my first issue in the following way :

    I use then a method which returns a IJavaElement (both IType and IMethod are IJavaElements) after calling both the methods above (it could happen I find the class but not the method, so I want to open just the class file).. Finally, I call

    I am not sure if this is the best approach possible (I guess not! ), but it works!

    I implemented also a filter following the example of the book, which uses a (static) InputDialog.. Maybe you know how may I implement such filter making it a dynamic filter (exactly as it happens with Ctrl+O within a java file)?

    Another question: do you know how to search the workspace for a given file? I googled for a solution and I thought something like:

    would do the trick, but it doesn't.. What I want to achieve is something like this:

    Any idea?

    Thanks again! Bye!

    Christophe Verré wrote:Eclipse Plug-ins, Third Edition is a great book to learn about Plug-ins development. The "Favorites" application made in this book would help you understand how to make your view.

    hi and thank you for your hint! Unfortunately I don't find that book that great.. It makes an example which is quite interesting and related to my problem, but it doesn't give all the instruments to understand and modify the favorites plugin.. Chapter 7 (the one about views) makes just a copy and paste of the source code with a very few (generics) comments.. So for example I didn't understand if I can use the fully qualified name (my.package.ClassName) coming from my xml to retrieve (and open upon double clicking) the corrisponding file? Is this conversion possible at all?

    thank you again!
    Hello everybody!

    I want (let's say better, I have to! ) to implement an eclipse plugin which has to receive as input an xml file having hundreds of elements of this type:

    that is, this xml file contains a lot of alias for methods of classes which are disseminated through the workspace (in several different projects). My plugin should add a new view tab to eclipse containing an (ordered) list of all the aliases. Upon double-clicking an alias, a new tab pointed at the method definition should be automatically opened (something similar to what happens when using "Open Type", but pointing to a method declaration). In addition to this, since the list is really huge, I would add the possibility of filtering the aliases exactly as it happens with method names when you click CTRL+O within a class.

    I have seen that there is a wizard to create a plugin which adds a new view to eclipse, so that can be my starting point. Furthermore, the unmarshaling of the xml should not be that difficult with JAXB. My only problem is "just" to understand how can I add the filtering function within the view and of course the behavior of automatically opening a java file (at a specified position) upon double clicking.. Maybe you know some example which may help me? Is the source code of the 2 plugins I mentioned ("open type", "search method") available?

    I will rellay appreciate any suggestion/help!
    Thank you in advance!