在 ormlite 中,我怎样才能有一个外国字段和一个字段列? [英] In ormlite, How can I have a foriegn field and a field column?
本文介绍了在 ormlite 中,我怎样才能有一个外国字段和一个字段列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的实体就是这样.
@Data
public class Comment implements Persistable<Long>, CBHistoryTable
{
@Id
private Long tid;
// sid and pid is required for serialized to json
@DatabaseField
private Long pid;
@DatabaseField
private Long sid;
@DatabaseField(foreign = true, foreignColumnName = "sid", columnName = "sid")
private Article article;
@DatabaseField(foreign = true, foreignColumnName = "pid", columnName = "tid")
private Comment parent;
}
当我插入时,会导致SQL语法异常,Column 'sid' 指定了两次
.在ormlite table config中,sid
和article
都被认为是同名的列.
When I insert, will cause the SQL Syntax Exception, Column 'sid' specified twice
.
In ormlite table config, both of sid
and article
are considered as a column with the same name.
我怎样才能做到这一点?
How Can I achieve this ?
编辑
这是我的文章实体
@Data
@DatabaseTable(daoClass = ArticleServiceImpl.class)
public class Article implements Persistable<Long>, CBHistoryTable
{
@Id
@SerializedName("SID")
private Long sid;
@SerializedName("SN")
@DatabaseField
private String sn;
@ForeignCollectionField(foreignFieldName = "article")
private Collection<Comment> comments = Sets.newHashSet();
}
推荐答案
您更改了您的问题,因此最后一个答案不再有效,请尝试并告诉我这是否是您要查找的内容:
You change your question so last answer does't work anymore, try that and tell me if it is what you are looking for :
@Data
public class Comment implements Persistable<Long>, CBHistoryTable
{
@Id
private Long tid;
// sid and pid is required for serialized to json
private Long pid;
private Long sid;
@DatabaseField(canbenull = true, foreign = true, foreignColumnName = "sid")
private Article article;
@DatabaseField(canbenull = true, foreign = true, foreignColumnName = "tid")
private Comment parent;
@ForeignCollectionField(foreignFieldName = "parent")
private Collection<Comment> comments = Sets.newHashSet();
public void setArticle(Article article) {
this.article = article;
sid=article.getSid();
}
public void setParent(Comment parent) {
this.parent = parent;
pid=comment.getTid();
}
}
@Data
@DatabaseTable(daoClass = ArticleServiceImpl.class)
public class Article implements Persistable<Long>, CBHistoryTable
{
@Id
@SerializedName("SID")
private Long sid;
@SerializedName("SN")
private String sn;
@ForeignCollectionField(foreignFieldName = "article")
private Collection<Comment> comments = Sets.newHashSet();
}
这篇关于在 ormlite 中,我怎样才能有一个外国字段和一个字段列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文