提问者:小点点

为什么我收到JPA@OneToOne未知实体错误?


我得到错误"org. hibernate.AnnotationException:@OneToOne或@ManyToOne在ca.allauto.ups.model.车辆.ModelId引用一个未知的实体: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;
}

车辆模型(儿童)类:

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

共1个答案

匿名用户

这行代码是错误的:

private Long modelId;

更改为:

private VehicleModel modelId;