javahibernatejpamappingone-to-one

Why am I getting a JPA @OneToOne Unknown Entity Error?


I am getting the error "org.hibernate.AnnotationException: @OneToOne or @ManyToOne on ca.allauto.ups.model.Vehicle.modelId references an unknown entity: java.lang.Long" for the following classes that have a one-to-one relationship.

The Vehicle (parent class):

package ca.allauto.ups.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "vehicle")
@Getter
@Setter
public class Vehicle {
    private enum Color {
        Beige,
        Black,
        Blue,
        Brown,
        Burgundy,
        Copper,
        Gold,
        Green,
        Grey,
        Orange,
        Purple,
        Red,
        Silver,
        Teal,
        White,
        Yellow
    }
    
    private enum KmsOrMiles {
        Kms,
        Miles
    }
    
    private enum Transmission {
        Automatic,
        Manual
    }
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;
    
    @Column(name="year")
    private short year;
    
    // THIS IS THE PROBLEMATIC FIELD
    // "id" IS THE PK IN THE "vehicle_model" TABLE
    @OneToOne
    @JoinColumn(name="model_id", referencedColumnName="id")
    private Long modelId;
    
    @Column(name="doors")
    private DoorsEnum doors;
    
    @Column(name="vehicle_type")
    private VehicleTypeEnum vehicleType;
    
    @Column(name="color")
    private Color color;
    
    @Column(name="odometer")
    private int odometer;
    
    @Column(name="kms_miles")
    private KmsOrMiles kmsOrMiles;
    
    @Column(name="transmission")
    private Transmission transmission;
    
    @Column(name="vin")
    private String vin;
}

The VehicleModel (child) class:

package ca.allauto.ups.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "vehicle_model")
@Getter
@Setter
public class VehicleModel {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;
    
    @Column(name="model")
    private String model;
    
    @OneToOne(mappedBy = "modelId")
    private Vehicle vehicle;
}

Solution

  • This line of code is wrong:

    private Long modelId;
    

    Change to:

    private VehicleModel modelId;