2016-01-27 22 views
0

我的实体中的集合不会被持久化,无论它是简单的集合还是关联。
我正在用mongodb使用OGM。在使用Hibernate OGM和mongodb的实体中缺少集合

为考虑问题的一个例子以下实体:

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

testdb现在包含3收藏品:Document,ChildDocumentParentDocument

  • ChildDocument文件是正确的,因为他们只需要有_idname
  • Document文件也只包含_idname,该names集合缺失
  • ParentDocument只有_idcount持续存在,但即使创建了ChildDocuments
但对孩子的引用丢失

我在做什么错?
谢谢

回答

1

在引擎盖下的Hibernate OGM执行一些优化,因此命令不会立即在db(通常)上执行。

当使用Hibernate OGM,你还是应该使用事务划分为您的操作:

final Session session = sessionFactory.openSession(); 

    session.beginTransaction(); 

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

    session.getTransaction().commit(); 

    session.close(); 

这个文档中解释:使用session.flush()session.close()会工作,以及在这种情况下http://docs.jboss.org/hibernate/ogm/5.0/reference/en-US/html/ch11.html#transactions

注意。