In ormlite, How can I have a foriegn field and a field column?

旧街凉风 提交于 2019-12-12 02:49:41

问题


My Entity is something like this.

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

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 ?

EDIT

Here is my Article Entity

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


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


来源:https://stackoverflow.com/questions/23733036/in-ormlite-how-can-i-have-a-foriegn-field-and-a-field-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!