Java Hibernate 映射异常!(无法确定类型:java.util.Map)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4324839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Hibernate Mapping Exception! (Could not determine type for: java.util.Map)
提问by Ehphan
I have made a class with name of Movie with folowing fields:
我创建了一个名为 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;
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...
如您所见,我使用了 hibernate 注释并且对象是 bean。但是当我尝试使用以下代码打开我的休眠会话时...
session = HibernateSessionFactory.getSessionFactory().openSession();
I encounter a problem regarding could not map a Java.Util.Map class. Here is exception stack trace:
我遇到了无法映射 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'm new to hibernate and don't know exactly what's happening... please help me!
我是休眠的新手,不知道到底发生了什么......请帮帮我!
采纳答案by AmirMV
that's because you have to use some jpa2 implementation! this guy had the same problem
那是因为您必须使用一些 jpa2 实现!这家伙有同样的问题
回答by Jinesh Parekh
Do you have both getter and setter for Properties?
你有属性的 getter 和 setter 吗?
回答by Spencer Ruport
Shouldn't properties just be a List<String>
type?
属性不应该只是一种List<String>
类型吗?
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?
听起来 Hibernates 的困惑与我的一样,为什么 Properies 是 Map 而不是列表?你到底想在那里做什么?
回答by Jinesh Parekh
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.
啊,我明白了,它是一个 . 除非您使用最新的 jar,否则我认为您无法映射原语。https://forum.hibernate.org/viewtopic.php?t=955308。检查那个链接。您可以使用键和值创建一个名为 Properties 的类,然后使用它吗?我有一个类似的问题,我不得不使用这种方法。
回答by Touhidur Rahaman Khan
I also face the same problem.It is late but i think it will help others.use @MapKeyColumn.here is my simple code
我也面临同样的问题。晚了,但我认为它会帮助其他人。使用@MapKeyColumn。这是我的简单代码
@ElementCollection(targetClass=String.class)
@MapKeyColumn(name="Employee_Position")
private Map<String,String> position=new HashMap<String,String>();