0

In my app i am using Hibernate OGM to persist data in MongoDB. I have the following structure of classes:

Historic:

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

}

Information (It is not an entity):

public class Information {

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

   private Map<String, String> settings;

}

And when i try to initialize my EntityManagerFactory, i get the following error:

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)]

When i comment the line with:

private Map<String, String> settings;

it works properly. If i put this Map attribute in Historic class it works too. The only difference between then is one is an Entity and other isn't. Any ideias?

Jose Victor
  • 350
  • 1
  • 5
  • 13
  • 1
    only a mapped Class can use @ElementCollection, Information is not a entity thus it can use this annotation. – Jorge L. Morla Jan 15 '18 at 20:07
  • Thanks for the comment Jorge. I have noticed this after when I tried to remove the annnotation and seems that nothing changes, the error is the same. Do you have any suggestions in how to use a Map in this case? Maybe create a custom converter, is it possible? – Jose Victor Jan 16 '18 at 11:24
  • I removed the line with the @ElementCollection annotation of the question's body – Jose Victor Jan 16 '18 at 11:27
  • there are a bit wrong implementation, a I suggest you to change a bit your application structure, do you want to have a history that contain a list of information it contain a stack of settings right? – Jorge L. Morla Jan 16 '18 at 14:09

1 Answers1

0

You need to add the @Embeddable annotation to Information and add @ElementCollection to settings:

@Embeddable
public class Information {

   ...

   @ElementCollection
   private Map<String, String> settings;

}
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • Thanks for the answer Davide.The answer you provided thrown the following exception: java.util.ConcurrentModificationException when it tries to create the EntityManagerFactory . I'm using 5.1.0.Final version. – Jose Victor Jan 16 '18 at 12:30