【发布时间】:2014-08-06 11:32:36
【问题描述】:
我正在尝试通过连接表中的附加属性在两个实体之间实现 MANY_TO_MANY 关系。 我找到了以下答案: how-to-do-a-many-to-many-relationship-in-spring-roo-with-attributes-within-de-relationship 但我未能尝试使提供的解决方案适应我的场景:
目前我有一个直接的 MANY_TO_MANY 关系:
# book:
entity jpa --class ~.domain.Book --testAutomatically
field string --fieldName title --notNull
field string --fieldName subtitle --notNull
field string --fieldName description
# n:m relationship to author
field list --fieldName authors --type ~.domain.Author --cardinality MANY_TO_MANY
# author:
entity jpa --class ~.domain.Author --testAutomatically
field string --fieldName firstName --notNull
field string --fieldName lastName --notNull
按预期工作,但我需要让作者订购。我想通过定义关系表并向其中添加一个像“序列”这样的整数字段来实现这一点,但是当我尝试在 Book 中定义多对多关系时遇到了困难:
entity jpa --class ~.domain.BookAuthorOrdered --table book_author_ordered
# the additional field to store sequence of authors:
field number --fieldName authorSequence --type java.lang.Integer --notNull
# reference to book:
field reference --fieldName bookId --type ~.domain.Book --cardinality MANY_TO_ONE
# reference to author:
field reference --fieldName authorId --type ~.domain.Author --cardinality MANY_TO_ONE
谁能给我一个提示,如何在 Book 中定义属性,以便我使用上面定义的连接表获得排序作者的列表?这是我尝试过的:
# complete the relationship
focus --class ~.domain.Book
field list --type ~.domain.BookAuthorOrdered --fieldName orderedAuthors --cardinality ONE_TO_MANY --mappedBy bookId
【问题讨论】:
标签: spring hibernate many-to-many persistence spring-roo