Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!

Will Farquharson

+ Follow
since Dec 02, 2008
Will likes ...
IntelliJ IDE Java Ubuntu
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 Will Farquharson

Thanks for the response, that's exactly the kind of thing I was looking for!

I didn't see it myself as I use JPA 1.0, but this is all the more reason to move up to JPA 2.0.

I will make an analogy of the scenario we have...

Imagine you have baskets of fruit: and, one basket having many fruit.
The BasketEntity class has a Collection<FruitEntity> (@OneToMany), and FruitEntity has a reference to BasketEntity (@ManyToOne).
The FRUIT table in the database has the "BASKET_ID" column.

In the application, the user can open a Basket and see the list of Fruit in it. He can modify the list by removing Fruit or adding some completely new Fruit via a text field.

When he clicks "Save", the Basket now needs to have the new collection of Fruit in it. Some of the fruit might be the same as before, some might be new, some might be removed.

We were under the impression we could just do basketEntity.setFruit(newCollectionOfFruit), then entityManager.merge(basketEntity). However, what this does is just adds the new fruits into the database, and doesn't remove the old ones.

We have resorted to looping over the old fruit calling entityManager.remove(aFruit) in a for loop, before we do the merge mentioned above.

Was this the correct approach to take? Is there a way we can set it up so we just have to "merge-in" the new collection and it'll disregard the old one?


Perfect, thank you for the responses.

I will make everything lazy for now, and when our application becomes larger, I will carry out an optimisation task and change certain mappings to eager, or look into fetch groups as per your advice.

Also, thanks for pointing that out about the debugger -- this is exactly what I'm seeing, and it makes sense now.

I'm setting up a new J2EE project using IntelliJ IDEA 10.5, and I want to follow the guidelines for a proper project structure. I have created the complete directory structure shown at the top here:

I'm having a lot of difficulty working out what artifacts I need IntelliJ to create and which ones to deploy to Glassfish.

I'm given the choice of artifacts "JavaEE Application", "Web Application", "EJB Application".

At first, I thought I'd just need a "JavaEE Application" artifact to represent "myapp1" in that diagram. This points to an application.xml file in myapp1/myapp1-ejb/src/java/META-INF/application.xml. However, IntelliJ complains that the web resources aren't included in this artifact, and I'm not sure how to point the application.xml to the war (myapp1-war) as I prefer everything to be exploded so there isn't a war file to put inside <web-uri></web-uri>.

So, I created another artifact, this time a "Web Application" one, and pointed that to "myapp1-war". It them seems I can include this web artifact inside the JavaEE Application one. Still, I can't get any of this to deploy to Glassfish... it just throws an exception saying there were no valid components in the application.

Could someone perhaps advise me with how resolve the above, or maybe share with me how their project is set up, or better yet, provide me with an IntelliJ project that's already set up for me to examine?

I always find setting up projects and getting things to build/deploy a million times more difficult than actual coding... so thanks for any advice you can give.


EDIT: I found this:
...but this project doesn't have the recommended structure from the link further above, doesn't have a place for EJBs, and thus only uses a Web Application artifact. What I need to do is similar to this, except for a J2EE Application that will have EJBs.
I apologise, I realise I should've posted this in the Object Relational Mapping Forum, I only just realised it exists -- is it possible to have the thread moved?

I have a few questions regarding LAZY and EAGER loading on JPAs... if you can answer any of them I'd be grateful to hear your response.

1. Firstly, I can't work out if lazy loading is even working. Even if I specify FetchType LAZY on mapped entities, and use my debugger to examine the entity that came back, I can see the mapped entities all fully populated. I have a sneaky suspicion that examining the entity with the debugger has actually triggered the lazy load, but that sounds like a long-shot. How can I verify it's actually working for sure?

2. I am obsessively deciding which mappings should be EAGER and which should be LAZY. Eventually, I'll need to write a piece of code where it makes more sense to use the opposite (i.e., I need EAGER and LAZY depending on which method is finding the entity). I then need to go back and make the mappings LAZY, and write a NamedQuery to specifically eagerly fetch the things I want using the "FETCH" keyword (only way I can see to get both LAZY and EAGER on the same mapping).

