简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM