# Friday, July 18, 2008

@CollectionOfElements, JPA, the documentation and the problems it raises

In a project that I mention a lot there's an persistent model using JPA as an interface to Hibernate.

There was case where I wanted to put extra columns in a @ManyToMany relationship. JPA cannot do that.

So I had to use the Hibernate's @CollectionOfElements.
It works like that: if there are the objects Class and Student, the extra columns go to the wrapper class EnhancedStudent. The EnhancedStudent has a property of type Student.

Now I have a few bugs related to it:
  1. Causes the @Version of the containing object (Class) to increment on EntityManager.html#find(java.lang.Class, java.lang.Object)
  2. The creation of an ExtraStudent cannot propagate (cascade) the inner Student - no matter what.
The funniest thing is that the documentation of the @CollectionOfElements is a single line:

Annotation used to mark a collection as a collection of elements or a collection of embedded objects

http://www.hibernate.org/hib_docs/annotations/api/org/hibernate/annotations/CollectionOfElements.html

:(

Gotcha MTEL, YOU CHEATING BASTARDS

From some time I noticed that Mtel (my mobile service operation) is using VoIP when I'm calling an Mtel number while roaming.
When I'm calling another operator's number while still roaming, there's no VoIP.

CHEAP BASTARDS. For these prices you can at least provide a decent service - a VoIP call is with bad quality and the callee cannot see who's calling, he sees some service number.

BUT THATS NOT ALL.

The top of the cherry is that when Mtel is using VoIP they clearly cannot see whether the other side answers or not, so no matter whether you make a successful call or not - while you're ringing you get billed. That's not right, is it?!

I found out all that today while checking my bill. Two months ago while in Brno I made a call and was quite sure that I would get billed for that call even though nobody answered.

GOTCHA, YOU CHEAP BASTARDOs


Update: other case of proved cheating:
Mtel made me pay 200 bucks for half a megabyte of internet while roaming. Before that I explicitly asked for the price. It came out that the price is 10 times more and they charge every GPRS session for a full megabyte (with 10 times the prices) without saying it.

Few months ago I caught them billing me for an international call to a friend I haven't spoken with for at least a couple of years.

# Thursday, July 17, 2008

Ctrl+Shift+T in Firefox = reopen last closed tab (window)

Ctrl+Shift+T is a popular shortcut in Eclipse.

Ctrl+T is a popular shortcut in Firefox - opens an empty tab.

Once by mistake I pressed the former by mistake and found out that Ctrl+Shift+T makes an undo for the last closed window (tab). And not only for the last tab.

Cool

Mocking an EJB Container

There are a lot of tests for some EJBs.

Starting an J2EE server is slow. I wanted to test the business logic faster.

Most of the beans reference only each other and a persistence context.

So what should the mock worry about - transaction demarcation and dependency injection. And... exception wrapping and unwrapping.

  1. First just add some setters or change the access level modifiers (from private to protected for example).
  2. Know your transaction authority.
  3. Count started transactions. If 0, begin. If 0, commit. Rollback when necessary.
  4. Listen for all the exceptions and wrap and unwrap them.
  5. Reuse as much as possible of the code.
If I have some spare time left, I'll write some code.


Serialization, cyclic references (via hashmaps) and overriding hashCode()

I'll try to simplify it.
There's an object model. In it there are cyclic references (one object references a second one, the second one - a third one, the third one - the first one).
Some of the cyclic references are through aggregations - one object has a map of other objects.
Some of the objects have a meaningful hashCode() and equals() overridden. These two depend on some properties in the object itself.

Some of the objects get serizalized/deserialized (travel through a stream).

Now here comes the problem - the deserialization first sees the cyclic reference, makes instances of all the objects, initializes all the primitive fileds, does not initialize the other fields, then links the objects.

Here comes the problem, linking two objects (one of which has a map of the other) requires hashCode(). This requires some specific properties in that object that are not initialized - this causes NullPointerException (or in my case an AssertionError).

If the hashCode returns a default value if the properties are not there - another serious problem si caused - there are objects in the map in the wrong buckets - they entered the map with the default hash, but when they got completely initialized - they now have a different hash. I think that is really bad - the map has to be rehashed.

Here's a bug detail:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4957674

Here's what some of the guys say on the subject:

The problem is that HashMap's readObject() implementation , in order to re-hash the map, invokes the hashCode() method of some of its keys, regardless of whether those keys have been fully deserialized.

And:

The fix for this is actually quite easy: Modify the readObject() and writeObject() of HashMap so that it also saves the original hash code. (I am currently using this fix in production code for a large web site.) That way, when the map is reconstructed, you don't have to recompute the hashcode----the problem is caused by recomputing the hashcode at a moment when it is not computable.

What you *give up* with this fix is that HashMaps containing Objects that don't override hashCode() and equals() will not be deserialized properly.

So basically, you have a choice: either it will be robust for classes that implement hashCode(), or it will work for bare Objects(). One or the other. I prefer the former, because people are supposed to implement hashCode().

But, not all my object have a rewritten equals (of course I can check with reflection which ones do and which ones don't, but...). This would also mean that I'm using a customized collection.


There's another proposition - to hash the hashcode.

The fix for this is actually quite easy:  Modify the readObject() and writeObject() of HashMap so that it also saves the original hash code.  (I am currently using this fix in production code for a large web site.)

The hashcode is a primitive type, so it would get initialized first and the problem would be solved. This would mean to have an hashCode() and equals() which check which one is available - the cached hash or the properties - isn't that UGLY.

I'll investigate more.
# Monday, July 14, 2008

73 meters from the ground

This is 73 meters from the ground. It is scary and physically intensive. I did this route twice, the first time without the special gloves. My fingers still hurt.



On the top the second time (on the verge of not making it).


It's over, let's have something to eat.


More pictures when I get them.

MySQL, I hate you so much [Installing mysql as a service]

The first idea was to have the DB (MySQL) start as part of the script that launched the tests. Google couldn't find anything good. MySQL is one of the most unintuitive things I have ever seen, so the decision was to not try to figure it out on my own.

The next thing was to install MySQL as a service. That turned out to be difficult.

I tried to install MySQL as a service:

/>mysqld --install

As easy as that. The problem is IT DID NOT WORK. The service did not want to start.

After some reading it came out that the RIGHT way was to do it like that:

__correct dir__/><<path>>\mysqld.exe --install MySQL --defaults-file="<<path>>\my.ini"

Why do I have to supply the mysql directory explicitly?
Why do I have to supply the my.ini directory explicitly?

Why doesn't mysqld give me an error message when I install it without the needed parameters?
Why doesn't mysqld give me an error message when I install it without the needed explicit paths?

...is beyond my inderstanding.

MySQL, you are a disgrace.
MySQL, not only are you not a real RDBMS but you can't even start without making your users' life misserable.


Google mail servers fail?! All of them?

Connection timed out on all their servers?!

Technical details of temporary failure:
TEMP_FAILURE: The recipient server did not accept our requests to connect.
[ASPMX.L.GOOGLE.com. (1): Connection timed out]
[ALT1.ASPMX.L.GOOGLE.com. (5): Connection timed out]
[ALT2.ASPMX.L.GOOGLE.com. (5): Connection timed out]
[ASPMX3.GOOGLEMAIL.com. (10): Connection timed out]
[ASPMX5.GOOGLEMAIL.com. (10): Connection timed out]
[ASPMX2.GOOGLEMAIL.com. (10): Connection timed out]
[ASPMX4.GOOGLEMAIL.com. (10): Connection timed out]

# Friday, July 11, 2008

The sources for hibernate-entitymanager.jar, version 3.2.1GA

Yea, it was difficult. So difficult that I had to extract the sources from a zipped project file, put them into archive and rename to jar file. And hope that the versions I'm using are mathing....

The JBoss jar says it's implemented by JBoss.
JBoss/hibernate-entitymanager.jar/META-INF/MANIFEST.MF:
Manifest-Version: 1.0
Product: Hibernate EntityManager
Specification-Title: JBoss
Created-By: 1.5.0_09-b03 (Sun Microsystems Inc.)
Specification-Version: 4.2.2.GA
Implementation-Vendor-Id: http://www.jboss.org/   WTF?
Version: 3.2.1.GA
Implementation-URL: http://www.jboss.org/
Ant-Version: Apache Ant 1.6.5
Implementation-Title: JBoss [Trinity]
Specification-Vendor: JBoss (http://www.jboss.org/)
Implementation-Version: 4.2.2.GA (build: SVNTag=JBoss_4_2_2_GA date=20
 0710221139)
Implementation-Vendor: JBoss Inc.

However, the JBoss source does not have the sources.
http://repository.jboss.org/hibernate-entitymanager/3.2.1.GA/
WTF number 2?


In the hibernate downloads the source is missing as a separate download
http://sourceforge.net/project/showfiles.php?group_id=40712&package_id=156160
WTF number 3?

Finally, found it in the zip file in the last link. I was desparate, thinking of using the repositories here:
http://anonsvn.jboss.org/repos/hibernate/entitymanager/tags/
Had to extract it, re-zip it and hope the versions match. So far so good.

Why does it have to be so difficult?!

# Thursday, July 10, 2008

Overriding a method with a raw type, want to use generics in the override

I want to override that api.org.hibernate.Interceptor#postFlush(java.util.Iterator)
I want to do it like that:
@Override
public void postFlush( Iterator<?> entities ) throws CallbackException {...}
Does not work - the method is not with the same signature ?!

Then:
@Override
public void postFlush( Iterator<Object> entities ) throws CallbackException {...}
Does not work - the method is not with the same signature ?! WTF?

The only thing that does work (without a silly warning) is that:
@Override
public void postFlush( @SuppressWarnings( "unchecked" ) Iterator entities ) throws CallbackException {


Why?!!

Iterating over iterator contents with a for loop

How an iterator works:

Iterator i = collection.iterator();
while( i.hasNext() ) {
 Object o = i.next();
 //do stuff
}

Well, I want to move the red line in the loop init, so it would not mingle with the loop body where only business logic would reside.

Unfortunately that cannot happen. A for loop first inits the data, then checks the condition. With an iterator one has to first check if there are elements, then to get them.

And why would someone give you an iterator instead of a collection, my mind cannot understand. (org.hibernate.Interceptor#postFlush(java.util.Iterator))
# Tuesday, July 08, 2008

JUnit, exceptions in @Before and @After methods

JUnit spec (not very easy to find) states, that if there's an exception in the @Before method, the test is not called. True. BUT

JUnit spec does not state that in this case the @After method is called.

JUnit spec also does not state that if there's an exception in both the @Before and @After methods, the second exception (guess which one) overrides the first one. In my case the first one causes the second one (the first one is ConnectionClosed or similar, the second one is a NullPointerException because the resource is not initialized).

So the real reason for the problem is lost. One will say - avoid exceptions in the @After method - that's what I just did (and the real exception was printed), but finding the reason for that was not easy and straightforward.

I'm using junit 4.4, the default runner is JUnit4ClassRunner, JUnit4ClassRunner is calling ClassRoadie.runProtected():

public void runProtected() {
    try {
        runBefores();
        runUnprotected();
    } catch (FailedBefore e) {
    } finally {
        runAfters();
    }
}

This is the reason for the mentioned override. I hate when finally does that.

There's no such thing as a natural

There's no such thing as a natural. A natural is someone who practices so much it looks like they don't need to.
# Monday, June 30, 2008

blogging from an iphone

It's not very easy, this is the first phone that is sophisticated enough to support the editing page and easy enough to be usable for that purpose. Uploading does not work. Rich editing does not work. But typing is fast enough and easy enough.

# Tuesday, June 24, 2008

Hibernate, AuditLogging, cannot configure listeners in persistence.xml

First, what's my goal
I want to do an audit log. I read the http://www.hibernate.org/318.html and there two different approaches were mentioned.
The first is using a Listener.
The second is using the new eventing model in Hibernate3.
The author said that he couldn't make the eventing approach work for an unknown reason.

I'm using Hibernate via JPA. I'm using it in an EJB. So the problem with the first approach is that it is difficult (if possilbe) to supply the same transaction to the interceptor so that it can write an entry in the audit log. There is a solution with a static field holding a session or an EM, but that seems ugly.

So I chose the second approach. I created a class, inherited few eventhandlers (PreInsertEventListener, PostUpdateEventListener ) and tried to add the class as a listener.

Using the API, it worked (the callback methods in the listeners were called):

AnnotationConfiguration configuration = new AnnotationConfiguration();

configuration.setListener( "post-update", new Test1() );


configuration.addProperties( hibernateProperties );
EntityManagerFactory factory = new EntityManagerFactoryImpl( configuration.buildSessionFactory(),
   PersistenceUnitTransactionType.RESOURCE_LOCAL, true );
EntityManager result = factory.createEntityManager();


Using properties (as if using persistence.xml) failed(the callback methods in the listeners were NOT called):

Properties hibernateProperties = new Properties();
...

hibernateProperties.put( "hibernate.ejb.event.pre-update", "package.Test1" );
hibernateProperties.put( "hibernate.ejb.event.pre-insert", "package.Test1" );
 
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.addProperties( hibernateProperties );
EntityManagerFactory factory = new EntityManagerFactoryImpl( configuration.buildSessionFactory(),
   PersistenceUnitTransactionType.RESOURCE_LOCAL, true );
EntityManager result = factory.createEntityManager();


Ideas why?
# Saturday, June 21, 2008

A new challenge - a dog

I have to keep it alive for 24 hours. It's gonna be hard




Update: It's done - I successfully returned the dog alive, unharmed and in good condition.
# Friday, June 20, 2008

Category A in my driving license

Today I passed the exam for category A (motorcycles above 50cc). Very happy.

'Sucks' category

There's a trend - there are a lot of articles here in which I bitch about something. So there should be a category called 'Sucks'.

Update: reviewing the articles I found a lot of candidates for the category, it comes out that all I'm doing is bitching about stuff - I should change the subtitle of the blog :)

@Override in eclipse

When one implements an interface, the template in Eclipse puts an @Override and it does not complain about it. Ant javac task also compiles without warnings.

Sometimes other Eclipse instances start to complain exactly for that @Override stating that there's no method that's overridden. Well, Eclipse, please do make up your mind.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Override.html
"Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message."

Well, that's not very clear. As far as I remember an Interface is a pure abstract class, right? So, which one's correct.

P.S. Sometime ago I was having a similar problem with Eclipse and generics - Eclipse only gave a warning about something (can't remember what exactly), but the javac said that it was an error - Google said something like "eclipse uses jikes, you use javac". So what, aren't there specs?!


Update: apparently JDK5 (or 1.5, suit yourself) does not allow that. So in order to get all the wrong @Overrides - set the Compliance level to 5.0 (Eclipse -> Window -> Preferences -> Java -> Compiler -> Compiler compliance level ) - and..... correct them. I have 29 left.

Referrers from live.com

All the referrers to this blog from live.com come like that:
http://search.live.com/results.aspx?q=class&form=QBHP or
http://search.live.com/results.aspx?q=interface&form=QBHP

The search word seems to be only 'class'. I can't believe that I come out in a search for only the word 'class'. I guess all the other words from the search are stripped which, if on purpose, is stupid, and, if not, ignorant.