テキスト全体を一度保存してから、柔軟なクラス階層を作成してコンテンツにインデックスを付けることをお勧めします。
特別な場所にのみ Hibernate アノテーションを付けました。
テキスト自体とパーツ オブジェクトを保持するコンテナーを作成します。
public class DocumentContainer extends Model {
// Column definition depends on the DB, here: MySQL
@Column(columnDefinition="LONGTEXT")
public String text;
public Set<DocumentPart> documentParts;
}
ドキュメントの一部は、テキストの領域で定義され、特定のタイプであり、ドキュメントの他の部分を参照できます。
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="partType")
public class DocumentPart extends Model {
Document document;
// indices over the documents text for this part
int startIndex;
int endIndex;
@Enumerated(EnumType.STRING)
PartType partType;
Set<DocumentPart> referencedParts;
}
public enum PartType {
DOCUMENT, PARAGRAPH, TOKEN
}
段落は、たとえば次のようになります。
@Entity
@DiscriminatorValue("PARAGRAPH")
public class Paragraph extends DocumentPart {
Set<Token> tokens;
}
このようにして、ドキュメント上にある領域のタイプに関して柔軟に対応でき、ドキュメント全体 (句読点などを含む) を保持できます。