Archive for the 'Programming' Category

Hibernate Schema Update Problems

Saturday, July 5th, 2008

I’ve been using Hibernate for relational persistence for a while now, and I have to say it’s been working out pretty well.

That was, until I went to do a SchemaUpdate.

The moment I did an alternation of a table, or created a new entity, things went sour, and I was unable to read my old data. Was it me, or was it Hibernate?

could not initialize a collection
PSQLException: ERROR: permission denied for relation newtablejustadded

It was me.

Turns out Hibernate’s update was working just fine. There was no magic versioning or class hashing going on, detecting the change to the database.

The problem was the ACL was blank for the newly created entity table (I was using Postgres).

By issuing this command, all was fine again:

GRANT INSERT, SELECT, UPDATE, DELETE, REFERENCES ON newtable TO GROUP agroupIwasusing;

Since it took a while to figure out what was going on, I thought I’d post this to help others that follow a similar, yet frustrating, path.

Firefox Slow Page Load - Solved

Friday, March 28th, 2008

A co-worker showed me an interesting problem with Firefox today. He loaded a page from our application (running on localhost) and the page content loaded instantly, but the page load itself didn’t end until a time out 20 seconds later. Literally.

Everything we saw a measured from the browser or from the sending application showed that the content was sent in milliseconds, and the page load was just sitting there doing nothing. We were even using the latest Firefox beta.

Other browsers had no such problem.

Turns out, we figured out what was going on using the Tamper Data add-on.

Turns out there was a Connection: keep-alive in the header. When we changed it from keep-alive to close, the browser behaved as expected. That is, it loaded the page instantly.

A little web investigation showed that when you use the keep-alive attribute, you must also use Content-Length: header, which the sending application wasn’t doing.

A quick application tweak to send the content length, and everything ran super spiffy.

Now, if you don’t have access to the application that’s sending you web pages, you can twiddle with the about:config and change the network.http.keep-alive setting to false.

SQLite Functions

Tuesday, March 25th, 2008

Seems silly, but I’ve been using SQLite and was having the darnedest time try to convert an integer into a time. Problem was, I knew the functions for Sqlite3 had to be out there, but all I kept getting from search engines and exploring the developer Wiki were the C/C++ API functions, not the ones needed for SQL. I guess the way the pages are indexed, the source pages score higher than the user manual pages.

Anyhow, located the SQL functions and wanted to document their location for myself so I could locate them again later.

Seems they are called Core Functions.

They hide under the language expression page, which is under the SQL language page.

I was thrilled to find that the SQLite C Interface allowed the creation of your own functions. Brilliant!

Turns out, that the date and time functions were hiding in a different part on the Wiki.

The magic I wanted, given a time stored in an integer, was:
SELECT datetime(timevalue, 'unixepoch', 'localtime');

An Outsider’s View of Programming Documentation

Saturday, December 15th, 2007

Size Of Void StarAs a programmer, who works with programmers, writing programs that are primarily targeted for programmers, I find that I’m blessed with the ability to drop into a pseudo-language to communicate complex ideas concisely with my peers.

Occasionally, when I come up for air to deal with the rest of the world, I have to revert back to English. For me, the context switch is effortless, and occasionally I find myself using programmatic terms, like ‘context-switch’, in standard sentences.

The problem is, for a technical person, we don’t grasp what it’s like not to deal with technical information on a constant basis and then be abruptly exposed to it. Today I got to find out, and it was insightful. No wonder so may people fear their computers.

This morning, I was reviewing some basic documentation on Objective-C. Here’s the section (skim it):

Sending Messages to nil
In Objective-C, it is valid to send a message to nil—it simply has no effect at runtime. There are several patterns in Cocoa that take advantage of this fact. The value returned from a message to nil may also be valid:

  • If the method returns an object, any pointer type, any integer scalar of size less than or equal to sizeof(void*), a float, a double, a long double, or a long long, then a message sent to nil returns 0.
  • If the method returns a struct, as defined by the Mac OS X ABI Function Call Guide to be returned in registers, then a message sent to nil returns 0.0 for every field in the data structure. Other struct data types will not be filled with zeros.
  • If the method returns anything other than the aforementioned value types the return value of a
    message sent to nil is undefined.

