gpt4 book ai didi

java - ID 字段为 "Repeated column in mapping for entity"

转载 作者:行者123 更新时间:2023-11-30 02:18:04 25 4
gpt4 key购买 nike

我正在使用 Hibernate 制作 Spring 应用程序,我只是想让我的实体之间的关系正常工作。我为所有外键设置了注释,现在出现“实体映射中的重复列”错误。我试图在这里找到解决方案,但我并没有真正理解一些答案,而且我认为它们不适用于我的代码。我之前在另一个项目中遇到过这个问题,其中所有 ID 字段都被命名为 id当我通过 @Column(name = "") 显式定义列名时并在 @JoinColumn 中输入该名称它起作用的参数。现在我遇到了同样的错误,但为 ID 字段定义显式名称没有帮助。

这是我的实体类:

@Entity
public class Task implements Serializable, Discussable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "task_id")
private long id;

private String name;

@ManyToMany
@JoinTable(
name = "user_task",
joinColumns = @JoinColumn(name = "task_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private Set<User> assigneeUsers;

@Enumerated(EnumType.ORDINAL)
private Status status; //alt + enter

private Priority priority;

private Date deadline;

private String description;

@ManyToOne
@JoinColumn(name = "task_id")
private Task parentTask;

@OneToMany(mappedBy = "parentTask")
private Set<Task> subtasks;

@Enumerated(EnumType.ORDINAL)
private Type type;

@OneToOne(cascade = CascadeType.REMOVE)
@JoinColumn(name = "channel_id")
private Channel channel;

@ManyToOne
@JoinColumn(name = "project_id")
private Project project;

// getters, setters and default constructor

我尝试编译时得到的结果是:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
at com.github.mesayah.assistance.AssistanceApplication.main(AssistanceApplication.java:20) [classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
... 16 common frames omitted
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.github.mesayah.assistance.model.Task column: task_id (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:830) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:848) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:870) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:605) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:265) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
... 22 common frames omitted

所以我认为问题出在 private Task parentTask;private Set<User> asigneeUsers;因为只有这些成员使用“task_id”的映射。但我无法弄清楚这段代码具体有什么问题。

最佳答案

您看到的错误:

Repeated column in mapping for entity: com.github.mesayah.assistance.model.Task column: task_id (should be mapped with insert="false" update="false")

This tells you that the column task_id inside the entity Task has been associated with more than one property.

You can associate the same column to multiple properties in a mapping; however, only one of the mappings can control the persistence behavior while the others are read-only.

As a trivial example:

public class MyEntity {
@Column(name = "NAME")
private String name;
@Column(name = "NAME", insertable = false, updatable = false)
private String name2;
}

本例中的属性name将始终被插入和更新。此属性控制列NAME 的持久性。属性 name2 永远不会被插入或更新,而只是充当列 NAME只读字段。

如果我没有在列映射中包含 insertable = false, updatable = false,那么 Hibernate 就会给出与您所看到的相同的错误消息。

现在您已经了解了错误消息的技术原因,您的问题正如 Maciej Kowalski 提到的那样。您可能应该以不同的方式映射您的父引用:

@ManyToOne
@Column(name = "parent_task_id")
private Task parentTask;

从概念上讲,您要寻找的是子Task,它有一个列task_id,还有第二列引用父级的外键任务。该外键必须存储在一个新列中,恰本地命名为 parent_task_id

关于java - ID 字段为 "Repeated column in mapping for entity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47690719/

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