Showing posts with label ejb3. Show all posts
Showing posts with label ejb3. Show all posts

Thursday, February 26, 2009

Hibernate: QueryException: could not instantiate: (updated)

I was doing some queries today in EJB-QL on JBossAS (which uses Hibernate as persistence provider).
And for a query that was nicely working in the Jopr Gui, I got errors in the test suite:


javax.ejb.EJBTransactionRolledbackException: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not instantiate: com.acme.Foo
at ...... ( a lot ) ....
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not instantiate: com.acme.Foo


Ok nice - Hibernate can not instantiate my class because it can not instantiate my class. So what?

After looking some time at this and debugging around I found out that the issue was the following. But before I show you the code, let me assure that IntelliJ flagged it as valid and it did work in the UI.

But now the code:


SELECT new com.acme.FOO(res.name, res.id, ... , parent.name, parent.id)

FROM Resource res

LEFT OUTER JOIN res.parentResource parent

...

WHERE ...


The constructor of com.acme.Foo looked like this:


Foo(String res.name, int res.id, ... , String parentName, int parentId) {}


This is all valid,

but:

It can happen for Resources at the root of the hierarchy to not have a parent (when they are roots). And in this case, parent is null and Hibernate is looking for a constructor to pass NULL in as last argument, which does not fit for 'int'. Thus the failure to instantiate the class.

The correct constrctor looks like this:

Foo(String res.name, int res.id, ... , String parentName, Integer parentId) {}

(of course you need to update the respective property within Foo too).

Conclusion:
Just to get it straight: Hibernate is doing the right thing - it's only not easy to find out what is going wrong.




Thursday, May 08, 2008

Sample app of my ejb3 book updated (German)

I have just updated the sample app + installation guide of the sample app from my ejb3 book to use JBoss 4.2.2.GA. This makes the installation much simpler. I also removed a few issues from various places.

You can the updated description on the books web page.

Remember, even as there is the free PDF version of the book, it still can be ordered in print.

Wednesday, April 16, 2008

Free PDF version of my German EJB 3 book available

I am very pleased to announce that dpunkt.verlag, my publisher for my EJB-3 and JBoss books, has made a free PDF version of my German EJB-3 book available for download.



Cover of my EJB 3 book



You can download it from the book site. Please take a few seconds to fill in the survey.

[ There were access issues on the server - please retry the download ]

The book is of course still available in printed form - e.g. at Amazon.de.

Many thanks go to the nice folks at dpunkt.

Saturday, March 01, 2008

EJB 3.1 first public draft available

The EJB 3.1 expert group published the first draft of the upcoming EJB 3.1 spec.

New stuff in EJB 3.1 (in relation to 3.0) is:

- no interface view for session beans: here you need no local interface anymore in order to expose the beans methods to local clients. Of course a remote interface is still needed for remote access. This no interface view is (also) convenient if you want to expose a bean as web service a la JSR-181.
- singleton session beans: Those are basically session beans that can be accessed as such by clients, that exist like singletons in the pure Java SE case
- calendar based expressions for timer creation: It is now possible to create timers where the fire date is given by a cron like syntax. The timeout method can be speficied by @Schedule. If @Schedule is augmented with the cron like date, the system will automatically create a timer for it.
- allowing to package ejbs in a .war
- asynchronous invocations of session beans: A sssion bean method marked with @Asynchronous can be be called by a client via the ususal session bean invocation semantics (remote, local, no-interface) but will run asynchronously like the onMessage() method of a MDB, but doesn't need e.g. JMS for this. It is for the client possible to retreive results via a Future object.
- a few clarifications over the old standard

This looks very promising to me.

The draft is available from the spec space at JCP.org. Unfortunately only the full spec document is available and not the simplified one as in EJB 3.0.

Tuesday, February 19, 2008

Be careful with @OneToOne and lazy loading

When modelling an entity one sometimes wants to have information in a separate table even if it would exist only one time for a given entity. Think of a 1:0...1 composition. This could be handled as embedded in the primary entity, but perhaps you don't want to do this as the dependent object is huge and you don't always need it. The dependent table could even live in a different table space for that reason. Or just for relational normalization reasons.

For the @xxxToOne relations, JPA 1.0 spec sees Eager loading as default. One can give a *hint* that the dependend end should be lazily loaded, but this is only a hint.

Now when you have an optional @OneToOne relation, Hibernate (at least) needs to do an additional select for that relation to find out if the dependend object is null or not. For a mandatory (1..1) relation it can just install a proxy to lazily load the relation, but this is not possible for 1...0..1.
See e.g. http://www.hibernate.org/162.html for an explanation.

Friday, February 16, 2007

Wednesday, February 14, 2007

Timestamp comparision oddities

Yesterday a colleague of mine ran into a problem where code like this produced a ClassCastException:

.. av.getEndTime().compareTo(someDate);

av is a domain class in JPA with endTime like this

@Temporal(TemporalType.TIMESTAMP)
java.util.Date endTime

and someDate being a java.util.Date.

