Java Hibernate 映射异常! (无法确定类型:java.util.Map)
我创建了一个名为 Movie 的类,其中包含以下字段:
@Id
@GeneratedValue
private Long id;
private String name;
@ElementCollection(targetClass = String.class)
private Map<String, String> properties;
private Double rate;
private Integer votersCount;
private Date releaseDate;
private Integer runtime;
@ManyToMany
@JoinTable(name = "movie_director")
@IndexColumn(name = "directorIndex")
private List<Person> directors;
@ManyToMany
@JoinTable(name = "movie_writer")
@IndexColumn(name = "writerIndex")
private List<Person> writers;
@OneToMany
@IndexColumn(name = "roleIndex")
private List<MovieRole> movieRoles;
@ManyToMany
@JoinTable(name = "movie_genre")
@IndexColumn(name = "genreIndex")
private List<Genre> genres;
如您所见,我使用了 hibernate 注释,对象是 bean。 但是当我尝试使用以下代码打开休眠会话时...
session = HibernateSessionFactory.getSessionFactory().openSession();
我遇到了无法映射 Java.Util.Map 类的问题。 这是异常堆栈跟踪:
org.hibernate.MappingException: Could not determine type for: java.util.Map, for columns: [org.hibernate.mapping.Column(properties)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
at org.hibernate.mapping.Property.isValid(Property.java:185)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:410)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1099)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1284)
at main.HibernateSessionFactory.getSessionFactory(HibernateSessionFactory.java:29)
at main.MainClass.main(MainClass.java:26)
我是休眠新手,不知道到底发生了什么...... 请帮我!
I have made a class with name of Movie with folowing fields:
@Id
@GeneratedValue
private Long id;
private String name;
@ElementCollection(targetClass = String.class)
private Map<String, String> properties;
private Double rate;
private Integer votersCount;
private Date releaseDate;
private Integer runtime;
@ManyToMany
@JoinTable(name = "movie_director")
@IndexColumn(name = "directorIndex")
private List<Person> directors;
@ManyToMany
@JoinTable(name = "movie_writer")
@IndexColumn(name = "writerIndex")
private List<Person> writers;
@OneToMany
@IndexColumn(name = "roleIndex")
private List<MovieRole> movieRoles;
@ManyToMany
@JoinTable(name = "movie_genre")
@IndexColumn(name = "genreIndex")
private List<Genre> genres;
as you can see, I have used hibernate annotation and object is bean.
but when I try to open my hibernate session with the following code...
session = HibernateSessionFactory.getSessionFactory().openSession();
I encounter a problem regarding could not map a Java.Util.Map class.
Here is exception stack trace:
org.hibernate.MappingException: Could not determine type for: java.util.Map, for columns: [org.hibernate.mapping.Column(properties)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
at org.hibernate.mapping.Property.isValid(Property.java:185)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:410)
at org.hibernate.mapping.RootClass.validate(RootClass.java:192)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1099)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1284)
at main.HibernateSessionFactory.getSessionFactory(HibernateSessionFactory.java:29)
at main.MainClass.main(MainClass.java:26)
I'm new to hibernate and don't know exactly what's happening...
please help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你有属性的 getter 和 setter 吗?
Do you have both getter and setter for Properties?
啊,我明白了,这是一个。我认为除非您使用最新的 jar,否则您无法映射原语。 https://forum.hibernate.org/viewtopic.php?t=955308 。检查该链接。您可以创建一个名为 Properties 的类,其中包含键和值,然后使用它吗?我遇到了类似的问题,我不得不使用这种方法。
Aah I see, its a . I dont think you can map a primitive unless you are using latest jar. https://forum.hibernate.org/viewtopic.php?t=955308. Check that link. Could you make a class called as Properties with key and value and then use it? I had a similar problem and I had to use that approach.
属性不应该只是
List
类型吗?听起来 Hibernate 的困惑和我的一样,即为什么 Properies 是 Map 而不是列表?你究竟想在那里做什么?
Shouldn't properties just be a
List<String>
type?It sounds like Hibernates confusion is the same as mine which is, why is Properies a Map instead of a list? What exactly are you trying to do there?
我也面临同样的问题。已经晚了,但我认为这会帮助其他人。使用@MapKeyColumn。这是我的简单代码
I also face the same problem.It is late but i think it will help others.use @MapKeyColumn.here is my simple code
那是因为你必须使用一些 jpa2 实现!这家伙有同样的问题
that's because you have to use some jpa2 implementation! this guy had the same problem