java - Spring Boot 与 Hibernate : Keep Creating Database when already Exist

标签 java spring hibernate spring-boot thymeleaf

我正在使用 Hibernate 开发这个 Spring Boot 应用程序。当我运行 spring boot 应用程序时,当数据库或表已经存在时,我不希望 Hibernate 创建数据库或表。相反,我只是希望它从现有数据库中获取数据或使用现有数据库向其中插入/更新数据。

application.properties

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
db.driver= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = developer
spring.datasource.password = 111111

## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

图像.class

@Entity
@Table(name = "Image")
public class Image {

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
private Long imageNum;

@NotNull(message = "Product image id is required")
@Column(name = "ProductImgID")
private int productID;

@NotNull(message = "Image name is required")
@Column(name = "ImageUrl")
private String url;

Getter and Setter are below

}

BookRepository.class

@Repository
public interface BookRepository extends CrudRepository<Image, Long> {

}

BookService.class

@Service
public class BookService {

private BookRepository repository;

@Autowired
public BookService(BookRepository repository) {
    this.repository = repository;
}

public List<Image> getImage() {
    List<Image> img = (List<Image>) repository.findAll();
    if (img.size() > 0) {
        for (Image ig : img)
            System.out.println(ig.toString());
        return img;
    }
    return new ArrayList<>();
 }

}

BookService.class

@Service
public class BookService {

private BookRepository repository;

@Autowired
public BookService(BookRepository repository) {
    this.repository = repository;
}

public List<Image> getImage() {
    List<Image> img = (List<Image>) repository.findAll();
    if (img.size() > 0) {
        for (Image ig : img)
            System.out.println(ig.toString());
        return img;
    }
    return new ArrayList<>();
}

}

**正如您在图像左侧看到的那样,当我运行应用程序时,它会自动添加其中没有数据的三列并将值返回给我。 ** enter image description here

当已经存在一个数据库、表或列时,如何使 Hibernate 创建另一个数据库、表或列?然后直接从数据库中获取已有的数据?

最佳答案

将此spring.jpa.hibernate.ddl-auto属性设置为none

Configure JPA Properties

Spring Data JPA already provides some vendor-independent configuration options (such as those for SQL logging), and Spring Boot exposes those options and a few more for Hibernate as external configuration properties. Some of them are automatically detected according to the context so you should not have to set them.

The spring.jpa.hibernate.ddl-auto is a special case, because, depending on runtime conditions, it has different defaults. If an embedded database is used and no schema manager (such as Liquibase or Flyway) is handling the DataSource, it defaults to create-drop. In all other cases, it defaults to none.

关于java - Spring Boot 与 Hibernate : Keep Creating Database when already Exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58985501/

相关文章:

Java JFrame 绘制多行

spring - 使用 @ExceptionHandler 处理 spring 安全认证异常

java - @PropertySource @Value 静态字符串返回 null

java - 如果使用@NotNull,双向一对一关系将无法保存

java - 如何在 hibernate 中解决 java.lang.ClassCastException : for query. list()

java - 如何正确建模复杂的层次结构并与 hibernate 交互

java - 将 API 类转换为继承类

java - 如何使用 java 32 位与 64 位

java - 更改方法中变量的值,Java

java - 从 jsp 中的 ModelAndView 检索模型