当我尝试使用schema
时,我遇到了共享相同ID的双向一对一Map的问题。当我没有在@Table
注解中使用schema属性时,程序会正常运行,并按照我的意愿创建SERVICES(id, description)
和USERS(name, password, application_id)
表。但是当我使用模式注解时,它显示了一些错误,并且没有创建表。有人知道在这种情况下如何使用schema吗?
ServiceJpa类:
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类:
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;
}
我使用H2数据库。application.properties文件:
# 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
错误日志的一部分:
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]
我只是尝试不使用模式注解,它可以工作,但我想知道我如何使用它
1条答案
按热度按时间t98cgbkg1#
我只需要在application.properties文件中的URL属性设置中设置INIT: