-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to correctly mapping a Map<Object, String> using XML
PostPosted: Fri Feb 10, 2017 4:00 am 
Newbie

Joined: Fri Feb 10, 2017 3:38 am
Posts: 2
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>


Top
 Profile  
 
 Post subject: Re: How to correctly mapping a Map<Object, String> using XML
PostPosted: Sat Feb 11, 2017 4:11 am 
Newbie

Joined: Fri Feb 10, 2017 3:38 am
Posts: 2
Solved with:
Code:
map name="fields" table ="FIELD_VALUE" cascade="all, save-update">
       <key column = "BIND_FILD_VALUE_ID"/>
       <map-key-many-to-many column="FIELD" class="Field"/>
       <element type="string" column="value" />
</map>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.