问题
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