Hibernate 不想在 hibernate.cfg.xml 中映射 class
Hibernate doesn't want to map the class in hibernate.cfg.xml
我有一个 class 用户是实体。当我尝试将其映射到 hibernate.cfg.xml
时
<mapping class="com.igorgorbunov3333.entities.User"/>
得到一个错误:
Exception in thread "main" org.hibernate.MappingException: Unknown
entity: com.igorgorbunov3333.entities.User
但是当我以编程方式映射 class 用户时它工作正常。
configuration.addAnnotatedClass(User.class);
这是什么原因?
我的实体class:
package com.igorgorbunov3333.entities;
import javax.persistence.*;
/**
* Created by Игорь on 01.07.2016.
*/
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
private String password;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
您的休眠配置文件 (hibernate.cfg.xml) 应该包含到实体 class.Something 的映射,如下所示:
<mapping resource="com/igorgorbunov3333/entities/User.hbm.xml"/>
你能检查一下吗。
尝试使用 4.3.6.Final 版本的休眠。你使用什么休眠版本?使用 5.2.1.Final 我看到了同样的问题。通过更改休眠版本来解决。
回答
https://forum.hibernate.org/viewtopic.php?f=1&t=1043461&p=2489997&hilit=hibernate.cfg.xml#p2489997
我遇到了同样的错误,我做了和你一样的事情(直接映射 class),它也有效。这对我来说没有任何意义所以我查看了源代码并看到了这个
/**
* Use the mappings and properties specified in the given application resource. The format of the resource is
* defined in <tt>hibernate-configuration-3.0.dtd</tt>.
*
* @param resource The resource to use
*
* @return this for method chaining
*
* @throws HibernateException Generally indicates we cannot find the named resource
*/
public Configuration configure(String resource) throws HibernateException {
standardServiceRegistryBuilder.configure( resource );
// todo : still need to have StandardServiceRegistryBuilder handle the "other cfg.xml" elements.
// currently it just reads the config properties
properties.putAll( standardServiceRegistryBuilder.getSettings() );
return this;
}
所以基本上,并不是 hibernate.cfg.xml 文件中的所有项目当前都在读取,我无法找出休眠提供的任何替代方案 API
我有一个 class 用户是实体。当我尝试将其映射到 hibernate.cfg.xml
时<mapping class="com.igorgorbunov3333.entities.User"/>
得到一个错误:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.igorgorbunov3333.entities.User
但是当我以编程方式映射 class 用户时它工作正常。
configuration.addAnnotatedClass(User.class);
这是什么原因?
我的实体class:
package com.igorgorbunov3333.entities;
import javax.persistence.*;
/**
* Created by Игорь on 01.07.2016.
*/
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
private String password;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
您的休眠配置文件 (hibernate.cfg.xml) 应该包含到实体 class.Something 的映射,如下所示:
<mapping resource="com/igorgorbunov3333/entities/User.hbm.xml"/>
你能检查一下吗。
尝试使用 4.3.6.Final 版本的休眠。你使用什么休眠版本?使用 5.2.1.Final 我看到了同样的问题。通过更改休眠版本来解决。
回答 https://forum.hibernate.org/viewtopic.php?f=1&t=1043461&p=2489997&hilit=hibernate.cfg.xml#p2489997
我遇到了同样的错误,我做了和你一样的事情(直接映射 class),它也有效。这对我来说没有任何意义所以我查看了源代码并看到了这个
/**
* Use the mappings and properties specified in the given application resource. The format of the resource is
* defined in <tt>hibernate-configuration-3.0.dtd</tt>.
*
* @param resource The resource to use
*
* @return this for method chaining
*
* @throws HibernateException Generally indicates we cannot find the named resource
*/
public Configuration configure(String resource) throws HibernateException {
standardServiceRegistryBuilder.configure( resource );
// todo : still need to have StandardServiceRegistryBuilder handle the "other cfg.xml" elements.
// currently it just reads the config properties
properties.putAll( standardServiceRegistryBuilder.getSettings() );
return this;
}
所以基本上,并不是 hibernate.cfg.xml 文件中的所有项目当前都在读取,我无法找出休眠提供的任何替代方案 API