java - org.hibernate.MappingException : Could not determine type for: java. util.Map

标签 java hibernate jpa-2.0

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

我的输出应该是:

表:设备 = ID, 设备(设备号),

表:设备数据 = ID, device_id(伪造 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);
    }
}

错误:

org.hibernate.MappingException: Could not determine type for:
   java.util.Map, at table: device_data, for columns:
   [org.hibernate.mapping.Column(additionalProperties)]     at
   org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:456)  at
   org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:423)  at
   org.hibernate.mapping.Property.isValid(Property.java:226)    at
   org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:597)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:265)     at
   org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
    at
   org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:461)
    at
   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
    at
   io.dropwizard.hibernate.SessionFactoryFactory.buildSessionFactory(SessionFactoryFactory.java:96)
    at
   io.dropwizard.hibernate.SessionFactoryFactory.build(SessionFactoryFactory.java:49)
    at
   io.dropwizard.hibernate.SessionFactoryFactory.build(SessionFactoryFactory.java:39)
    at
   io.dropwizard.hibernate.HibernateBundle.run(HibernateBundle.java:67)
    at
   io.dropwizard.hibernate.HibernateBundle.run(HibernateBundle.java:19)
    at io.dropwizard.setup.Bootstrap.run(Bootstrap.java:200)    at
   io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:42)
    at
   io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87)
    at io.dropwizard.cli.Cli.run(Cli.java:78)   at
   io.dropwizard.Application.run(Application.java:93)

最佳答案

默认情况下,JPA 会尝试保留 @Entity 类的所有属性,但您可以使用 @Transient 注释忽略某些属性。在您的情况下,如果您不想在两个类中保留 additionalProperties 字段,则应将它们标记为 @Transient:

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

关于java - org.hibernate.MappingException : Could not determine type for: java. util.Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56406002/

相关文章:

java - X12 解析使用 hadoop mapreduce

java - 在 JPA 中,实体是否可以访问其 Embedded 对象的 Embedded?

java - hibernate 和子树异常的意外结束

java - 非法参数异常 : Could not locate named parameter

java - 为什么 Hibernate 会生成约 500 个 SQL 查询?

mysql - 嵌入式类和@ManyToOne之间的性能差异

java - JPA 问题,当我调用 : em. persist(obj); 时 `Referential integrity constraint violation Exception`

java - JBoss 集群和 Lighttpd 负载平衡显示不一致的行为

java - 如何考虑夏令时在 mongoDB 中存储 future 的调度日期

java - 在类 android.app.Notification$Action 中找不到引用的方法 'android.app.RemoteInput[] getRemoteInputs()'