Hibernate / SQL Server and trying to use newsequentialid

Good morning,

I have a table in SQL Server that uses newsequentialid () as the default for the primary key. Pre-Hibernate worked great, keeping keys consistent, but I can't find any configuration settings for Hibernate that allow me to keep using it.

I came across a neat solution that involves implementing an IdentifierGenerator to fetch the next sequential key (which is executed via a stored procedure) and then setting the key in my table like this:

@Id
@GenericGenerator(name = "generator", strategy = "com.test.test.helpers.PrimaryKeyGenerator")
@GeneratedValue(generator = "generator")
@Column(name="MyID", columnDefinition="uniqueidentifier", nullable=false, unique=true)
private String MyID;
      

Run codeHide result


However, the downside to this is that it now makes an additional selection every time I save a new object. I also use connection pooling, which I have confirmed I cannot auto-install this class, so I am missing out on that benefit too.

Let me know what you think and to be honest, I am very new to SQL Server and even new to Hibernate, so yes, I could very easily miss the obvious :)

Thank!

+3


source to share





All Articles