gpt4 book ai didi

java - 如何跳过某些 @Entity 类在 Spring Boot 的 h2(内存中)数据库中创建为表?

转载 作者:行者123 更新时间:2023-12-03 23:08:43 28 4
gpt4 key购买 nike

我正在尝试构建一个使用 2 个数据源的 Spring Boot 应用程序。我现在的主要数据库是内存数据库(仅用于测试目的),其中的表在我创建的 sql 文件的帮助下填充。另一个数据库 (oracledb) 具有已填充的表。

我想要达到什么目标?
我正在尝试从 oracledb 中提取数据,处理它并在 h2 数据库中填充表。

我面临的问题是什么?
我只希望将 h2 db 的实体创建为 in-mem 数据库中的表(我希望能够指定 h2 哪些实体要扫描和创建表,而不是所有具有 @Entity 注释的类)。所有具有 @Entity 的类在我的 in-mem 数据库中被创建为表,这就是我试图避免的。

我尝试了什么?
我为属于 h2 和 oracledb 的实体创建了两个单独的包,并使用 @EntityScan我只扫描了包含 h2 实体的包。这有效,但是,我需要扫描 oracledb 的其他实体,以便我能够使用它们。

我所拥有的代码片段:

我的applications.properties 文件

#spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=true

spring.h2.console.enabled=true
spring.h2.console.path=/h2


# first data source
spring.datasource.url=jdbc:h2:mem:mydb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

spring.application.name=Health Monitor


## 2nd data source
spring.production-datasource.url= jdbc:oracle://localhost:3306/db2
spring.production-datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.production-datasource.username=abcd
spring.production-datasource.password=xyz123


DemoApplication.java
package com.automation.demo;

import java.sql.SQLException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
//@EntityScan( basePackages = {"domain"} )
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}


@Bean(initMethod = "start", destroyMethod = "stop")
public org.h2.tools.Server inMemoryH2DatabaseaServer() throws SQLException {
return org.h2.tools.Server.createTcpServer(
"-tcp", "-tcpAllowOthers", "-tcpPort", "9090");
}



}

配置DB.java
package com.automation.demo.configurations;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
public class ConfigureDB {

@Primary
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSourceProperties devDataSourceProperties() {
return new DataSourceProperties();
}

@Bean
@Primary
@ConfigurationProperties("spring.datasource.configuration")
public DataSource devDataSource() {
return devDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();
}

@Bean
@ConfigurationProperties("spring.production-datasource")
public DataSourceProperties productionDataSourceProperties() {
return new DataSourceProperties();
}

@Bean
@ConfigurationProperties("spring.production-datasource.configuration")
public DataSource productionDataSource() {

return productionDataSourceProperties().initializeDataSourceBuilder()
.type(HikariDataSource.class).build();

}

}

员工.java (对于 h2 数据库)
package com.automation.demo.dev.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="test")
public class Employee {

// define fields
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private int id;

@Column(name="FirstName")
private String firstName;

@Column(name="LastName")
private String lastName;

@Column(name="Email")
private String email;


// define constructors

public Employee() {

}

public Employee(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

// define getter/setter

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

// define tostring

@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}

}


客户信息.java (这是oracledb中代表一个表的实体,我想防止在h2数据库中创建一个代表这个实体的表)
package com.automation.demo.production.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="customer")
public class CustomerInfo {

// define fields
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private int id;

@Column(name="FirstName")
private String firstName;

@Column(name="LastName")
private String lastName;

@Column(name="Email")
private String email;


// define constructors

public CustomerInfo() {

}

public CustomerInfo(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

// define getter/setter

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

// define tostring

@Override
public String toString() {
return "Customer [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}

}


如果我错过了什么,请告诉我。另外,如果我犯了任何其他错误但与我当前的问题无关,请一并提及。

谢谢。

最佳答案

Hibernate 有一种方法可以通过 SchemaFilterProvider 做到这一点.

SchemaFilterProvider

public class MySchemaFilterProvider implements SchemaFilterProvider {

@Override
public SchemaFilter getCreateFilter() {
return MySchemaFilter.INSTANCE;
}

@Override
public SchemaFilter getDropFilter() {
return MySchemaFilter.INSTANCE;
}

@Override
public SchemaFilter getMigrateFilter() {
return MySchemaFilter.INSTANCE;
}

@Override
public SchemaFilter getValidateFilter() {
return MySchemaFilter.INSTANCE;
}
}

架构过滤器

public class MySchemaFilter implements SchemaFilter {

public static final MySchemaFilter INSTANCE = new MySchemaFilter();

@Override
public boolean includeNamespace(Namespace namespace) {
return true;
}

@Override
public boolean includeTable(Table table) {
if (table.getName().equals("customer")) {
return false;
}

return true;
}

@Override
public boolean includeSequence(Sequence sequence) {
return true;
}
}

Spring 属性配置
spring.jpa.properties.hibernate.hbm2ddl.schema_filter_provider=MySchemaFilterProvider

关于java - 如何跳过某些 @Entity 类在 Spring Boot 的 h2(内存中)数据库中创建为表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60420562/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com