我在Maven中使用Hibernate時遇到了異常。休眠版本是5.1.0.Final。 唯一的例外是: 這裏是我的項目結構: Hibernate Maven MappingException未知實體
這裏是我的實體類ABC:
package com;
import javax.persistence.*;
@Entity
@Table(name = "abc_inf")
public class ABC {
@[email protected]
private Integer id;
private String name;
public ABC() {
}
setters and getters omitted
}
這裏是我的主類:
package com;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Main {
public static void main(String[] args) {
Configuration conf = new Configuration().configure();
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(sr);
Session session = sf.openSession();
ABC abc = new ABC();
abc.setName("abc");
session.save(abc);
session.flush();
session.close();
sf.close();
}
}
這裏是我的hibernate.cfg.xml:
<hibernate-configuration>
<session-factory>
mysql connection and properties settings omitted
<mapping class="com.ABC"/>
</session-factory>
</hibernate-configuration>
能否請您確認您所呼叫的hibernate.cfg.xml的運行 – LearningPhase
它使用的hibernate.cfg.xml你的主程序之前配置你的實體,否則會拋出一些連接異常因爲它首先無法連接到數據庫。我還從配置中打印了屬性,它與hibernate.cfg.xml中定義的屬性相同 – user2716189