使用连接表
我正在尝试使用 Java2EE 做一个类似 twitter 的项目,这是数据库的简单视图。
Mention、Followers和Following是三个JoinTable。 (请注意, said、follower、following 和每个用户字段都有 User.username 作为外键。) 我的第一个有问题,导致我的部署失败并出现以下异常:
异常描述:映射到元素[字段提及]的引用列名称[用户名]与映射引用上的有效字段不对应。
这是两个实体的映射。
推文:
public class Tweet implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "id", nullable = false)
private Integer id;
...
@JoinColumn(name = "user", referencedColumnName = "username")
@ManyToOne
User user;
@JoinTable(name = "Mention", joinColumns = {
@JoinColumn(name = "tweet", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "mentioned", referencedColumnName = "username")})
@OneToMany
private Collection<Tweet> mentions;
用户:
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "username", nullable = false, length = 50)
...
@OneToMany(mappedBy = "user")
Collection<Tweet> tweets;
@JoinTable(name = "Following", joinColumns = {
@JoinColumn(name = "user", referencedColumnName = "username")}, inverseJoinColumns = {
@JoinColumn(name = "following", referencedColumnName = "username")})
@OneToMany
Collection<User> following;
@JoinTable(name = "Followers", joinColumns = {
@JoinColumn(name = "user", referencedColumnName = "username")}, inverseJoinColumns = {
@JoinColumn(name = "follower", referencedColumnName = "username")})
@OneToMany
Collection<User> followers;
@OneToMany(mappedBy = "mentions")
Collection<Tweet> mentioning;
我的映射出了什么问题? 有人可以指出我的解决方案吗?
I'm trying to do a twitter-like project using Java2EE, and here's a simple view of the database.
Mention, Followers and Following are three JoinTable.
(Note that mentioned,follower,following and every user field have User.username as foreign key.)
I've got a problem with the first one, that causes my deploy to fail with this exception :
Exception Description: The reference column name [username] mapped on the element [field mentions] does not correspond to a valid field on the mapping reference..
Here's the mapping for the two entities.
Tweet :
public class Tweet implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "id", nullable = false)
private Integer id;
...
@JoinColumn(name = "user", referencedColumnName = "username")
@ManyToOne
User user;
@JoinTable(name = "Mention", joinColumns = {
@JoinColumn(name = "tweet", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "mentioned", referencedColumnName = "username")})
@OneToMany
private Collection<Tweet> mentions;
User :
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
@Column(name = "username", nullable = false, length = 50)
...
@OneToMany(mappedBy = "user")
Collection<Tweet> tweets;
@JoinTable(name = "Following", joinColumns = {
@JoinColumn(name = "user", referencedColumnName = "username")}, inverseJoinColumns = {
@JoinColumn(name = "following", referencedColumnName = "username")})
@OneToMany
Collection<User> following;
@JoinTable(name = "Followers", joinColumns = {
@JoinColumn(name = "user", referencedColumnName = "username")}, inverseJoinColumns = {
@JoinColumn(name = "follower", referencedColumnName = "username")})
@OneToMany
Collection<User> followers;
@OneToMany(mappedBy = "mentions")
Collection<Tweet> mentioning;
What's wrong with my mapping?
Can someone point me to the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须更改
以指定用户表,
因为基本上该消息是说您的
tweet
表没有外键字段username
,但它不具有外键字段username
您已将其称为user
这 ->
私人收藏提及;
也应该是一对多的用户集合I think you have to change
to specify the user table
As basically the message is saying that your
tweet
table doesn't have a foreign key fieldusername
which it doesn't as you have called ituser
This ->
private Collection<Tweet> mentions;
should also be a one to many collection of users leaving you with