1

I have the following Java Entity classes:

@MapsSuperClass
class Request {
    @OneToMany
    List<Parameter> parameters; 

    String service; 
}

@Entity
class PersistedRequest extends Request {
     @Id
     String id; 
}

@Embeddable
class Parameter {
     String key; 
     String value; 
}

I want the data to be mapped to a DB Schema like this:

PersistedRequest     |            ParameterEntity 
ID        SERVICE    |    REQUESTID    KEY        VALUE
                                     \------v------/
                                          Parameter
                          \--------v---------/
                               ParameterPK

The Parameter entity has no unique ID value on the java entity, but the combination Request ID + key is unique. So I created two entities to be able to store the parameters EmbeddedID class:

@Embeddable
class ParameterPK {
    String requestId; 
    String key;
}

@Entity
class ParameterEntity {
    @EmbeddedId
    ParameterPK id; 

    @Embedded
    Parameter parameter; 
}

My main question is:

  • (How) can I somehow map the List on the request class to the database schema above (where Parameter is embedded inside the ParameterEntity)? I do not want to change the java entities (e.g. to put the ParameterEntity inside the Request instead of the Parameter).

  • How do I make the annotations for the ParameterEntity, ParameterPK and Parameter where the key column is shared between ParameterPK and Parameter?

Mathias Mamsch
  • 323
  • 1
  • 15
  • https://stackoverflow.com/questions/37162156/onetomany-with-embeddedid-and-kundera – Anand Vaidya Dec 11 '20 at 14:51
  • That one only partly answers the question. I still don't know if I can make the "Parameter" class be persisted without having a primary key and without changing the java entities. – Mathias Mamsch Dec 11 '20 at 17:23
  • If you want to persist parameter, then why dont you create a separate entity and use it as a foreign key? You can make a join very well.. no? – Anand Vaidya Dec 17 '20 at 16:06

0 Answers0