Сохранение хеш-карты в спящем режиме - PullRequest
0 голосов
/ 02 июля 2018

В приведенном ниже примере, как мне сохранить хэш-карту транспортных средств в классе сотрудников. @ElementCollection работал для Карты имен книг, но не работает на карте объектов. ОШИБКА

Failed to create sessionFactory object.org.hibernate.AnnotationException: 
Use of @OneToMany or @ManyToMany targeting an unmapped class: hibernate.application.Employee.vehicles[hibernate.application.Vehicle]
Exception in thread "main" java.lang.ExceptionInInitializerError
    at hibernate.application.Manager.main(Manager.java:32)
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: hibernate.application.Employee.vehicles[hibernate.application.Vehicle]
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1255)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:808)
    at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:97)

Employee.java

package hibernate.application;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.MapKey;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="Employee_table")
public class Employee {

    @Id
       private int id;
    @Column(name = "first_name")
       private String firstName; 
    @Column(name = "last_name")
       private String lastName;   
    @Column(name = "salary")
       private int salary;  

    //@org.hibernate.annotations.Type( type = "org.hibernate.type.SerializableType" )
        @ElementCollection
        private Map<Integer, String> books;


        @OneToMany
        private Map<Integer, Vehicle> vehicles;

       public Employee() {}
       public Employee(String fname, String lname, int salary) {
          this.firstName = fname;
          this.lastName = lname;
          this.salary = salary;
          books = new HashMap<Integer,String>();
          vehicles = new HashMap<Integer,Vehicle>();
       }
       public void showBooks(){
           for (Integer key : books.keySet()) {
                System.out.println("Books owned - "+ books.get(key));

            }
       }

       public void addBook(String name){
           books.put(books.size(), name);
       }

       public void addVehicle(String name){
           vehicles.put(vehicles.size(), new Vehicle(vehicles.size(), name));
       }


       public int getId() {
          return id;
       }

       public void setId( int id ) {
          this.id = id;
       }

       public String getFirstName() {
          return firstName;
       }

       public void setFirstName( String first_name ) {
          this.firstName = first_name;
       }

       public String getLastName() {
          return lastName;
       }

       public void setLastName( String last_name ) {
          this.lastName = last_name;
       }

       public int getSalary() {
          return salary;
       }

       public void setSalary( int salary ) {
          this.salary = salary;
       }
    }

Vehicle.java

package hibernate.application;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "vehicle_table")
public class Vehicle {

    @javax.persistence.Id
    @Column(name = "vehicle_id")
    int Id;

    @Column(name = "model")
    String Model;

    public int getId() {
        return Id;
    }
    public void setId(int id) {
        Id = id;
    }
    public String getModel() {
        return Model;
    }
    public void setModel(String model) {
        Model = model;
    }

    public Vehicle(int id, String name) {
        super();
        Id = id;
        Model = name;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...