My wife came over to see what I was reading.

Curious to know what a non-programmer’s take on this would be, I asked her to read it to me, interjecting her comments along the way. It went like this:

“Sending Messages to nil” (she paused for a moment, internally analyzing what she’d just gotten into)

“In Objective-C, it is valid to send a message to nil—” (pause) “it simply has no effect at runtime.” This sunk in for a second. “Well it it has no effect, when the heck are you sending it messages for?” She resumed composure.

“There are several patterns in Cocoa that take advantage of this fact.” Confusion set it. “Several patterns? You need more than one to have no effect?

“The value returned from a message to nil may also be valid: …” Her eyes combed the list.

“If the method returns an object, any pointer type, any integer scalar…” She hung on that word, and when I sat quietly, she continued. “…of size less than or equal to size of void star…” This was too much, “What?! Black holes can be different sizes?

Frustrated, “a float, a double, a long ” (…pregnant pause…) “double, or a long long,…” She stopped again. “A ‘long long’? You’re making this up.” She checked my face to see to see if I was pulling a fast one.

She continued, “… then a message sent to nil returns 0.” By this point it the sentence had no meaning.

“If the method returns a struct,” apparently she liked that, adding, “Oh boy! A struct”, but it was unclear if she was teasing me now.

Resuming, “as defined by the Mac OS X ABI Function Call Guide” (which again meant nothing) “to be returned in registers.” I saw a furrowed brow drop, internally wondering what cash registers had to do with any of this.

“…then a message sent to nil returns 0.0 for every…” and she stopped herself.

What?!? Zero-point-zero is different than zero? What the f***!?!” At that point she burst into an embarrassed laughter at just how mind boggling her internal monologue had gotten and refused to continue.

I had never looked at documentation from an outsider’s perspective before, it really did seem like techno-babble in that …context.

BAD IDEA: “Wanted C++/Java Programmers”

Thursday, December 6th, 2007

I’m frequently the recipient of recruiter emails that are looking for C++ and Java programmers. And, while I know both of these languages very, very well, I tend to avoid offers that words things in terms of just programming languages.

Consider a help wanted sign that said: “Wanted English Writers”

In this context it’s more obvious what’s wrong: just because you write in a particular language doesn’t mean you’re a particularly good author. And, even if you are a master at words, you might be unable to convey complex ideas to the common man very well. And, even if you can communicate with technical precision, you might not be mentally engaging. And, even if you are able to keep a reader, you might not have an interesting topic to address the masses.

There’s a reason television shows have writers, there’s a reason comedians have joke writers, and there’s a reason why books that you really enjoy are done by a small circle of authors that resonate to your liking. Mastery of a written language doesn’t necessarily make you a writer.

And that’s the fallacy that many technical companies make: they assume that because you can write in C++ or Java, that you must be smart, and clearly smart means good. Right?

Problem is, learning a computer programming language isn’t all that difficult. Learning to program well, takes experience.

Consequently, when I perform an interview with someone for a position, I’m more interested in the problem solving skills and interpersonal communication than how well they know a particular language.

And when I say problem solving skills, I don’t mean the Microsoft “why are manhole covers round” brain teasers. No, I present real code and real problems that’s representative of how my team works together.

What I’ve found is that there are actually three types of candidates that make excellent programmers, regardless of language:

  • Mathematicians - these are people who clearly have a solid grasp of data structures and algorithms.
  • Philosophers - these are people who really, truly, and deeply grasp the intricate details of logic, notation, and language.
  • Musicians - these are people who intuitively see patterns and are extremely creative.

Whenever I’ve hired from these three groups, and the person has solid communication skills, and the person had demonstrated a personal passion for software development, we’ve always had resounding success.

And the funny thing? Those talented people know other talented people.

Reasonable Trial Durations: 30/30/30

Friday, October 19th, 2007

