gpt4 book ai didi

java - Hibernate InheritanceType.JOINED 阻止表创建

转载 作者:行者123 更新时间:2023-12-02 00:59:23 25 4
gpt4 key购买 nike

我有两个类,“OrderBase”和“OrderPay”,我试图为其实现“InheritanceType.JOINED”类型的继承。

虽然 TABLE_PER_CLASS 和 SINGLE_TABLE 工作正常,但一旦我将类型更改为“JOINED”,就无法创建它,而且我不明白为什么。

在切换继承类型时,我删除并重新创建了架构,以确保这不是问题。

我尝试为其实现该类型的继承的任何其他类也会发生这种情况。

我的表创建策略是“创建”:spring.jpa.hibernate.ddl-auto=创建

当将其更改为“更新”时,即使该表存在于数据库中(抛出错误之前和之后),错误仍然会存在。

我还创建了一个新项目,以确保没有其他东西阻止它运行,并且仍然会发生相同的错误。

我在 github 上分享了该项目。在本地主机上使用 mysql 创建模式的规范位于 application.properties 文件中: https://github.com/Danielmethner/brownbag

这里是类:

  1. OrderBase.java
package com.brownbag_api.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "ORDER_BASE")
@Inheritance(strategy = InheritanceType.JOINED)
public class OrderBase implements Serializable {

private static final long serialVersionUID = -3458221490393509305L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;

@NotNull
@Column(name = "AMOUNT")
private double amount;

public Order() {
}

public Long getId() {
return id;
}

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

public double getAmount() {
return amount;
}

public void setAmount(double amount) {
this.amount = amount;
}
}
  • OrderPay.java
  • package com.brownbag_api.model;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.validation.constraints.NotNull;

    @Entity
    public class OrderPay extends OrderBase {

    private static final long serialVersionUID = 4643589803146964779L;

    @NotNull
    @Column(name = "BOOK_TEXT")
    private String bookText;

    public OrderPay() {
    }

    public String getBookText() {
    return bookText;
    }

    public void setBookText(String bookText) {
    this.bookText = bookText;
    }
    }

    堆栈跟踪如下(实际上归结为“未找到父类表”)

    引起:javax.persistence.PersistenceException:[PersistenceUnit:默认]无法构建Hibernate SessionFactory;嵌套异常是 org.hibernate.MappingException:无法实例化持久器 org.hibernate.persister.entity.JoinedSubclassEntityPersister导致:org.hibernate.MappingException:无法实例化持久器org.hibernate.persister.entity.JoinedSubclassEntityPersister原因:org.hibernate.AssertionFailure:找不到表brownbag.order_base

    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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.brownbag</groupId>
    <artifactId>api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>api</name>
    <description>API for brownbag project</description>
    <properties>
    <java.version>1.8</java.version>
    </properties>

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.9.1</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
    <exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>

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

    </project>

    最佳答案

    你有一个错字。可能您继承自 javax.persistence.criteria.Order。

    如果您将 OrderPay 更改为从正确的 OrderBase 类扩展,那么一切都会按预期工作并创建表:

    public class OrderPay extends OrderBase {

    //rest of code

    }

    此外,删除此属性:

    spring.jpa.properties.hibernate.default_schema=brownbag

    您已经在 spring.datasource.url 中定义了要连接的数据库。

    关于java - Hibernate InheritanceType.JOINED 阻止表创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60901178/

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