Правильное создание зависимостей JPA

343
10 февраля 2017, 03:28

Объясните плиз данную ошибку я новичок в JPA/Hibernate!

Unknown column 'employee0_2_.employee_id' in 'field list'
Что не так с полями или колонками ? Что не хватает ? 

Получаю ошибку:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'employee0_2_.employee_id' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1885)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:56)

Employee:

@Entity
@Table (name = "employee")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee{
public Employee(int id, String name, double salary, String specialization) {
    this.employee_id = id;
    this.name = name;
    this.salary = salary;
    this.specialization = specialization;
}
@Override
public String toString() {
    return "Employee{" +
            "id=" + employee_id +
            ", name='" + name + '\'' +
            ", salary=" + salary +
            ", deg='" + specialization + '\'' +
            '}';
}
public int getId() {
    return employee_id;
}
public void setId(int id) {
    this.employee_id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public double getSalary() {
    return salary;
}
public void setSalary(double salary) {
    this.salary = salary;
}
public String getSpecialization() {
    return specialization;
}
public void setSpecialization(String specialization) {
    this.specialization = specialization;
}
@Id
@Column(name = "employee_id")
private int employee_id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private double salary;
@Column (name = "specialization")
private String specialization;
public Employee(){}

}

Department:

@Table(name = "department")
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Department extends Managers{
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Department(String name) {
    this.name = name;
}
public Managers getManager() {
    return manager;
}
public void setManager(Managers manager) {
    this.manager = manager;
}
@OneToOne
@JoinColumn(name = "department_id" , referencedColumnName = "manager_id", insertable = false, updatable = false)
private Managers manager;
@Column(name = "name")
private String name;
public int getDepartment_id() {
    return department_id;
}
public void setDepartment_id(int department_id) {
    this.department_id = department_id;
}
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "department_id")
private int department_id;
public Department(){}
}

Managers:

@Table(name = "managers")
@Entity
public class Managers extends Employee{
@OneToOne
@JoinColumn(name = "manager_id", referencedColumnName = "employee_id" , insertable = false, updatable = false)
private Employee employee;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
public Managers(String name, String lastname) {
    this.name = name;
    this.lastname = lastname;
}
public Managers(){}
@Column(name = "name")
private String name;
@Column(name = "lastname")
private String lastname;
public int getManager_id() {
    return manager_id;
}
public void setManager_id(int manager_id) {
    this.manager_id = manager_id;
}
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "manager_id")
private int manager_id;
}

Sql Table:

CREATE TABLE Employee(employee_id INTEGER PRIMARY KEY NULL , name VARCHAR(50) UNIQUE NULL , salary VARCHAR(50) null , specialization VARCHAR(50) NULL);
CREATE TABLE Managers(name  VARCHAR(50) NULL UNIQUE , lastname VARCHAR(50) NULL UNIQUE , manager_id INTEGER NULL PRIMARY KEY AUTO_INCREMENT, FOREIGN KEY (manager_id) REFERENCES Employee(employee_id) );
CREATE TABLE Department (department_id INTEGER PRIMARY KEY AUTO_INCREMENT ,name VARCHAR(50) UNIQUE NULL , FOREIGN KEY (department_id) REFERENCES Managers(manager_id));

Main:

public class ApplTest {
public static void main(String[] args) {
    System.out.println("In the main");
    EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("EmployeesTesting");
    EntityManager entityManager = emfactory.createEntityManager();
    Employee employee = new Employee(1000,"Maks",3500,"Programmer");
    Managers manager = new Managers("maks","Burkov");
    Department department = new Department("Programmers");
     if(entityManager.contains(employee)){
        System.out.println("In the contains , the object has persistence state! ");
        entityManager.getTransaction().begin();
        entityManager.persist(employee);
        entityManager.persist(manager);
        entityManager.persist(department);
        entityManager.getTransaction().commit();
        entityManager.close();
        emfactory.close();
        System.out.println("Data Inserted!");
    }
    else if (!(entityManager.contains(employee))){
        System.out.println("In the not contains, the object is detached! ");
        entityManager.getTransaction().begin();
        entityManager.merge(employee);
        entityManager.merge(manager);
        entityManager.merge(department);
        entityManager.getTransaction().commit();
        entityManager.close();
        emfactory.close();
        System.out.println("Data Inserted!");
    }
    System.out.println("Get out from main");
  }
}

Persistence xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
     http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="EmployeesTesting" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>employee.Employee</class>
    <class>department.Department</class>
    <class>managers.Managers</class>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/employees"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="****"/>
        <property name="javax.persistence.jdbc.Driver" value="com.mysql.jdbc.Driver"/>
    </properties>
</persistence-unit>

READ ALSO
Вопрос по synchronized

Вопрос по synchronized

Здравствуйте, вопрос по ключевому слову synchronized

312
Почему мой Parcelable возвращает null?

Почему мой Parcelable возвращает null?

Дано: при нажатии на кнопку из адаптера во фрагмент передается Parcelable:

340
Эффективный дебаг рекурсии

Эффективный дебаг рекурсии

Хочу создать сбалансированное, отсортированное дерево, для того чтобы отрабатывать методы вставки, поиска, подсчета размера и глубины дерева

269
Нужен алгоритм java

Нужен алгоритм java

Создаем массив чисел от 1 до 75, с помощью рандома чисел от 1 до 75 начинаем поиск элемента в массивеСовпал - удалили

316