Bypass GeneratedValue in Hibernate


Is it possible to bypass @GeneratedValue for an ID in hibernate, we have a case where, most of the time we want the ID to be set using GeneratedValue, but in certain cases would like to set the ID manually.

Is this possible?


I know you can do this in the JPA spec, so you should be able to in Hibernate (using JPA+ annotations).

If you just fill in the ID field of the new persistent model you're creating, then when you "Merge" that model into the EntityManager, it will use the ID you've set.

This does have ramifications, though. You've just used up that ID, but the sequence specified by the GeneratedValue annotation doesn't know that. Unless you're specifying an ununsed ID that's LESS than the current sequence value, you're going to get a problem once the sequence catches up to the value you just used.

So, maybe I can see where you might want the user to be able to specify an ID, but then you need to catch the possible Exception (duplicate ID) that may come in the future.

Unfortunately Firefox has a stupid bug here... the paths are relative to the path of the page, instead of being relative to the position of the CSS file. Which means if you have pages in different positions in the tree (like having Default.aspx in the root and Information.aspx in the View folder) there's no way to have working relative paths. (IE will correctly solve the paths relative to the location of the CSS file.)

The only thing I could find is this comment on but, to be honest, I haven't managed to make it work yet. If I do I'll edit this comment:

re: Making sense of ASP.Net Paths by Russ Brooks February 25, 2006 @ 8:43 am

No one fully answered Brant's question about the image paths inside the CSS file itself. I've got the answer. The question was, "How do we use application-relative image paths INSIDE the CSS file?" I have long been frustrated by this very problem too, so I just spent the last 3 hours working out a solution.

The solution is to run your CSS files through the ASPX page handler, then use a small bit of server-side code in each of the paths to output the root application path. Ready?

  1. Add to web.config:
 <compilation debug="true">
 <!-- Run CSS files through the ASPX handler so we can write code in them. -->
 <add extension=".css" type="System.Web.Compilation.PageBuildProvider" />

 <add path="*.css" verb="GET" type="System.Web.UI.PageHandlerFactory" validate="true" />
  1. Inside your CSS, use the Request.ApplicationPath property wherever a path exists, like this:

    #content { background: url(<%= Request.ApplicationPath %>/images/bg_content.gif) repeat-y; }

  2. .NET serves up ASPX pages with a MIME type of "text/html" by default, consequently, your new server-side CSS pages are served up with this MIME type which causes non-IE browsers to not read the CSS file correctly. We need to override this to be "text/css". Simply add this line as the first line of your CSS file:

    <%@ ContentType="text/css" %>

Make you life easy, just put images used in your CSS in the /css/ folder alongside /css/style.css. Then when you reference your images, use relative paths (e.g. url(images/image.jpg)).

I still keep images that are displayed with a <img> in an /images/ folder. Photos for example are content, they are not part of the website's skin/theme. Thus, they do not belong in the /css/ folder.

By : JohnB

This video can help you solving your question :)
By: admin