The question is, am I right in being this obsessive about it? Am I really improving performance by selectively eagerly fetching things, considering the EntityManager has a cache anyway? How much worse would the performance be if I just used LAZY everywhere?

3. I'm using the Eclipselink implementation of JPA 1.0. Is the behaviour of this LAZY/FETCH stuff specific to the implementation? If so, what kind of changes are expected between them?

Thank you very much in advance,
Perfect, thank you, that pretty much wraps this one up

I wish you luck for your OCPEJBD 6! Can I ask what you are using to study?

I'm reading an O'Reilly EJB3 book cover to cover, but it puts me to sleep at times. Maybe I should be creating test projects and playing around, but I worry I'd miss out a lot of theory if I spent time doing that... and after all, the exam is all theory.
Thank you so much for taking the time to write that!

I will spend some time now reading up on CDI and the links you posted.

If you don't mind though, I still have a couple of niggling thoughts...

If you realize that you need some of the EJB features (transactions, pooling, timers, asynchronous call, WS endpoint, access through remote interface, ...)

When might you realise you need pooling? When might you not need it? If I understand correctly, it's a performance benefit with no drawbacks... so why wouldn't we just add @Stateless on all our POJOs?

On a slightly related note, I was making a start to study for SCBCD 5.0. However, from what you say, it seems Java EE 6 has more features which I really should know about, so perhaps I should consider studying OCPEJBD 6 instead. This means I'd have to study the persistence exam separately though. Would you have any advice on this? Which certification would you prefer to see a programmer with?

Thanks again!

I have a couple of small questions regarding EJB 3.0's Stateless Session Beans that I wondered if someone could help with...

I've seen applications where a Session Bean can sometimes call a mixture of other Session Beans, as well as regular Java classes (which themselves might call more Session Beans).

My question is... is there a disadvantage to getting the non-EJB classes involved here? Shouldn't these also be marked as EJBs? I'm sure that doing it this way they'd still be getting the transaction handling benefits of Session Beans, but am I right in thinking they're not getting full advantage of bean pooling and such performance related benefits on the regular classes? I know how bean pooling works, but I can't get my head round how it works if the beans call regular classes.

While I'm on the subject, I'd also like to ask...
In an application I'm involved in, we have a policy where we only create Session Beans in our application layer if we specifically need to contain everything we're doing in a transaction (updating several tables one after the other for example). Otherwise, we just implement our business logic in regular classes. The only other place we use Session Beans are to wrap up calls to the EntityManager in a DAO class way down in the persistence layer.
I guess it was decided at some point that creating Session Beans where we don't specifically need them (for wrapping everything in one transaction) is an unneeded overhead. However, I again wonder if we'd be missing out on the performance benefits of bean pools. Is this good practice? Or should we be marking everything as EJBs for the reason I mentioned?

I understand the answers to these questions are probably very specific to whatever the application is, but if anyone could offer an opinion or an explanation I'd much appreciate it.
Thanks =)
Phew, that was confusing. I fixed it by putting this in my getter for defaultFileName:

The \\+ stuff is to represent blank spaces properly.

Also of note, I had been pasting Russian characters directly into my code via my IDE for test purposes -- turns out this is a BAD idea, it turns everything into "???" but still renders them as Russian. If I restart my IDE, the same String literal has turned into "???"! Lesson learnt.

Case closed.
11 years ago

In our Struts 2 web app, we allow users to export their data to a file, which is offered to them as a download.

To achieve this, we added something like this to Struts.xml:

And a corresponding Action:

For the most part it works very well -- the front end calls that export() Action, the contents of the file is built up in "String text", then set into the inputStream, which is sent back to the Browser and offered as a file download. HOWEVER, this solution doesn't seem to support a file name with non-ASCII characters, such as Russian. The file name that comes back in the response is a load of "???" (seen through Fiddler), and Internet Explorer offers you to download "______.xml".

