Java org.hibernate.MappingException:未知实体员工
也许这是一个关于stackoverflow的常见问题,有着明确的答案。但是经过两个小时的搜索,我找不到答案 我不熟悉hibernate和maven。我使用的是注释库,但做了一些更改。然而,它不起作用。我使用了一些xml映射文件。 我甚至添加了Employee.hbm.xml,这是用于HibernateXML库的 在这一行>>session.persist(e1)它抛出异常 Employee.javaJava org.hibernate.MappingException:未知实体员工,java,hibernate,Java,Hibernate,也许这是一个关于stackoverflow的常见问题,有着明确的答案。但是经过两个小时的搜索,我找不到答案 我不熟悉hibernate和maven。我使用的是注释库,但做了一些更改。然而,它不起作用。我使用了一些xml映射文件。 我甚至添加了Employee.hbm.xml,这是用于HibernateXML库的 在这一行>>session.persist(e1)它抛出异常 Employee.java import javax.persistence.*; import javax.persist
import javax.persistence.*;
import javax.persistence.Entity;
@Entity
@Table(name = "employee")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="employee")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import org.hibernate.*;
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 configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setName("sonoo");
Regular_Employee e2 = new Regular_Employee();
e2.setName("Vivek Kumar");
e2.setSalary(50000);
e2.setBonus(5);
Contract_Employee e3 = new Contract_Employee();
e3.setName("Arjun Kumar");
e3.setPay_per_hour(1000);
e3.setContract_duration("15 hours");
session.persist(e1);
session.persist(e2);
session.persist(e3);
t.commit();
session.close();
System.out.println("success");
}
}
Main.java
import javax.persistence.*;
import javax.persistence.Entity;
@Entity
@Table(name = "employee")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="employee")
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import org.hibernate.*;
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 configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setName("sonoo");
Regular_Employee e2 = new Regular_Employee();
e2.setName("Vivek Kumar");
e2.setSalary(50000);
e2.setBonus(5);
Contract_Employee e3 = new Contract_Employee();
e3.setName("Arjun Kumar");
e3.setPay_per_hour(1000);
e3.setContract_duration("15 hours");
session.persist(e1);
session.persist(e2);
session.persist(e3);
t.commit();
session.close();
System.out.println("success");
}
}
Employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="employee" discriminator-value="employee">
<id name="id">
<generator class="increment"></generator>
</id>
<discriminator column="type" type="string"></discriminator>
<property name="name"></property>
<subclass name="Regular_Employee" discriminator-value="reg_emp">
<property name="salary"></property>
<property name="bonus"></property>
</subclass>
<subclass name="Contract_Employee" discriminator-value="con_emp">
<property name="pay_per_hour"></property>
<property name="contract_duration"></property>
</subclass>
</class>
</hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="Employee.hbm.xml"/>
<mapping class="Employee"/>
<mapping class="Contract_Employee"/>
<mapping class="Regular_Employee"/>
</session-factory>
</hibernate-configuration>
这个问题是什么
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory(serviceRegistry);
您尝试进行两次配置。这不是必要的
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
您不能使用这种方式来配置Hibernate 5。只需对配置执行此操作
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
如果使用批注,则不需要映射文件。我知道,但我决定添加它,也许我的问题已经解决了,但是,假设您有代表合同员工和普通员工的类,这对meI没有帮助?松开映射文件。忘记XML文件。15年前,当注释不存在时,就需要注释了。另外,永远不要把任何类放在默认包中。我认为你应该试着从一个简单的例子开始。首先,尝试使用注释(无映射文件)只映射employee类,然后从那里开始工作。