如何使用Hibernate XML文件映射字符串列表

时间:2016-07-12 16:15:56

标签: java xml hibernate

我正在尝试使用Hibernate XML映射文件映射String列表。我的课程如下:

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

我在文档中读到我应该使用<element>标记,但我不知道我将如何引用Historic类中的特定属性。

我认为它可能是这样的:     

<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>

是吗?

2 个答案:

答案 0 :(得分:2)

您可以按如下方式编写:

import multiprocessing

class class1():
    def classfunction1(self, a):
        self.x = a

class class2():
    def classfunction2(self, a):
        self.y = a

def test(i):
    print("I'm in the Testfunction")
    b = i * class1.x * class2.y

    return b

def init():  # added
    global class1, class2
    class1 = class1()
    class2 = class2()
    x = 1
    y = 2
    class1.classfunction1(x)
    class2.classfunction2(y)

if __name__ == "__main__":
    init()  # explicit call here
    print("This variable is callable", class1.x)
    print("And this one is also callable", class2.y)
    counter = []
    for i in range(10):
        counter.append(i)
    pool = multiprocessing.Pool(initializer=init, processes=4)  # implicit call
    results = pool.imap(test, counter)
    pool.close()
    pool.join()
    resultslist = list(results)
    print(resultslist)

有关详细信息,请查看https://achieversnitin.wordpress.com/2016/12/06/hibernate-collection-mapping-list/

答案 1 :(得分:0)

假设您有两张桌子(父母和子女) 你应该这样做 - &gt;在您的成绩,分数等情况下,您应该定义另一个表格。

这是hibernate中的XML映射示例

<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>

但是对于List,您也可以使用@ElementCollection annotation。 像这样:

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

获取有关收藏的更多信息:http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html#collections-ofvalues

我希望这会对你有所帮助。