[英]In ormlite, How can I have a foriegn field and a field column?
我的实体就是这样。
@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' specified twice
。 在ormlite表配置中, sid
和article
都被视为具有相同名称的列。
我该如何实现?
编辑
这是我的文章实体
@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();
}
您更改了问题,所以最后一个答案不再起作用,请尝试一下并告诉我是否是您要的内容:
@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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.