30/30/30While doing Java development, I was looking for a new IDE for Windows. Naturally, my hunt ended with IntelliJ, with Eclipse and NetBeans close on its heels.

What started the whole chain of thought for me, though, was IntelliJ’s trial period. Frankly, I really respect a company that has enough faith in their products that they let you use them, unencumbered, for a month in order to make an informed purchasing decision.

Oddly enough, though, it wasn’t enough. Allow me to explain.

The purpose behind a trial period is to allow end users to “log enough flight time” with the product that they know whether or not it meets their needs.

And here’s the problem. I have an existing code base of inter-related projects that I need to import into the IDE. And, since this is for work, my schedule is fairly swamped. I can only come up for air to do an evaluation once every week or two just for an hour or so.

What inevitably happens is this: I install the software, validate it installs, then a week or so later, I try to import; it fails, so I table the project until I have more time. A week or two goes by, and I try again, getting closer. Then, when I come up for air and try to get a bit further, the evaluation period is over. I’ve realistically had about less than three hours using the software, and none of it in the IDE writing code.

This happened to me last year as well.

And, what’s the natural conclusion at this point? I don’t know if it will meet my needs or not. Thus, a purchase doesn’t happen.

From marketing’s perspective, they think that the following scenario is the norm: a user downloads the project, tries creating a project, slings a bit of code, gets married to the IDE, and is willing to pay to keep the experience. In fact, I’ve done just this, and I really love IntelliJ.

But, no matter how much love I have for the product, if I can’t move our corporate applications into it from an existing source base, I can’t justify the site-wide purchase. End of story.

Oh sure, I could talk with the kind folks at JetBrains and ask for an extension, and I’m sure they’d give me one.

But that isn’t the point.

Being a software provider myself, I see this as a generic problem. What if I want to produce trial software that’s fair. I can’t have my customers not being able to make a well informed decision for running out of time.

Here’s my solution… 30 days, 30 invocations, 30 hours - Whichever Comes Last

Here’s how it works:

  • You’re guaranteed at least a month of physical time.
  • You’re guaranteed at least 30 invocations.
  • You’re guaranteed at least 30 hours.
  • When all three of the above goals are hit, stop the trial.

Implementing this isn’t be hard at all. It’s also quite fair and balanced.

If you are doing real work, making use of the application for 30 days, then you’re going to quickly chew through the 30 invocations and 30 hours.

If you have just haven’t even tried the software enough, you get 30 attempts.

Finally, the 30 hour rule recognizes if you haven’t had time to actually experience the software.

I’d like to see vendors start taking this approach. It’s a good one, too. It would certainly result in more sales.

Hibernate: Duplicate Mapping and Imports

Tuesday, September 25th, 2007

<GEEK BLOG ENTRY>
I ran into a very frustrating problem this evening, causing me to stay much later than I had intended, and to miss out on some fun socializing event that I was looking forward to. Unfortunately, there was little to no useful information on the Internet as Google was coming up with few and useless results.

I hope this post saves some poor soul from the same fate.

The Problem


I’m using the Hibernate library for persistence with a JBoss EJB using JPA. My code is sprinkled with annotations, my hibernate.cfg.xml file is clean, and I have no *.hbm.xml files. My code compiles. And it runs.

However.

When I try to access something that uses the Hibernate library, I get an odd message about “Duplicate Collection Role Mapping“.

The class in question contained a Set interface and a HashSet implementation for a member.

So, I commented out this container and tried again, hoping to simplify the problem.

This time I was greeted with a “[Mappings] duplicate import” and a “DuplicateMappingException: Duplicate class/entity mapping” set of error messages.

The only related web pages was a handful of archive with people asking similar questions in various online forums.

Almost always these fell into one of three responses:

  1. You’ve got a problem with the mapping element in your hibernate config file.
  2. You’ve got annotations and and class.hbm.xml file doing something wrong.
  3. This is an old bug in JBoss.

None of the symptoms existed in my case.

Here’s How I Solved It


