gpt4 book ai didi

java - 如何在java中从H2数据库映射id列

转载 作者:太空宇宙 更新时间:2023-11-04 10:26:48 25 4
gpt4 key购买 nike

我正在尝试创建一个数据表,并使用 H2 内存数据库将该表映射到一个实体。我正在使用 spring-boot (版本 2.0.2)和 Flyway 在运行之前创建数据库。

由于某种原因,当 hibernate 要根据架构进行验证时,我无法成功映射标识列。

建议?

表:

CREATE TABLE T_ADDRESS (
ID BIGINT(19) PRIMARY KEY,
CREATION_TIME TIMESTAMP NOT NULL,
CREATED_BY VARCHAR(50) NOT NULL,
UPDATED_TIME TIMESTAMP NOT NULL,
UPDATED_BY VARCHAR(50) NOT NULL,
ADDRESS_LINE_1 VARCHAR(50) NOT NULL,
ADDRESS_LINE_2 VARCHAR(50),
ADDRESS_LINE_3 VARCHAR(50),
CITY VARCHAR(50) NOT NULL,
COUNTRY VARCHAR(7),
ZIP_CODE INTEGER(4) NOT NULL
);

实体:

@Entity
@Table(name = "T_ADDRESS")
public class AddressEntity extends PersistentEntity {

@Id private Long id;

@Column(name = "ADDRESS_LINE_1", nullable = false) private String addressLine1;
@Column(name = "ADDRESS_LINE_2") private String addressLine2;
@Column(name = "ADDRESS_LINE_3") private String addressLine3;
@Column(name = "CITY", nullable = false) private String city;
@Column(name = "COUNTRY") private String country;
@Column(name = "ZIP_CODE", nullable = false) private Integer zipCode;

...
}

映射的父类(super class):

@MappedSuperclass
public abstract class PersistentEntity {

@Column(name = "CREATION_TIME") private LocalDateTime creationTime;
@Column(name = "CREATED_BY") private String createdBy;
@Column(name = "UPDATED_TIME") private LocalDateTime updatedTime;
@Column(name = "UPDATED_BY") private String updatedBy;

...
}

Maven 依赖项:

    <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-web</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

应用程序属性:

spring.datasource.url=jdbc:h2:mem:jactor
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=validate

我已经尝试了每种GenerationType(@GenerateValue中的策略)

当 spring-boot 应用程序启动时,当 hibernate 将验证架构时,应用程序上下文会失败:

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at ...
Caused by: org.hibernate.AnnotationException: Unknown Id.generator: address_seq
at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:665) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at ...
org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:888) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]

我继续前进,但没有解决这个问题。我已经为这个问题创建了一个方面驱动的解决方案,如果没有提供,它将输入一个 id 的序列号。

我知道这是一个脆弱的解决方案,但我可以继续前进并专注于更迫切的需求。这仍然是一个应该解决的问题。我使用以下解决方案更新了 git-hub 上的存储库:https://github.com/jactor-rises/jactor

最佳答案

请通过this并搜索 GenerationType.SEQUENCE。

根据阅读进行扣除:

@Id @GeneratedValue(generator = "address_seq", strategy = GenerationType. SEQUENCE) 
@SequenceGenerator(name="<name of schema in H2>", sequenceName = "address_seq", allocationSize=1)
private Long id;

关于java - 如何在java中从H2数据库映射id列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50447830/

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