I've tried various suggestions I've found on Google including doing URL encoding on the file name, setting UTF-8 in various places, but nothing so far has made any difference =(

Does anyone have any suggestions I could try? I need the file to be represented properly and to retain non-ASCII characters.

Thanks in advance...

11 years ago

It seems our visitors are copying+pasting chunks of text into our site straight from Microsoft Word, resulting in those special apostrophes/quotations being stored into the database and they aren't being written out properly to the page.

I'd rather filter all these so that they just become the plain old \' marks, but I'm struggling to find a way to do this... I've tried several different regex statements using String.replaceAll(), including the unicode, the hex, the ascii value, etc, but nothing I try seems to get rid of them. Printing out an affected String in the console, the quotations look like this:

‘ and ’

There are several forum posts online where people have stuck up some code that loops over characters apparently replacing them, but none of them have worked thus far, it comes out looking exactly the same.

Can anyone suggest a proper way to get rid of these, or a regex statement that might pick them up?

I'm talking about the characters like:
u2018 Left Single Quotation Mark
u2019 Right Single Quotation Mark

Thanks in advance...
11 years ago

My friend and I have recently developed a website using Stuts 1 and JDBC. We mostly learnt as we went along, and although the website seems to be working perfectly, we know enough now to realise we should be concerned about potential concurrency issues.

The website itself isn't much - it allows users to browse business listings, search for them (basic and advanced search), and allows business owners to log in and add/edit their business listing. It basically has a load of Struts Dispatch Actions with FormBeans that get populated by service classes. The service classes themselves are singletons, although I can't remember why we decided to make them this way -- they don't have any class level variables so I guess these are okay.

The problem is, we want to be 100% certain there aren't going to be concurrency issues once our site gets busier, but we're not really sure where to start. The last time we properly dealt with concurrency problems was during University classes, and they didn't really go into detail about web applications in particular.

So, my questions are...
  • Is thread safety / concurrency going to be a problem for us given our site is made with Struts?
  • If so, where might we encounter problems, and how can we test to ensure everything is okay?
  • Is there some recommending reading you could give me that will explain what we need to know in terms of Struts 1 and concurrency? (finding it hard to find good concise information on the topic that isn't totally baffling me)

  • Thank you very much!
    11 years ago

    I'm running a fairly large web application using Struts 2 as a front end on Weblogic 10.3. I want to make parts of it modular, ie, different implemenations can be plugged in depending on requirements. I've been thinking about using Shared J2EE Libraries with Weblogic in order to do this (

    For example, there will be my core application deployed, which may have a page which is a restaurant menu for example...

    But the sections of this menu for "Starter", "Main Meal", etc, will come from the relevant web app which is running as a Shared J2EE library. The idea is, the core application doesn't need to know which implementation of "Main Meal" is there, just that it can rely on it to get a "viewMainMeal.jsp" from it to view on the overall menu page.

    This is obviously a very similifed example of what I'm trying to do, but it gets the point across.

    However, I'm struggling to work out if Struts 2 will be able to handle this kind of thing. I was hoping to be using something like Tiles to be able to build up this "main menu" page, using parts of pages it gets from the J2EE Shared Libraries. I'm not sure how this will work given that each library has its own context. I'm looking for something convenient so I can almost just say <s:include value="viewStarters.jsp"> from the main menu page, on the core application.

    I was also wondering if it's possible for the struts configuration files to inherit/extend others that are sitting on another web app (in my case, the Shared Library ones)? I wouldn't like to have to repeat all my Interceptors and stuff on each library, but I can't think of a way to say extends="struts-core.xml" because it would have no idea where struts-core.xml is since it's away off in my core application.

    I'm going to continue reading about Struts 2 in the meantime but if anyone at all has had any experience with what I'm trying to do, I'd love to hear you're input or ideas.

    Thank you!
    12 years ago
    That link is just to a Web Service testing page that usually appears if you click "Test Web Service" from the right click menu in Netbeans - I'm not sure what it does behind the scenes with files etc, but it usually just displays a list of all the methods you have and lets you invoke them to see if they are work. I thought the fact that this page didn't work remotely might be a good indicator as to why my client can't consume the web service either.... I could be wrong though.
    13 years ago