Hello everyone, I recently started using hibernate, and I am faced with a problem, how to map a Map <Field, String> where Field is a class already mapped in Hibernate, when the application is running and insert data there are no problems, the data present in the classes are consistent, but when i close the application and run it again the data do not match.
I searched the internet for possible solutions but none worked correctly.
Note:
Field class are used in another classes.
Thanks in advance for any help
Binding.java:
Code:
@Entity
@Table(name = "BINDING")
public class Binding {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private long id;
@OneToMany
private List<Register> registerRef;
@Column(name = "TYPE")
private int type;
@Column(name = "VALUE")
private final StringProperty value;
@OneToMany
private Map<Field, String> fields;
public Binding(){
registerRef = new ArrayList<>();
fields = new HashMap<>();
value = new SimpleStringProperty();
}
public void setId(long i){
this.id = i;
}
public long getId(){
return this.id;
}
public void setType(int i){
this.type = i;
}
public int getType(){
return this.type;
}
public void setValue(String s){
this.value.set(s);
}
public String getValue(){
return this.value.get();
}
public void setRegisterRef(List<Register> l){
this.registerRef = l;
}
public List<Register> getRegisterRef(){
return this.registerRef;
}
public void addRegisterRef(Register r){
this.registerRef.add(r);
}
public void removeRegisterRef(Register r){
this.registerRef.remove(r);
}
public void setFields(Map<Field, String> f){
this.fields = f;
}
public Map<Field, String> getFields(){
return this.fields;
}
public void addField(Field f, String s){
this.fields.put(f, s);
}
public void removeField(Field f){
this.fields.remove(f);
}
}
Binding.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="ic2doc2.model.Binding" table="BINDING">
<id name="id" column="ID" type="long" >
<generator class="native"/>
</id>
<property name="type" column="TYPE" type="int"/>
<property name="value" column="VALUE" type="string"/>
<list name="registerRef" cascade="all">
<key column="BINDING_REGISTER_ID"/>
<list-index column="REGISTER_POSITION"/>
<one-to-many class="ic2doc2.model.Register"/>
</list>
<map name="fields" table="FIELD_VALUE" cascade="all">
<key column="ID"/>
<index column="VAL" type="string" />
<many-to-many class="ic2doc2.model.Field" column="BINDING_FIELD_ID"></many-to-many>
</map>
</class>
</hibernate-mapping>
Field.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="ic2doc2.model.Field" table="FIELD">
<id name="id" column="ID" type="long" >
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string"/>
<property name="width" column="WIDTH" type="int"/>
<property name="offset" column="OFFSET" type="int"/>
<property name="access" column="ACCESS">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">ic2doc2.model.Access</param>
<param name="useNamed">true</param>
</type>
</property>
<property name="description" column="DESCRIPTION" type="string"/>
<set name="values" cascade="all, delete-orphan">
<key column="FIELD_ID"/>
<one-to-many class="ic2doc2.model.Value"/>
</set>
<property name="defaultValue" column="DEFAULT_VALUE" type="string"/>
</class>
</hibernate-mapping>