2

I'm trying to map a list of String using Hibernate XML mapping files. My class is the following:

public class Historic {
    private String id;
    private String resolution;
    private List<String> names;
    private List<Score> scores;
    private String base;
    private List<Grade> grades;
}

I read in the documentation that I should be using the <element> tag, but I don't know how I'm going to reference the specific attributes from the Historic class.

I think that it could be something like:

<class name="Historic" table="HISTORIC">
    <id name="id" column="id">
        <generator class="native" />
    </id>

    <property name="resolution" type="string" column="resolution" />

    <element
        name="names"
        column="names"
        type="string"
    />

    ...

</class>

Is that right?

2
  • you can't directly do it in a single table, need a mapping first to other table. Commented Jul 12, 2016 at 16:22
  • you can do it but it's not a good practice. Commented Jul 12, 2016 at 17:02

2 Answers 2

2

You can write it as follows:

<list name="names" table="names">
  <key column="name_id"></key>
  <index column="seq"></index>
  <element column="names" type="string"></element>
</list>

For further details take a look at https://achieversnitin.wordpress.com/2016/12/06/hibernate-collection-mapping-list/.

Sign up to request clarification or add additional context in comments.

Comments

0

Assume you've two table (parent and children) you should do something like this -> in your case for grades, scores etc. you should define another tables.

here is an example of XML mapping in hibernate

<class name="Parent" table="Parent">
  <id name="parentId" column="id" type="integer" /> <!-- TODO: specify generator -->
  <property name="parentName" type="string" column="name" />
  <bag name="childs" table="Children" inverse="true">
    <key column="parent_id" />
    <one-to-many class="Child" />
  </bag>
</class>

<class name="Child" table="Children">
  <id name="childId" column="id" type="integer" /> <!-- TODO: specify generator -->
  <property name="childName" type="string" column="name" />
  <many-to-one name="parent" column="parent_id" not-null="true"/>
</class>

but for List you can use @ElementCollection annotation too. like this :

@ElementCollection
@CollectionTable(name="Nicknames", joinColumns=@JoinColumn(name="user_id"))
@Column(name="nickname") // nickname is the name of String field.
public List<String> getNicknames() { ... } 

to get more info about collection : http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-ofvalues

I hope this helps you.

1 Comment

in above mapping, every parent has one or many child but each child has only one parent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.