0

I am using MySQL as a database in my spring boot application. I dropped the database and created it again and ran Junit test and it is not creating hibernate_sequence table now but before it did have that table.

My entity class looks like this

@Entity
public class Greeting {

@Id
@GeneratedValue
private Long id;

private String text;
//getters & setters
}

My Hibernate properties

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.connection.sid=xe
spring.jpa.properties.hibernate.connection.characterEncoding=utf8
spring.jpa.properties.hibernate.connection.CharSet=utf8
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.LocalSessionFactoryBean
spring.jpa.properties.hibernate.id.new_generator_mappings=true

My Junit test

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@SpringBootTest
public class GreetingRepositoryDAOTest {
    @Resource
    private BaseRepository greetingRepositoryImpl;
    @Test
    public void test() {
        Greeting gr = new Greeting();
        gr.setText("Third Text");
        greetingRepositoryImpl.save(gr);
    }
}

2 Answers2

0

hbm2ddl values

Use create instead of update

Example:

spring.jpa.properties.hibernate.hbm2ddl.auto=create

Database URL

Add the following parameter to your database url to automatically create the database: createDatabaseIfNotExist=true

Example:

spring.datasource.url=jdbc:mysql://localhost/testdb?createDatabaseIfNotExist=true
Community
  • 1
  • 1
Kyle Anderson
  • 6,801
  • 1
  • 29
  • 41
  • What is the console output from Spring Boot during Application Startup? You'll see Hibernate log entries that might point to the problem. – Kyle Anderson Nov 21 '16 at 18:31
  • When I run the spring boot app there are not errors its only when I try to add data through the DAO I get the error. –  Nov 21 '16 at 18:33
  • Understood. But I'd like to see the log entries during Application Startup as they can help identify many things including: Profiles, Hibernate Configuration, etc. – Kyle Anderson Nov 21 '16 at 18:35
  • error when running Junit test - https://anotepad.com/notes/2ecwe5 Log when springboot app is run - https://anotepad.com/notes/enh83d –  Nov 21 '16 at 18:37
  • @DeepakSunandaPrabhakar I've amended my answer to include additional information. – Kyle Anderson Nov 21 '16 at 19:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128645/discussion-between-deepak-sunanda-prabhakar-and-punkrocker27ka). –  Nov 21 '16 at 19:03
0

I don't know, I am right or wrong. I hope it will be work.

@Entity
public class Greeting {

@Id
//GenerationType.IDENTITY use for create primary key
//GenerationType.AUTO use for create table without primary key
//for this reason hibernate create a table hibernate_sequence that store 
//last value (auto incremented value)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

private String text;
//getters & setters
}
Md. Maidul Islam
  • 564
  • 6
  • 10