Turns out that Hibernate makes the recommendation that you build a HibernateUtil helper class. Inside it, you’re supposed to make a singleton of the SessionFactory (and in the case of JBoss, you should use JNDI).

A co-worker had refactored the AnnotationConfiguration() to store a single copy, however, the routine that returned it happened to call .configure() on it before returning it each time. An honest mistake, which got integrated silently into my code when I pulled the latest version from version control.

Because .configure() was being called twice, to Hibernate it did look as if I had duplicate mapping directives in my hibernate config file.

Correcting the HibernateUtil method, which handled setting up and returning the AnnotationConfiguration solved the problem.

</GEEK BLOG ENTRY>

Hibernate: No Persistance Provider

Friday, August 10th, 2007

I’ve been spending a lot of time with Hibernate, an ORM persistence manager.

After ripping Pro Hibernate 3 a new one with a scathing review, I finally have concluded that the best book on Hibernate is Java Persistence with Hibernate, which is revised edition of Hibernate In Action, though you’ll still need to reference the ERRATA for Java Persistence with Hibernate on occasion.

In the middle of page 76 it was time to go build my sample application, and all I got was a strange message that said: javax.persistence.PersistenceException: No Persistence provider for EntityManager named helloworld

[java] Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named helloworld
[java] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)

Most of the discussions on the Web lead to the conclusion that the file persistence.xml is missing, or if it isn’t, it hasn’t been placed in the META-INF directory. In my case it was present, it was in the right place, and it was correct. What gives? Seems other people were having this problem as well.

The Solution
Thanks to a szermierz’s entry on the introductory page An Introduction to Java Persistence for Client-Side Developers, which is actually a great article itself, it got me to question what libraries I had in my classpath, and whether or not that list was complete.

Turns out there was one file he listed that I didn’t have: hibernate-entitymanager.jar.

Looks important, eh?

I easily found a copy in the Hibernate Extensions at HibernateExt\tools\lib\testlibs.
There was also a copy in the Hibernate Tools at HibernateTools-3.2.0b9a\plugins\org.hibernate.exlipse_3.2.0.beta9a\lib\annotations.

Once I placed a copy in there, things worked great. So it’s not just the property file, but the .jar file that needs it.

Flex - Could Not Resolve mx:Application

Tuesday, July 31st, 2007

While looking at some Flex Examples for Flex 2 SDK, I ran into the follow error message:

Error: Could not resolve <mx:Application> to a component implementation.

My code looked like this:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">

<mx:Label text="Hello World!"/>
<mx:Label text="Hello World!" fontSize="40"/>

</mx:Application>

Problem One: Use 2006, not 2003.

However, don’t be too quick to read the message and make this simple change. Read it again! It is telling you what to use.

Error: unsupported MXML namespace found (’http://www.macromedia.com/2003/mxml’). Please use the MXML namespace ‘http://www.adobe.com/2006/mxml’ for MXML 2 source code.

Problem Two: It is no longer Macromedia, but Adobe.

The correct code that compiles looks like this:

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Label text="Hello World!"/>
<mx:Label text="Hello World!" fontSize="40"/>

</mx:Application>

Code Markup for WordPress

Thursday, July 5th, 2007

I’ve been looking for a decent code markup plugin for WordPress so that I can include source code fragments in WordPress.

Problem is, using <CODE> tag in conjuction with <PRE> injected extra blank lines ( <BR/> ) into the code.

Using Code Markup, I was able to do it.

But there was a trick…

First, the plugin requires that the <code> tag be in lowercase. Internally, I was using uppercase so it’d stand out visually to me. In theory, HTML tags ought to be case insensitive, but the filter required them. I’m going to look at this as a “good thing” since it allows me both worlds. I just wish I found it by a means other than clever guesswork.

Second, if you want spaces preserved, you need to put your code block inside of a <pre> tag. This is actually well documented on the Code Markup site.

Third and finally, do not go sprinkling HTML entities like &amp; in your code; let the filter do it for you.


Bad Behavior has blocked 2129 access attempts in the last 7 days.