Archive

Archive for the ‘Code’ Category

Hibernate Annotations and Lazy Associations for OneToOne and ManyToOne mappings

June 14th, 2009 admin No comments

By default, all Hibernate ToOne assocations are treated as Eager. This is because “Lazy fetching is only conceptually possible for a mandatory association since we have to hit the other table to determine whether the association is null or not!”

When using Hibernate Annotations, the critical bit is optional=”true”

Therefore, you need to map a OneToOne as @OneToOne(fetch=FetchType.LAZY, optional=false) To get lazy loading. The optional part is key, that way Hibernate does not need to check the other side of the association for nulls, since it knows in this case it cannot be a null. So it’s safe to put a proxy there, and proxies are how Hibernate does its lazy loading magic.

The bit that confused me was that if there is a @NotFound(action = NotFoundAction.IGNORE), that will still cause the relation to be EAGER, even if you’ve mapped it as LAZY and optional=false. You must have NotFoundAction.EXCEPTION if you have a NotFoundAction defined at all. This is clear once you think about it (you would be contradicting the optional=false in the ToOne mapping), but it took me a bit to realize this was the case.

Mozilla Firefox Multiple Request Behaviour with background:url() Style

April 25th, 2009 admin No comments

Here’s an odd one that took me quite a long time to figure out.  There were a few styles on a page I was working on which were using background:url() to overwrite a background image, instead of correctly specifying background-image:none.

Well in addition to looking generally ugly in your code, this CSS hack has another much nastier component: it causes Firefox to request your page more than once.  For instance, if you are looking for /myform.html and using a POST, if you watch the traffic coming from the browser, you’ll set a POST for /myform.html and later, a GET for /myform.html.  If you use a GET for /mypage.html, you’ll see a GET for /mypage.html and later another GET.  In the cases where you are using a POST especially, this can be some pretty nasty behavior as you can imagine.

Firefox will not request the page once per background:url() appearance in your code however, it will simply request the page twice until all instances of background:url() are removed.

This behavior was tested on various 2.0 forms on Linux and Windows, I’m unsure of the behavior with 1.5 and prior browsers.  I have not tested it yet in 3.X versions.

Categories: Code, CSS Tags:

Writing text over a input type image using CSS

April 16th, 2009 admin No comments

This was one of those things I’d wanted to figure out, but I hadn’t sat down to finally come up with a workable solution. When I did, it seemed so simple, but I hadn’t read about it anywhere else so I thought I’d put it out there for other people to use.

The basic idea is it’s nice to have images for things like submit buttons, but it’s equally annoying to generate as many of these as you have text to put over them. Wouldn’t it be nice to just use the same image but vary the text which is displayed? Of course. And clearly you can do something by putting a background-image over any div and having JavaScript trigger the form submit, but that is a little ugly in my opinion. You always would have to remember to include the form submit code in your page, instead of simply using the div as you would a standard input type=”image”.

Well it turns out you can have both. All that you really need to do, is put the image you want to use as a submit button into the background of a div, as you might before. And put the text you want over the image as the content of that div. The next part is the trick, and it’s a simple one: using absolute positioning, put a input type=”image” right over the top of that div. But, just use a clear gif as the image. Viola! Now you can see and read the text and the image behind it, but when you click on it, you’re really just clicking on the submit button. Just make sure the container is position:relative so that it works in Internet Explorer as well as FireFox. Also note we’re sizing the input at 100%, so that all we really need to size is the image part, making that class reusable for different sizes of submit image.

About the only downside I can see to his technique is that you can’t easily copy the text on the submit button, since you’ll be selecting the clear gif sitting on top of it. But, that really doesn’t bother me much. And you would not be able to copy text that sat inside of an image in any event.

Markup is like this:

<div class="image-submit-container image-submit-big">
Continue
<input type="image" name="image-button" value="Continue" src="spacer.gif"/>
</div>

And styles are like this (flavor to taste):

.image-submit-container {
	color:#FFFFFF;
	font-weight:bold;
	position:relative;

}
.image-submit-container input {
	width:100%;
	height:100%;
	display:inline;
	float:left;
	position:absolute;
	top:0px;
	left:0px;
}
.image-submit-big {
	background: transparent url(btn_blank_big.gif) no-repeat 0 0;
	height:36px;
	width:190px;
	padding-top:6px;
}
Categories: Code, CSS Tags: