Hello everyone,
I have fallowing table with user properties: ID_USER, KEY, VALUE, where the PK is {ID_USER,KEY}
The entity looks like:
@Entity
@Table(name = "USER_PROPERTIES")
public class UserProperties implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_USER")
private Integer id;
@ElementCollection
@MapKeyColumn(name = "[key]", table = "USER_PROPERTIES")
@Column(name = "[value]", table = "USER_PROPERTIES")
@CollectionTable(name = "USER_PROPERTIES", joinColumns = @JoinColumn(name = "ID_USER"))
private Map<String, String> properties = new HashMap<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Map<String, String> getProperties() {
return properties;
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
}
I can successfully read these entity instances from database, but I am not able to save such entity.
Trying fallowing code:
UserProperties up= new UserProperties();
up.getProperties().put(UserPropertiesKeys.FIRST_NAME.toString(), response.getFirst("fName"));
up.getProperties().put(UserPropertiesKeys.LAST_NAME.toString(), response.getFirst("lName"));
up.getProperties().put(UserPropertiesKeys.COMPANY.toString(), response.getFirst("company"));
up.getProperties().put(UserPropertiesKeys.EMAIL.toString(), response.getFirst("mail"));
up.getProperties().put(UserPropertiesKeys.PHONE.toString(), response.getFirst("phone"));
em.persist(up);
I am getting fallowing exception:
(Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: The list of fields to insert into the table [DatabaseTable(USER_PROPERTIES)] is empty. You must define at least one mapping for this table.
Query: InsertObjectQuery(null)
The entity before saving looks like this (in JSON):
{"properties":{"mail":"mail@mail.com","first_name":"testt","phone":"987654","company":"cmp","last_name":"test"}}
What am I doing wrong? Can you please explain me how to persist such map?
Any help greatly appreciated!