I was trying to import a sample project in to eclipse and was facing the below given error up on running the application.
Caused by: org.hibernate.MappingExc
The reason for exception is:
Hibernate expects from underlying database to provide an auto increment feature for a given property, in your case it's id
. IOW, Oracle(your case) should support auto increment feature for a field. Oracle started to provide auto increment feature with 12c version and, as your version was less, you got that exception .
You can change .identity
by .sequence
and it will work:
GeneratedValue(strategy=GenerationType.SEQUENCE)
You can use tell Hibernate to use a sequence to generate your ID's
@Id
@Column(name = "ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")
@SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")
private int id;
This config basically tells Hibernate to use a database sequence called ID_SEQ to generate the ID's for this object. You can specify other sequences on other objects if you want other unique ID's or you can use the same sequence if you want globally unique ID's across your entire system.
The only downside to this is that can't perform batch inserts (without some further config) because Hibernate needs to get the next sequence value from the database every time, and you can't use this config if you want to use a MySQL database, because they don't support sequences.
If any of that doesn't make sense let me know and I'll explain it further.
You can just use @GeneratedValue(strategy = GenerationType.TABLE)
if you just need to be able to auto increment the value for attributes such as some ID which is primary key of your table. It worked for me. Hope it helps.
I also had the same problem I am using OracleDB, I tried Identity, GenerateSequence and nothing gave me the solution. Until I had to change the dialect in my application properties. For some reason the dialect was not generating the correct sequence, which is why I decided to use a different dialect for my Oracle12c case.
Before:
spring.jpa.database-platform = org.hibernate.dialect.Oracle10gDialect
Then:
spring.jpa.database-platform = org.hibernate.dialect.Oracle12cDialect
You could verify that you have value in the dialect of your connection maybe and changing it to a different version will be resolved like me.
my reputation is too low...
Well, I'm very grateful to JamesENL
I substituted
@GeneratedValue(strategy = GenerationType.IDENTITY)
by
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence")
@SequenceGenerator(name = "id_Sequence", sequenceName = "ID_SEQ")`
and that works fine