2014-05-19 1 views
0

내 개체는 다음과 같습니다. ormlite에서 foriegn 필드와 필드 열을 어떻게 가질 수 있습니까?

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

내가 삽입

Column 'sid' specified twice의 SQL 구문 예외가 발생합니다. ormlite 테이블 구성에서 sidarticle은 모두 같은 이름의 열로 간주됩니다.

어떻게하면됩니까? 여기에 편집

당신은, 그래서 마지막 대답은 더 이상 작동 does't 질문을 변경할 것을 시도하고 당신이 찾고있는 경우 말해 내 제 엔티티

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

'Common'과'Article'의'columnName' 속성이 모두'sid' 인 이유는 무엇입니까? –

+0

부모님의 sid가 실수입니다. – wener

+0

'Article s '의'Long sid;'또는'columnName' 속성 값의'sid' 열 이름을 적절한 이름으로 변경해야한다고 생각합니다. 데이터베이스 열 이름이 같은 테이블에 없어야합니다. –

답변

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

나는 이해하지 못한다. 코드를 보여줄 수 있니? 고마워요 – wener

+0

죄송합니다, 내 질문에, 나는 정상적인 필드에 대한'@ DatabaseField'을 무시했습니다. 사실, 그들은 기본'@ DatabaseField' 주석을 가지고 있습니다. 그래서, 당신의 코드는 여전히 같은 예외를 야기합니다. : < – wener

+0

왜 기사와 부모 열 "sid"와 "tid"를 호출해야합니까? 그리고 sid는 article.getSid()가 아닌가요? –