Java Hibernate 映射异常! (无法确定类型:java.util.Map)

发布于 2024-10-05 08:52:16 字数 1766 浏览 12 评论 0原文

我创建了一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云醉月微眠 2024-10-12 08:52:38

你有属性的 getter 和 setter 吗?

Do you have both getter and setter for Properties?

七婞 2024-10-12 08:52:36

啊,我明白了,这是一个。我认为除非您使用最新的 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.

雄赳赳气昂昂 2024-10-12 08:52:33

属性不应该只是 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?

影子是时光的心 2024-10-12 08:52:31

我也面临同样的问题。已经晚了,但我认为这会帮助其他人。使用@MapKeyColumn。这是我的简单代码

@ElementCollection(targetClass=String.class)
@MapKeyColumn(name="Employee_Position")
private Map<String,String> position=new HashMap<String,String>();

I also face the same problem.It is late but i think it will help others.use @MapKeyColumn.here is my simple code

@ElementCollection(targetClass=String.class)
@MapKeyColumn(name="Employee_Position")
private Map<String,String> position=new HashMap<String,String>();
尐籹人 2024-10-12 08:52:29

那是因为你必须使用一些 jpa2 实现!这家伙有同样的问题

that's because you have to use some jpa2 implementation! this guy had the same problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文