【问题标题】:H2 console can't find entities on my laptop but it can on othersH2 控制台在我的笔记本电脑上找不到实体,但可以在其他人身上找到
【发布时间】:2019-02-17 06:12:07
【问题描述】:

对于一个 uni 作业,我们开始学习如何使用 Spring boot、H2 控制台和 JPA,如果我不了解某些东西,我在这些主题上还很陌生,请提前道歉。

问题是我们想出了如何将实体加载到我的小组伙伴笔记本电脑上的 h2 控制台,但是当他将它推送到 git 并且我尝试在我的笔记本电脑上运行它时,h2 控制台看不到任何实体.

我在下面提供了更多信息,希望有人能够帮助我。提前谢谢!

地图结构图片

控制台输出图片

代码:

应用

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EntityScan("businessLayer")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

用户

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;
    private String password;
    private String department;

    public User() {
        super();
    }

    public User(String name, String email, String password, String department) {
        super();
        this.name = name;
        this.email = email;
        this.password = password;
        this.department = department;
    }

    public String getPassword() {
        return password;
    }

    public String getDepartment() {
        return department;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

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

    @Override
    public String toString() {
        return String.format("User [id=%s, name=%s, department=%s]", id, name, department);
    }

}

application.properties

server.port=8081

应用程序-dev.properties

spring.h2.console.enabled=true

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>CoffeeBackEnd</groupId>
    <artifactId>CoffeeBackEnd</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.0.0.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.0.0.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.12.Final</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.0.1.RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>


    </dependencies>


    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

【问题讨论】:

  • 您的应用程序是否正常启动?

标签: maven spring-boot intellij-idea h2


【解决方案1】:

按照现在的配置,H2 数据库在内存中运行。每次停止应用程序时,数据库的内容都会丢失。

此外,您的 src/main/resources 中没有 data.sql 文件,因此在您启动应用程序时不会填充 H2 数据库。

我猜你的小伙伴重启应用的时候应该和你一样的问题。

【讨论】:

  • 但是,当我们有同一个项目时,如何为他加载实体而不为我加载实体?
  • 我不太清楚。你看到你的项目和你的伙伴项目之间的控制台有什么不同吗?当您的伴侣获取项目的新副本时会发生什么?他是否承诺推动一切?
  • 他查看了控制台输出,看不出有什么不同,还说他提交了所有文件。昨天晚上他又测试了一遍,一切正常。
猜你喜欢
  • 2021-09-06
  • 2018-06-20
  • 2020-10-24
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
  • 1970-01-01
  • 2016-07-03
相关资源
最近更新 更多