org.hibernate.MappingException:无法确定类型:

我在休眠中映射属性时遇到问题。我只想将 Device(1 对多)映射到 DeviceData(多对 1),反之亦然。


我的输出应该是:


表:device=id,device(一个设备号),


表:DeviceData = id, device_id (forgein key), ...


一切都由 json2ojo 生成器生成。


@Entity(name = "Device")

@Table(name = "device_devices")

public class Device {    

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @JsonProperty

    private Long id;


    @JsonProperty("device")

    @Column(unique = true)

    private String device;


    @JsonIgnore

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();


    @OneToMany(fetch = FetchType.LAZY, mappedBy = "device", cascade = CascadeType.ALL, orphanRemoval = true, targetEntity = DeviceData.class)

    private List<DeviceData> deviceData = new ArrayList<>();

...

}


@Entity(name = "DeviceData")

@Table(name = "device_data")

public class DeviceData {

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @JsonProperty

    private Long id;

...

    @JsonIgnore

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Device.class)

    @JoinColumn(name = "device_id", referencedColumnName = "id")

    private Device device;


    @JsonIgnore

    private Map<String, Object> additionalProperties = new HashMap<String, Object>();



   @JsonProperty("id")

    public Long getId(){

        return id;

    }


    @JsonIgnore

    public Device getDevice(){

        return device;

    }


    @JsonIgnore

    public void setDevice(Device device){

        this.device = device;

    }

...


...

    @JsonAnyGetter

    public Map<String, Object> getAdditionalProperties() {

        return this.additionalProperties;

    }


    @JsonAnySetter

    public void setAdditionalProperty(String name, Object value) {

        this.additionalProperties.put(name, value);

    }

}



喵喵时光机
浏览 69回答 1
1回答

BIG阳

默认情况下,JPA 会尝试保留类的所有属性,但您可以使用注释@Entity忽略某些属性。@Transient在您的情况下,如果您不想additionalProperties在两个类中都保留字段,则应将它们标记为@Transient:@JsonIgnore@Transientprivate Map<String, Object> additionalProperties = new HashMap<String, Object>();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java