使用 Spring JPA 保存实体时外键列中的空值
Null values in foreign key columns when saving an entity with Spring JPA
在我的数据库中,我有 2 个实体,它们之间存在单向关系。
用户:
CREATE TABLE public."user" (
uuid uuid NOT NULL DEFAULT uuid_generate_v4(),
"name" varchar NULL,
CONSTRAINT user_pk PRIMARY KEY (uuid)
);
留言:
CREATE TABLE public.message (
sender uuid NOT NULL,
receiver uuid NOT NULL,
"content" varchar NULL,
uuid uuid NOT NULL,
CONSTRAINT message_pk PRIMARY KEY (uuid),
CONSTRAINT message_fk FOREIGN KEY (sender) REFERENCES "user"(uuid) ON UPDATE RESTRICT ON DELETE RESTRICT,
CONSTRAINT message_fk_1 FOREIGN KEY (receiver) REFERENCES "user"(uuid) ON UPDATE RESTRICT ON DELETE RESTRICT
);
这就是我使用 Spring JPA 映射这些实体的方式:
@Entity
@Table(name = "user", schema = "public")
public class User
{
@Id
@Column(name = "uuid", insertable = false, updatable = false)
private UUID uuid;
@Column(name = "name")
private String name;
}
@Entity
@Table(name = "message", schema = "public")
public class Message
{
@Id
@Column(name = "uuid")
private UUID uuid;
@ManyToOne
@JoinColumn(name = "uuid", insertable = false, updatable = false) // this uuid is in the User entity
private User sender;
@ManyToOne
@JoinColumn(name = "uuid", insertable = false, updatable = false) // this uuid is in the User entity
private User receiver;
@Column(name = "content")
private String content;
}
在我的 REST 控制器中,我公开了一个端点来创建消息。这个端点只需要两个用户(发送者和接收者)和消息的内容:
@PostMapping
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<CreateMessageResponseRO> createMessage(@RequestBody CreateMessageRequestRO request)
{
Message message = new Message();
User sender = userRepository.findById(UUID.fromString(request.getSender())).get();
User receiver = userRepository.findById(UUID.fromString(request.getReceiver())).get();
String content = request.getContent();
message.setSender(sender); // sender is not null here
message.setReceiver(receiver); // receiver is not null here
message.setContent(content);
messageRepository.save(message);
return ResponseEntity.ok(new CreateMessageResponseRO(message));
}
我已经用调试器检查过
messageRepository.save(message)
这两个用户(sender
和 receiver
)不为空并且他们有一个有效的 UUID(即这些 UUID 指的是 2 个不同的现有用户)但是,当我调用该端点时我得到这个例外:
org.postgresql.util.PSQLException: ERROR: null value in column "sender" of relation "message" violates not-null constraint
Detail: Failing row contains (null, null, hello world!, 818ea408-fa40-49f8-93a1-85e2ff026dac).
这是我正在使用的JSON:
{
"sender": "75090bb0-0e29-4cd8-84ab-2bf2727419d2",
"receiver": "3539c23f-42f8-4074-a5f8-a8d3f0be8aeb",
"content": "hello world!"
}
当 userRepository
正确找到 2 个用户时,为什么我会得到那个异常?
编辑 1
我已尝试从 Message
实体的 @JoinColumn
注释中删除属性 insertable = false, updatable = false
。这是更新后的代码:
@Entity
@Table(name = "message", schema = "public")
public class Message
{
@Id
@Column(name = "uuid")
private UUID uuid;
@ManyToOne
@JoinColumn(name = "uuid")
private User sender;
@ManyToOne
@JoinColumn(name = "uuid")
private User receiver;
@Column(name = "content")
private String content;
}
但是,现在我在启动应用程序时出现异常,提示它需要我已删除的属性:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.akwares.sccs.entity.Message column: uuid (should be mapped with insert="false" update="false")
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=19=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.5.jar!/:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.5.jar!/:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.5.jar!/:5.3.5]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1313) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-2.4.4.jar!/:2.4.4]
at com.akwares.sccs.Application.main(Application.java:11) ~[classes!/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[server-1.0-SNAPSHOT.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:107) ~[server-1.0-SNAPSHOT.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[server-1.0-SNAPSHOT.jar:na]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[server-1.0-SNAPSHOT.jar:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.akwares.sccs.entity.Message column: uuid (should be mapped with insert="false" update="false")
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.5.jar!/:5.3.5]
... 25 common frames omitted
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.akwares.sccs.entity.Message column: uuid (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:862) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:880) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:902) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:634) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:267) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:354) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:298) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.5.jar!/:5.3.5]
... 29 common frames omitted
我认为您的 @JoinColumn
名字有问题。对我来说,你应该使用不同的名称并使用 referencedColumnName
来引用外列名称
@ManyToOne
@JoinColumn(name = "sender_uuid", referencedColumnName = "uuid", insertable = false, updatable = false) // this uuid is in the User entity
private User sender;
@ManyToOne
@JoinColumn(name = "receiver_uuid", referencedColumnName = "uuid", insertable = false, updatable = false) // this uuid is in the User entity
private User receiver;
删除 insertable = false, updatable = false
。这是告诉 JPA 将此列从生成的 SQL 语句中删除。
在我的数据库中,我有 2 个实体,它们之间存在单向关系。
用户:
CREATE TABLE public."user" (
uuid uuid NOT NULL DEFAULT uuid_generate_v4(),
"name" varchar NULL,
CONSTRAINT user_pk PRIMARY KEY (uuid)
);
留言:
CREATE TABLE public.message (
sender uuid NOT NULL,
receiver uuid NOT NULL,
"content" varchar NULL,
uuid uuid NOT NULL,
CONSTRAINT message_pk PRIMARY KEY (uuid),
CONSTRAINT message_fk FOREIGN KEY (sender) REFERENCES "user"(uuid) ON UPDATE RESTRICT ON DELETE RESTRICT,
CONSTRAINT message_fk_1 FOREIGN KEY (receiver) REFERENCES "user"(uuid) ON UPDATE RESTRICT ON DELETE RESTRICT
);
这就是我使用 Spring JPA 映射这些实体的方式:
@Entity
@Table(name = "user", schema = "public")
public class User
{
@Id
@Column(name = "uuid", insertable = false, updatable = false)
private UUID uuid;
@Column(name = "name")
private String name;
}
@Entity
@Table(name = "message", schema = "public")
public class Message
{
@Id
@Column(name = "uuid")
private UUID uuid;
@ManyToOne
@JoinColumn(name = "uuid", insertable = false, updatable = false) // this uuid is in the User entity
private User sender;
@ManyToOne
@JoinColumn(name = "uuid", insertable = false, updatable = false) // this uuid is in the User entity
private User receiver;
@Column(name = "content")
private String content;
}
在我的 REST 控制器中,我公开了一个端点来创建消息。这个端点只需要两个用户(发送者和接收者)和消息的内容:
@PostMapping
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<CreateMessageResponseRO> createMessage(@RequestBody CreateMessageRequestRO request)
{
Message message = new Message();
User sender = userRepository.findById(UUID.fromString(request.getSender())).get();
User receiver = userRepository.findById(UUID.fromString(request.getReceiver())).get();
String content = request.getContent();
message.setSender(sender); // sender is not null here
message.setReceiver(receiver); // receiver is not null here
message.setContent(content);
messageRepository.save(message);
return ResponseEntity.ok(new CreateMessageResponseRO(message));
}
我已经用调试器检查过
messageRepository.save(message)
这两个用户(sender
和 receiver
)不为空并且他们有一个有效的 UUID(即这些 UUID 指的是 2 个不同的现有用户)但是,当我调用该端点时我得到这个例外:
org.postgresql.util.PSQLException: ERROR: null value in column "sender" of relation "message" violates not-null constraint
Detail: Failing row contains (null, null, hello world!, 818ea408-fa40-49f8-93a1-85e2ff026dac).
这是我正在使用的JSON:
{
"sender": "75090bb0-0e29-4cd8-84ab-2bf2727419d2",
"receiver": "3539c23f-42f8-4074-a5f8-a8d3f0be8aeb",
"content": "hello world!"
}
当 userRepository
正确找到 2 个用户时,为什么我会得到那个异常?
编辑 1
我已尝试从 Message
实体的 @JoinColumn
注释中删除属性 insertable = false, updatable = false
。这是更新后的代码:
@Entity
@Table(name = "message", schema = "public")
public class Message
{
@Id
@Column(name = "uuid")
private UUID uuid;
@ManyToOne
@JoinColumn(name = "uuid")
private User sender;
@ManyToOne
@JoinColumn(name = "uuid")
private User receiver;
@Column(name = "content")
private String content;
}
但是,现在我在启动应用程序时出现异常,提示它需要我已删除的属性:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.akwares.sccs.entity.Message column: uuid (should be mapped with insert="false" update="false")
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=19=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.5.jar!/:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.5.jar!/:5.3.5]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.5.jar!/:5.3.5]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1313) ~[spring-boot-2.4.4.jar!/:2.4.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-2.4.4.jar!/:2.4.4]
at com.akwares.sccs.Application.main(Application.java:11) ~[classes!/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[server-1.0-SNAPSHOT.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:107) ~[server-1.0-SNAPSHOT.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[server-1.0-SNAPSHOT.jar:na]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[server-1.0-SNAPSHOT.jar:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.akwares.sccs.entity.Message column: uuid (should be mapped with insert="false" update="false")
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.5.jar!/:5.3.5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.5.jar!/:5.3.5]
... 25 common frames omitted
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.akwares.sccs.entity.Message column: uuid (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:862) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:880) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:902) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:634) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:267) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:354) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:298) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.29.Final.jar!/:5.4.29.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.5.jar!/:5.3.5]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.5.jar!/:5.3.5]
... 29 common frames omitted
我认为您的 @JoinColumn
名字有问题。对我来说,你应该使用不同的名称并使用 referencedColumnName
来引用外列名称
@ManyToOne
@JoinColumn(name = "sender_uuid", referencedColumnName = "uuid", insertable = false, updatable = false) // this uuid is in the User entity
private User sender;
@ManyToOne
@JoinColumn(name = "receiver_uuid", referencedColumnName = "uuid", insertable = false, updatable = false) // this uuid is in the User entity
private User receiver;
删除 insertable = false, updatable = false
。这是告诉 JPA 将此列从生成的 SQL 语句中删除。