1

I have a problem with mapping a property in hibernate. I just want to map Device (1 to many) to DeviceData (many to 1) and vice versa.

My output should be:

Table: Device = id, device (a device number),

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

Everything has been generated by json2ojo generator.

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

Error:

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)

1 Answer 1

1

By default, JPA tries to persist all the properties of @Entity class, but you can ignore some properties using @Transient annotation. In your case, if you don't want to persist additionalProperties field in both classes, you should mark their as @Transient:

@JsonIgnore
@Transient
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.