I am trying to create a record in a table but it gives me the following exception.
我试图在一个表中创建一个记录,但是它给了我下面的异常。
org.hibernate.MappingException: Unknown entity: com.Person
My get(Person person)
method works fine and returns the result.
我的get(Person)方法可以正常工作并返回结果。
DAO class:
DAO类:
public void create(Person person){
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.save(person);//----------------------->this throws exception
session.flush();
tx.commit();
session.close();
}
public List<Person> get(Person person){
criteria = createCriteria("person");
criteria.add(Restrictions.eq("person", person.getFirstName()));
return criteria.list();
}
hibernate.cfg.xml file:
hibernate.cfg。xml文件:
<hibernate-configuration>
<session-factory>
//other database properties
<mapping resource="com/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
Person.hbm.xml file:
Person.hbm。xml文件:
<hibernate-mapping default-lazy="false">
<class name="com.Person" table="Person" entity-name="person">
<id name="personId" type="integer" column="PERSON_ID">
<generator class="increment"/>
</id>
<property name="firstName" column="FIRST_NAME"/>
<property name="address" column="ADDRESS"/>
</class>
</hibernate-mapping>
Entity mapped to table Person:
映射到table Person的实体:
public class Person implements Serializable{
private Integer personId;
private String firstName;
private String address;
public Person(){}
//getters setters
}
1 个解决方案
#1
2
The issue is caused by using the entity-name property in your hbm.xml file. If you do this then you must also provide the same entity-name when you call save:
这个问题是由在hbm中使用实体名称属性引起的。xml文件。如果这样做,那么在调用save时也必须提供相同的实体名称:
session.save("person", person);
A cleaner option would be to remove the entity-name property from your mapping file so you can just call save(person) w/o having to provide an entity-name (it defaults to the FQ class name). Then create your criteria by passing the class instead of a string name. For example:
一个更干净的选项是,从映射文件中删除实体名称属性,这样您就可以调用save(person) w/o,它必须提供一个实体名称(它默认为FQ类名称)。然后通过传递类而不是字符串名称来创建标准。例如:
session.createCriteria(Person.class);
#1
2
The issue is caused by using the entity-name property in your hbm.xml file. If you do this then you must also provide the same entity-name when you call save:
这个问题是由在hbm中使用实体名称属性引起的。xml文件。如果这样做,那么在调用save时也必须提供相同的实体名称:
session.save("person", person);
A cleaner option would be to remove the entity-name property from your mapping file so you can just call save(person) w/o having to provide an entity-name (it defaults to the FQ class name). Then create your criteria by passing the class instead of a string name. For example:
一个更干净的选项是,从映射文件中删除实体名称属性,这样您就可以调用save(person) w/o,它必须提供一个实体名称(它默认为FQ类名称)。然后通过传递类而不是字符串名称来创建标准。例如:
session.createCriteria(Person.class);