This just worked on my Mac, but failed for one colleague, while others had no problem.
Looking under the covers turned out that av.getEndTime() actually returned a java.sql.Timestamp, which in java versions between 1.5.0_00 and 1.5.0_06 (including) were suposed to throw a CCE when the argument of compareTo() is a Date. Interstingly before() and after(), which also compare, did not have this restriction.
In Java Version 1.4.x the CCE was also not present and SUN seems to have it changed back to the original behaviour in 1.5.0_07. Unfortunately the manual page was not updated, adding to the confusion.
This blog entry also talks about this problem. Sun has a list of page about incompatibilities where this problem is listed. Unfortunately, the original bug report is not accessible (to me).

Some people argue that one should write own comparators, but I think it is easier to update to a newer version of the JDK. Of course if the software gets shipped to customers, one needs to make sure the customer also has 1.5.0_07 at least.

Saturday, February 10, 2007

EJB3 alpha feature from IBM / Bea compliant

While in the past big blue was not too enthusiastic about EJB3, they now have an alpha version of EJB 3 support online.
It only works with WAS 6.1, but neverthless.

This means that EJB3 support is very widthspread now so that it will definitively become a widely accepted standard.
It is also interesting that vendors that haven't really been on the early adopters radar like SAP have one of the first certified implementations.
[update]
Bea announced that they are now also Java EE 5 compliant. See the post on TheServerSide.com. So Java EE and EJB 3 support by the big vendors is now also coming along. As many others I think this is a good thing even if I would have liked JBossAS to be the first.

Monday, February 05, 2007

Amazon finally has it - or not?

After I have my first copy of the EJB3 book since the 23th and I got my box of author copies, even Amazon now lists it as available :)
Man what a tough time waiting :-)

Another look at the Amazon page and it is listed as non available again. This sucks *sigh*

Tuesday, January 23, 2007

Yippie!

I just got a copy of my EJB3 book in my mailbox. It is again a great feeling to actually have the printed copy in the hands.
One can get sort of a feeling when one print it out on the laser printer, but having the real book is still different.

Amazon still shows it as unavailable, but this should change within the next few days. All those who preordered a copy will also soon get it.

I still need to add a few things to the accompanying web page, but I'll do this in the next few days.

Here is a picture with my copy:




Technorati:

Monday, January 15, 2007

EJB 3 book, talklets at Stuttgart Java User Group

My EJB 3 book will probably be in stores starting on 26th of this month.

Within the next month, the Java User Group Stuttgart will host two EJB 3 talklets

  • 25.1.06: Sun evening - presentations about Java SE 6 and EJB 3.0 from a Sun computers perspective.
  • 15.2.06: Broader look into EJB 3 - presentation about EJB 3, existing Appserver and some tips and tricks on using EJB 3

As usual, everyone is invited to join those sessions.


Technorati:

Tuesday, December 05, 2006

EJB 3 book in typesetting phase

It's nearly done .. last night I finished to work in the results of the spell checking.
Now the book is in the typesetting phase after which it will go to the printer.
Expect it to be in a store near you end of January / start of February.

*smile*

Thursday, November 23, 2006

Final cover of my EJB-3-book online

As I wrote before, the final draft of my book is at dpunkt.
They now also have the final cover design online. Unfortunately, Amazon is no yet this fast as you can see here

Wednesday, November 15, 2006

Final draft of my ejb3 book at the publisher

I just sent the final draft of my book to the publisher.
Next steps are professional spell checking, type setting and printing. So I expect it to be in stores end on January 2007.
Stay tuned :)

Friday, August 18, 2006

Draft of my next book is at the publisher [updated]

I just submitted the draft of my upcoming next book to the publisher to start the review process.
You might wonder what it is about. Well it is about EJB 3.0 and targeted at readers who are already familiar with EJB 2.x and want to know the changes without repeatedly reading things that they already know about.
As there is no final title or cover etc, I can't give them at the moment. Stay tuned, as I will post some more later on.

Dpunkt.verlag is again my publisher, so you might also check out their page about upcoming titles for the anouncement and a link to the final page.

[Update]: Dpunkt has the book description (with the non-final cover) now online

Saturday, January 28, 2006

EJB3 session visibility

EJB3 started with the claim to reduce the artefacts needed to program with EJBs. Unfortunately if you want to expose a SessionBean locally *and* remotely, you start to either list all business methods in a POJI for the local interface and the same again in an interface for the remote view. If you update the session bean class by a new method, you need to remember to update both interface files (this calls again for generators like Xdoclet ...)

Another variant to acheive the same result is by using the following view pattern:



In this case you only list the business methods in the "Business" interface. IRemote and ILocal are only marker interfaces that just extend the "Business" interface and which carry the respective annotation:


package foo;

import javax.ejb.Local;

@Local
public interface ILocal extends Business {
// empty
}


This way extending the session bean by new methods just means to update one additional file. Unfortunately this yields again four artefacts for this task, which is far from simple.

Are there better ways to do this?

Wednesday, December 21, 2005