我在Spring启动应用程序中使用MySQL作为数据库。我删除了数据库并再次创建它并运行Junit测试,它现在没有创建hibernate_sequence表,但它确实拥有该表。
我的实体类看起来像这样
@Entity
public class Greeting {
@Id
@GeneratedValue
private Long id;
private String text;
//getters & setters
}
我的Hibernate属性
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
我的Junit测试
@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);
}
}
答案 0 :(得分:0)
使用create
代替update
示例:
spring.jpa.properties.hibernate.hbm2ddl.auto=create
将以下参数添加到数据库URL以自动创建数据库:createDatabaseIfNotExist=true
示例:
spring.datasource.url=jdbc:mysql://localhost/testdb?createDatabaseIfNotExist=true
答案 1 :(得分:0)
我不知道,我是对还是错。我希望它能成功。
@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
}