由于创建了唯一的密钥,因此无法在许多人映射中添加多个实体
关于两个实体:用户和配置信息,我一直在与这个问题作斗争。
每个用户都应保留许多配置文件实例,但是在创建第二个配置文件时,我会遇到以下错误:重复输入'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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论