java 为什么会出现JPA @OneToOne未知实体错误?

rta7y2nd  于 2023-05-21  发布在  Java
关注(0)|答案(1)|浏览(181)

我收到错误“org.hibernate.AnnotationException:ca.allauto.ups.model.Vehicle.modelId上的@OneToOne或@ManyToOne引用未知实体:对于以下具有一对一关系的类,可以使用“java.lang.Long”。
车辆(父类):

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;
}

VehicleModel(child)类:

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;
}
enyaitl3

enyaitl31#

这一行代码是错误的:

private Long modelId;

变更为:

private VehicleModel modelId;

相关问题