rj2700
rj2700

Reputation: 1965

Auto increment MicrosoftSQL in Hibernate (Java)

I had a question on how to change the way Hibernate (in a Java Spring boot app) sends DDL SQL statements to a Microsoft SQL DB.

I followed the guide on this site (http://therealdanvega.com/blog/2015/11/03/spring-boot-connect-to-azure-sql-server-database) to connect a Spring Boot app to an Azure MS SQL DB, which works well. The only issue is, Hibernate uses MySQL syntax by default, so while there a minute difference between the two (in terms of syntax), it's still enough to throw an error.

As an example - say you have the follow attribute in a model class:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

When connecting to an Azure MS SQL DB with this, it actually throws an error because of the CREATE TABLE statement using the AUTO_INCREMENT keyword. My question is, is there a way to change the syntax that Hibernate uses for it's SQL DDL statements?

Thanks.

Upvotes: 1

Views: 769

Answers (1)

rj2700
rj2700

Reputation: 1965

Figured this out a while ago but use this:

@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)

This (http://www.developerscrappad.com/408/java/java-ee/ejb3-jpa-3-ways-of-generating-primary-key-through-generatedvalue/) link explains why...

Upvotes: 1

Related Questions