由于创建了唯一的密钥,因此无法在许多人映射中添加多个实体

发布于 2025-01-21 20:56:57 字数 1441 浏览 0 评论 0原文

关于两个实体:用户和配置信息,我一直在与这个问题作斗争。

每个用户都应保留许多配置文件实例,但是在创建第二个配置文件时,我会遇到以下错误:重复输入'uk_141232asdas78k5552'uke'usernameInquestion''usernameInquestion'

在检查数据库模式是否 profile Information 我注意到“ 用户名”字段是作为唯一键创建的:

 `username` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UK_14113pekuxo8jklda2678k552` (`username`),
  CONSTRAINT `FKt3ewldgdc9d75ji6j5ii7cbrc` FOREIGN KEY (`username`) REFERENCES `users` (`username`)

我在profileSinformation和另一个具有相同的实体之间有着非常相似的关系代码(除了类的命名和@joincolumn(name =“ profileinformation_id”)),这不会发生:

`profileinformation_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FKl5ww2gn1e1wgqcuvraa6p70ya` (`profileinformation_id`),
  CONSTRAINT `FKl5ww2gn1e1wgqcuvraa6p70ya` FOREIGN KEY (`profileinformation_id`) REFERENCES `profileinformationtable` (`id`)

这是更大的系统的一部分,以及使这项工作的所有混乱之间试图复制已经有效的情况。

public class ProfileInformation

[ ... ]

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToOne
@JoinColumn(name="username")
private User user;

用户类是以下内容(尽管从我收集到的实际上似乎并不重要):

public class User

[ ... ]

@Id
@Column(name = "username", nullable = false)
private String username;

@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
private List<ProfileInformation> infos;

I have been fighting this issue regarding two entities: User and ProfileInformation.

Each user should hold many ProfileInformation instances but I get the following error when creating a second ProfileInformation: Duplicate entry 'usernameInQuestion' for key 'UK_141232asdas78k552'.

When checking the database schema for ProfileInformation I noticed that the 'username' field was created as a unique key:

 `username` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UK_14113pekuxo8jklda2678k552` (`username`),
  CONSTRAINT `FKt3ewldgdc9d75ji6j5ii7cbrc` FOREIGN KEY (`username`) REFERENCES `users` (`username`)

I have a very similar relation between ProfileInformation and another entity with the same code (except the naming of the classes and @JoinColumn(name = "profileinformation_id") ) and this doesn't happen:

`profileinformation_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FKl5ww2gn1e1wgqcuvraa6p70ya` (`profileinformation_id`),
  CONSTRAINT `FKl5ww2gn1e1wgqcuvraa6p70ya` FOREIGN KEY (`profileinformation_id`) REFERENCES `profileinformationtable` (`id`)

This is part of a bigger system and between all the confusion of making this work I attempted to replicate the already working similar situations.

public class ProfileInformation

[ ... ]

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToOne
@JoinColumn(name="username")
private User user;

The User class is the following (although from what I gather this really doesn't seem to matter much):

public class User

[ ... ]

@Id
@Column(name = "username", nullable = false)
private String username;

@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
private List<ProfileInformation> infos;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文