Hibernate-sequence doesn't exist
I tried to upgrade hibernate from 4 to 5 in my project with spring 4.2
version. After this upgrade, I found the following error in my stack trace when I called a method for updating.
10:53:32,185 ERROR TableStructure:149 - could not read a hi value
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.hibernate_sequence' doesn't exist
I changed the auto incremented Id with annotation
@GeneratedValue(strategy=GenerationType.AUTO)
still the error remains.
You can also put :
@GeneratedValue(strategy = GenerationType.IDENTITY)
And let the DateBase manage the incrementation of the primary key:
AUTO_INCREMENT PRIMARY KEY
You need to set <prop key="hibernate.id.new_generator_mappings">false</prop>
.. see link and link.
Working with Spring Boot
Solution
Put the string below in .application.properties
spring.jpa.properties.hibernate.id.new_generator_mappings=false
Explanation
On Hibernate 4.X this attribute defaults to true
.
This is the reason behind this error:
It will look for how the database that you are using generates ids. For MySql or HSQSL, there are increment fields that automatically increment. In Postgres or Oracle, they use sequence tables. Since you didn't specify a sequence table name, it will look for a sequence table named hibernate_sequence and use it for default. So you probably don't have such a sequence table in your database and now you get that error.
I was getting the same error "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mylocaldb.hibernate_sequence' doesn't exist".
Using spring mvc 4.3.7 and hibernate version 5.2.9, application is made using spring java based configuration. Now I have to add the hibernate.id.new_generator_mappings
property mentioned by @Eva Mariam in my code like this:
@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
sessionBuilder.addProperties(getHibernateProperties());
sessionBuilder.addAnnotatedClasses(User.class);
return sessionBuilder.buildSessionFactory();
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.id.new_generator_mappings","false");
return properties;
}
And it worked like charm.
FYI
If you are using hbm files to define the O/R mapping.
Notice that:
In Hibernate 5, the param name for the sequence name has been changed.
The following setting worked fine in Hibernate 4:
<generator class="sequence">
<param name="sequence">xxxxxx_seq</param>
</generator>
But in Hibernate 5, the same mapping setting file will cause a "hibernate_sequence doesn't exist" error.
To fix this error, the param name must change to:
<generator class="sequence">
<param name="sequence_name">xxxxxx_seq</param>
</generator>
This problem wasted me 2, 3 hours.
And somehow, it looks like there are no document about it.
I have to read the source code of org.hibernate.id.enhanced.SequenceStyleGenerator to figure it out
in hibernate 5.x, you should add set hibernate.id.new_generator_mappings to false in hibernate.cfg.xml
<session-factory>
......
<property name="show_sql">1</property>
<property name="hibernate.id.new_generator_mappings">false</property>
......
</session-factory>
When you use
@GeneratedValue(strategy=GenerationType.AUTO)
or
@GeneratedValue
which is short hand way of the above, Hibernate starts to decide the best generation strategy for you, in this case it has selected
GenerationType.SEQUENCE
as the strategy and that is why it is looking for
schemaName.hibernate_sequence
which is a table, for sequence based id generation.
When you use GenerationType.SEQUENCE
as the strategy you need to provide the @TableGenerator
as follows.
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "user_table_generator")
@TableGenerator(name = "user_table_generator",
table = "user_keys", pkColumnName = "PK_NAME", valueColumnName = "PK_VALUE")
@Column(name = "USER_ID")
private long userId;
When you set the strategy it the to
@GeneratedValue(strategy = GenerationType.IDENTITY)
.
original issue get resolved because then Hibernate stop looking for sequence table.
Just in case someone pulls their hair out with this problem like I did today, I couldn't resolve this error until I changed
spring.jpa.hibernate.dll-auto=create
to
spring.jpa.properties.hibernate.hbm2ddl.auto=create
You can also put :
@GeneratedValue(strategy = GenerationType.IDENTITY)
And let the DateBase manage the incrementation of the primary key:
AUTO_INCREMENT PRIMARY KEY
The above answer helped me.
This might be caused by HHH-10876 which got fixed so make sure you update to:
- Hibernate ORM 5.2.1,
- Hibernate ORM 5.1.1,
- Hibernate ORM 5.0.11
I added Hibernate sequence in postgres. Run this query in PostGres Editor:
CREATE SEQUENCE hibernate_sequence
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 2
CACHE 1;
ALTER TABLE hibernate_sequence
OWNER TO postgres;
I will find out the pros/cons of using the query but for someone who need help can use this.
In my case, replacing all annotations GenerationType.AUTO
by GenerationType.SEQUENCE
solved the issue.
Run this query
create sequence hibernate_sequence start with 1 increment by 1
참고URL : https://stackoverflow.com/questions/32968527/hibernate-sequence-doesnt-exist
'Program Tip' 카테고리의 다른 글
How to get current url in view in asp.net core 1.0 (0) | 2020.09.25 |
---|---|
iOS 11 - Keyboard Height is returning 0 in keyboard notification (0) | 2020.09.25 |
How do you make an anchor link non-clickable or disabled? (0) | 2020.09.25 |
Best way to incorporate Volley (or other library) into Android Studio project (0) | 2020.09.25 |
POSTing JSON to URL via WebClient in C# (0) | 2020.09.25 |