使用模式在主键共享情况下

huangapple go评论59阅读模式
英文:

Using schema in a primary key sharing situation

问题

ServiceJpa类和UserJpa类中的@Table(schema = "company", name = "SERVICES")@Table(schema = "company", name = "USERS")注释是用来指定数据库中表的模式(schema)和名称(name)。您遇到的问题是与模式相关的。

在使用schema注释时,您需要确保数据库中已经存在名为"company"的模式(schema)。如果模式不存在,您需要在数据库中创建它,然后再运行应用程序。这可以通过执行SQL命令来完成,如下所示:

CREATE SCHEMA IF NOT EXISTS company;

然后,当您的模式存在于数据库中,您的应用程序应该能够成功创建名为"SERVICES"和"USERS"的表格,这些表格将属于"company"模式。

总之,要使用schema注释,确保模式已存在于数据库中,然后您的应用程序应该能够正常工作。希望这能帮助您解决问题。

英文:

I'm having problems with a bidirectional one-to-one mapping that share the same Id when I try to use a schema. When I don't use the schema attribute in the @Table annotation the program runs normally and creates the SERVICES(id, description) and USERS(name, password, application_id) tables just as I would like. But when I use the schema annotation it shows some errors and doesn't create the tables.
Does anyone know how to use schema in this situation?

ServiceJpa class:

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Getter
@Setter
@Entity
@Table(schema = "company", name = "SERVICES")
public class ServiceJpa {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	private String description;
	
	@OneToOne(cascade = CascadeType.ALL)
	@PrimaryKeyJoinColumn
	private UserJpa user;
	
}

UserJpa class:

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Getter
@Setter
@Entity
@Table(schema = "company", name = "USERS")
public class UserJpa {
	
	@Id
	private Long id;
	
	private String name;
	
	private String password;
	
	@OneToOne(mappedBy = "user")
	@MapsId
	private ServiceJpa application;
	
}

I'm using H2 database.
application.properties file:

# datasource
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:banco
spring.datasource.username=sa
spring.datasource.password=

# jpa
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

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

Part of error log:

2023-05-20 11:30:29.011  WARN 2508 --- [           main] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "create table company.services (id bigint generated by default as identity, description varchar(255), primary key (id))" via JDBC Statement

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table company.services (id bigint generated by default as identity, description varchar(255), primary key (id))" via JDBC Statement

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "COMPANY" not found; SQL statement:
create table company.services (id bigint generated by default as identity, description varchar(255), primary key (id)) [90079-214]

I just tried not using the schema annotation and it works, but I want to know how do I use it

答案1

得分: 0

我刚刚不得不在application.properties文件的datasource url属性设置中设置INIT:

# 数据源
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:banco;INIT=CREATE SCHEMA IF NOT EXISTS company;
spring.datasource.username=sa
spring.datasource.password=
英文:

I just had to set the INIT in the datasource url property setting in application.properties file:

# datasource
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:banco;INIT=CREATE SCHEMA IF NOT EXISTS company;
spring.datasource.username=sa
spring.datasource.password=

huangapple
  • 本文由 发表于 2023年5月20日 22:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定