使用连接表

发布于 2024-12-24 19:11:18 字数 2055 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2024-12-31 19:11:18

我认为您必须更改

@JoinColumn(name = "mentioned", referencedColumnName = "username")

以指定用户表,

@JoinColumn(name = "mentioned", referencedColumnName = "user")

因为基本上该消息是说您的 tweet 表没有外键字段 username ,但它不具有外键字段 username您已将其称为 user

这 -> 私人收藏提及; 也应该是一对多的用户集合

@OneToMany
@JoinTable(
    name = "Mention", 
    joinColumns = {
        @JoinColumn(name = "tweet", referencedColumnName = "id")
    }, 
    inverseJoinColumns = {
        @JoinColumn(name = "mentioned", referencedColumnName = "user")
    }
)    
private Collection<User> mentions;

I think you have to change

@JoinColumn(name = "mentioned", referencedColumnName = "username")

to specify the user table

@JoinColumn(name = "mentioned", referencedColumnName = "user")

As basically the message is saying that your tweet table doesn't have a foreign key field username which it doesn't as you have called it user

This -> private Collection<Tweet> mentions; should also be a one to many collection of users leaving you with

@OneToMany
@JoinTable(
    name = "Mention", 
    joinColumns = {
        @JoinColumn(name = "tweet", referencedColumnName = "id")
    }, 
    inverseJoinColumns = {
        @JoinColumn(name = "mentioned", referencedColumnName = "user")
    }
)    
private Collection<User> mentions;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文