尝试在Hibernate OGM中保留Map的嵌入文档时出现问题

时间:2018-01-15 20:03:16

标签: java mongodb hibernate nosql hibernate-ogm

在我的应用程序中,我使用Hibernate OGM在MongoDB中保存数据。我有以下类的结构:

历史:

@Entity(name = "historic")
public class Historic {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Type(type = "objectid")
    private String id;

    @ElementCollection
    private List<Information> informations;

    private Double pctDocumented;

    private boolean actual;

}

信息(它不是实体):

public class Information {

   private String infoType;
   private boolean documented;
   private boolean implemented;

   private Map<String, String> settings;

}

当我尝试初始化我的EntityManagerFactory时,我收到以下错误:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: ogm] Unable to build Hibernate SessionFactory
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at br.com.app.Main.main(Main.java:53)
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: historico_informations, for columns: [org.hibernate.mapping.Column(settings)]

当我用以下内容评论该行:

private Map<String, String> settings;

它运作正常。如果我把这个Map属性放在Historic类中它也可以。当时唯一的区别是一个是实体而另一个是不是。任何想法?

1 个答案:

答案 0 :(得分:0)

您需要将@Embeddable注释添加到信息中,并将@ElementCollection添加到settings

@Embeddable
public class Information {

   ...

   @ElementCollection
   private Map<String, String> settings;

}