0

単純なコレクションであろうと関連であろうと、エンティティのコレクションが保持されません。
私は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含まれています。 DocumentChildDocumentParentDocument

  • ChildDocumentのみが必要なため、ドキュメントは正しいです。_idname
  • Documentドキュメントには と のみが含まれており_idnameコレクションnamesがありません
  • andParentDocumentのみが永続化されていますが、が作成されていても子供への参照がありません_idcountChildDocuments

私は何を間違っていますか?
ありがとう

4

1 に答える 1

1

内部の Hibernate OGM はいくつかの最適化を実行するため、コマンドはデータベースですぐには実行されません (通常)。

Hibernate OGM を使用する場合でも、操作にはトランザクション境界を使用する必要があります。

   final Session session = sessionFactory.openSession();

   session.beginTransaction();

   session.save(document1);
   session.save(document2);
   session.save(parent);

   session.getTransaction().commit();

   session.close();

これはドキュメントで説明されています: http://docs.jboss.org/hibernate/ogm/5.0/reference/en-US/html/ch11.html#transactions

この場合、session.flush()beforeを使用しても機能することに注意してください。session.close()

于 2016-01-27T19:45:18.550 に答える