PersistenceException,列'id'指定两次 [英] PersistenceException, Column 'id' specified twice
问题描述
我在Play Framework 2.2.3中有以下文件
I have the following files in Play Framework 2.2.3
控制器:
public class Comment extends Controller
{
public Result create(UUID id)
{
models.blog.Blog blog = models.blog.Blog.finder.byId(id);
Result result;
if(blog == null)
{
result = notFound(main.render("404", error404.render()));
}
else
{
Form<models.blog.Comment> commentForm = Form.form(models.blog.Comment.class);
commentForm = commentForm.bindFromRequest();
if(commentForm.hasErrors())
{
result = badRequest(Json.toJson(commentForm));
}
else
{
models.blog.Comment comment = commentForm.get();
comment.setId(UUID.randomUUID());
comment.setTimeCreated(new Date());
comment.setBlogId(blog.getId());
comment.save();
result = ok(Json.toJson(comment));
}
}
return result;
}
}
两个模型
@Entity
@Table(name="blog")
public class Blog extends Model
{
private static final SimpleDateFormat MONTH_LITERAL = new SimpleDateFormat("MMMMM"),
DAY_NUMBER = new SimpleDateFormat("d"),
YEAR_NUMBER = new SimpleDateFormat("yyyy");
public static Finder<UUID, Blog> finder = new Finder<UUID, Blog>(UUID.class, Blog.class);
@Id
@Column(name="id",length=36, nullable=false)
public UUID id;
@OneToOne
@JoinColumn(name="author_id")
public User author;
@Column(name="title",length=255)
public String title;
@Column(name="summary",length=255)
public String summary;
@Column(name="url",length=255)
public String url;
@Column(name="content")
public String content;
@Column(name="time_updated")
public Date time_created;
@Column(name="time_created", nullable=false)
public Date time_updated;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="blog_id")
public List<Comment> comments;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name="blog_tag_map",
joinColumns={ @JoinColumn(name="blog_id", referencedColumnName="id") },
inverseJoinColumns={ @JoinColumn(name="tag_id", referencedColumnName="id") }
)
public List<Tag> tags;
public List<Comment> getComments()
{
return this.comments;
}
}
@Entity
@Table(name="blog_comment")
public class Comment extends Model
{
private static final SimpleDateFormat MONTH_LITERAL = new SimpleDateFormat("MMMMM"),
DAY_NUMBER = new SimpleDateFormat("d"),
YEAR_NUMBER = new SimpleDateFormat("yyyy");
@Id
@Column(name="id",length=36, nullable=false)
public UUID id;
@Column(name="blog_id", length=36)
public UUID blog_id;
@ManyToOne
public Blog blog;
@Column(name="content", length=500)
public String content;
@Column(name="website", length=255)
public String website;
@Column(name="name", length=255)
public String name;
@Column(name="time_created", updatable=false)
public Date time_created;
}
为了简洁,我从这些模型中排除了一些setter和getter
I have excluded some setters and getters from these models for brevity, so it doesn't clog up this post.
当我尝试向上述控制器发出POST请求时,一切都进行得很顺利,直到找到comment.save()在控制器文件中,我得到以下错误。
When I attempt to make a POST request to the aforementioned controller, everything goes fine until I get to the "comment.save()" statement in the controller file, then I get the following error.
我不确定为什么这个保存没有通过,为什么有一个列冲突。
帮助非常感谢
I'm unsure why this save isn't going through, and why there is a column conflict. Help much appreciated
推荐答案
问题在于你已经为Blog定义了两个外键列您的评论实体:
The issue lies in the fact that you have defined basically two foreign key columns for Blog in your Comment's entity:
@Column(name = "blog_id", length = 36)
public UUID blog_id;
@ManyToOne
public Blog blog;
您的blog字段的默认列名称为:blog_id
然而, ve已经命名为您的blog_id列。
有趣的是,创建此表时不会抛出错误/警告...
The default column name for your 'blog' field is: blog_id However, you've already named your 'blog_id' column that. Interestingly, no error/warning is thrown when creating this table...
因此,当您调用comment.save()时,将生成以下insert语句:
So when you call comment.save(), the following insert statement is generated:
insert into blog_comment (id, blog_id, content, website, name, time_created, blog_id) values (?,?,?,?,?,?,?)
注意两次引用blog_id无效。
这是因为上面的双重映射。
Notice a reference to 'blog_id' column twice, which is invalid. And this is because of the above double mapping.
要修复,只需给你的'blog'属性一个不同的名字用于外键列:
To fix, just give your 'blog' property a different name to use for the foreign key column:
@Column(name = "blog_id", length = 36)
public UUID blog_id;
@ManyToOne
@JoinColumn(name = "blogId")
public Blog blog;
我不知道为什么要映射这样的实体blog_id字段似乎是多余的(和混乱),因为您已经有一个实体映射以您的博客属性的形式。
I'm not sure why you're mapping your entities like this (perhaps legacy schema?) but the 'blog_id' fields seem to be redundant (and confusing) as you already have an entity mapping in the form of your 'blog' property.
这篇关于PersistenceException,列'id'指定两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!