簡體   English   中英

在ormlite中,我如何擁有前字段和字段列?

[英]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表配置中, 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();
}

您更改了問題,所以最后一個答案不再起作用,請嘗試一下並告訴我是否是您要的內容:

@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM