単純なコレクションであろうと関連であろうと、エンティティのコレクションが保持されません。
私はmongodbでOGMを使用しています。
問題の例として、次のエンティティを検討してください。
@Entity
class Document {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")
String id;
String name;
@ElementCollection
Set<String> names;
Document() {
this.names = new HashSet<>();
}
Document(String name) {
this();
this.name = name;
}
}
@Entity
class ChildDocument {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")
String id;
String name;
ChildDocument() {}
ChildDocument(String name) {
this.name = name;
}
}
class ParentDocument {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")
String id;
int count;
@OneToMany(cascade = CascadeType.ALL)
@AssociationStorage(AssociationStorageType.IN_ENTITY)
List<ChildDocument> kids = new LinkedList<>();
}
次のセットアップ:
final StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder()
.applySetting(OgmProperties.ENABLED, true)
.applySetting(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta")
.applySetting(AvailableSettings.JTA_PLATFORM, "JBossTS")
.applySetting(OgmProperties.DATASTORE_PROVIDER, MongoDB.DATASTORE_PROVIDER_NAME)
.applySetting(OgmProperties.DATABASE, "testdb")
.applySetting(OgmProperties.CREATE_DATABASE, "true");
final StandardServiceRegistry registry = registryBuilder.build();
final MetadataSources sources = new MetadataSources(registry);
sources.addAnnotatedClass(Document.class);
sources.addAnnotatedClass(ChildDocument.class);
sources.addAnnotatedClass(ParentDocument.class);
final SessionFactory sessionFactory = sources.buildMetadata().getSessionFactoryBuilder()
.unwrap(OgmSessionFactoryBuilder.class)
.build();
そして、この短いプログラム:
Document document1 = new Document("one");
Document document2 = new Document("two");
document2.names.add("one.one");
document2.names.add("one.two");
ParentDocument parent = new ParentDocument();
parent.count = 2;
parent.kids.add(new ChildDocument("one"));
parent.kids.add(new ChildDocument("two"));
final Session session = sessionFactory.openSession();
session.save(document1);
session.save(document2);
session.save(parent);
session.close();
sessionFactory.close();
には、 、 、 の 3 つのコレクションがtestdb
含まれています。 Document
ChildDocument
ParentDocument
- と
ChildDocument
のみが必要なため、ドキュメントは正しいです。_id
name
Document
ドキュメントには と のみが含まれており_id
、name
コレクションnames
がありません- and
ParentDocument
のみが永続化されていますが、が作成されていても子供への参照がありません_id
count
ChildDocuments
私は何を間違っていますか?
ありがとう