| Considering the following mapping involving an @Entity with an @ElementCollection of type Map with @Embeddable as value and a declared column of this @Embeddable as Key:
@Entity
@Table(name = "parameters")
public class Parameter {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@ElementCollection
@MapKeyColumn(name = "value")
@CollectionTable(name = "states",
joinColumns = @JoinColumn(name = "parameter_id"))
protected Map<Integer, State> states;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<Integer, State> getStates() {
return states;
}
public void setStates(Map<Integer, State> states) {
this.states = states;
}
}
@Embeddable
public class State {
@Column(insertable = false, updatable = false)
private Integer value;
private String level;
private String description;
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
When adding a new "State" class to the Map then Flushing, the number of parameters in the SQL insert statement does not matches the number of bind parameters. There is one extra parameter set to null value, which makes the underlaying driver to trigger an exception (see log below). This has been tested with MariaDB and H2. Here the test code:
public class App {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("main");
EntityManager entityManager = emf.createEntityManager();
entityManager.getTransaction().begin();
Parameter type = new Parameter();
type.setName("test_type");
entityManager.persist(type);
entityManager.flush();
State state = new State();
state.setLevel("OK");
state.setDescription("Some description");
Map<Integer, State> states = new HashMap<>();
states.put(0, state);
type.setStates(states);
entityManager.flush();
}
}
and the output exception :
|