在ormlite中,我如何拥有外部字段和字段列?

时间:2014-05-19 08:27:39

标签: java sql 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' specified twice。 在ormlite表配置中,sidarticle都被视为具有相同名称的列。

我如何实现这一目标?

修改

这是我的文章实体

@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();
}

1 个答案:

答案 0 :(得分:1)

您更改了自己的问题,以便最后的答案不再适用,请尝试并告诉我这是否是您要找的内容:

@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();
}