gpt4 book ai didi

java - 使用 Hibernate 导入和规范化 XML

转载 作者:数据小太阳 更新时间:2023-10-29 02:32:55 24 4
gpt4 key购买 nike

当使用 Hibernate 将 xml 导入数据库时​​,有没有办法解析由逗号分隔值组成的属性以填充相关表?

在这个(有点混淆)示例中,我有一个 xml 文件,其中每一行代表一个人。 Person 有一个 Hobbies 属性,其中包含一个以逗号分隔的值列表。 Person-Hobby 关系是多对多的。实际上,我有大量数据要处理。

当将每个人导入 PEOPLE 表时,我想将每个 Hobby 添加到 HOBBIES 表(忽略重复项),然后将映射添加到 PEOPLE_HOBBIES 表。

我已经使用双向关联设置了我的映射文件,并且 Hibernate 似乎按照我的预期构建了表格(详情如下),但是我看不到我可以使用什么机制来提取/填充 HOBBIES和 PEOPLE_HOBBIES,同时处理 PEOPLE。

非常感谢收到所有帮助和/或 RTFM 引用。

这是我正在处理的文件 (people.xml):

<People>
<Person Id="1" Name="Dave" Hobbies="drinking, walking"/>
<Person Id="2" Name="Geoff" Hobbies="football, ballet"/>
<Person Id="3" Name="Anne" Hobbies="walking, karate"/>
<Person Id="4" Name="Frank" Hobbies="karate, cross-stitch"/>
</People>

Person.hbm.xml 是(省略 xml decl):

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="name.seller.rich.hobby">
<class name="Person" node="Person" table="PEOPLE">
<id name="id" node="@Id" column="PEOPLE_ID"/>
<property name="name" node="@Name" column="NAME" type="string"/>
<property name="hobbies" node="@Hobbies" column="HOBBIES" type="string"/>
<set name="hobbiesSet" table="PEOPLE_HOBBIES">
<key column="PEOPLE_ID"/>
<many-to-many column="HOBBY" class="Hobby"/>
</set>
</class>
</hibernate-mapping>

Hobby.hbm.xml 是:

<hibernate-mapping package="name.seller.rich.hobby">  
<class name="Hobby" node="Hobby" table="HOBBIES">
<id name="hobby" column="HOBBY" type="string"/>
<set name="people" table="PEOPLE_HOBBIES" inverse="true">
<key column="HOBBY"/>
<many-to-many column="PEOPLE_ID" class="Person"/>
</set>
</class>
</hibernate-mapping>

这是 Person 类,在 setHobbies() 方法中,我用 Hobby 实例填充了 hobbiesSet:

package name.seller.rich.hobby;

import java.util.HashSet;
import java.util.Set;

public class Person {

private long id;

private String name;

private String hobbies;

private Set hobbiesSet = new HashSet();

public String getHobbies() {
return hobbies;
}

public Set getHobbiesSet() {
if (hobbiesSet == null) {
hobbiesSet = new HashSet();
}
return hobbiesSet;
}

public long getId() {
return id;
}

public String getName() {
return name;
}

public void setHobbies(final String hobbies) {
this.hobbies = hobbies;
}

public void setHobbiesSet(final Set hobbiesSet) {
this.hobbiesSet = hobbiesSet;
}

public void setId(final long id) {
this.id = id;
}

public void setName(final String name) {
this.name = name;
}
}

这是我用来处理文件的代码:

package name.seller.rich.hobby;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class DataImporter {

public static void main(final String[] args) {
File baseDir = new File("C:\\workspaces\\hobby");
DataImporter importer = new DataImporter();
Configuration config = importer.setupDb(baseDir);

if (config != null) {
importer.importContents(new File(baseDir, "people.xml"), config);
}
}

private void importContents(final File file, final Configuration config) {
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Session dom4jSession = session.getSession(EntityMode.DOM4J);

SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(file);

List list = document.selectNodes("//Person");
Iterator iter = list.iterator();

while (iter.hasNext()) {
Object personObj = iter.next();
dom4jSession.save(Person.class.getName(), personObj);
}

session.flush();
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}

private Configuration setupDb(final File baseDir) throws HibernateException {
Configuration cfg = new Configuration();
cfg.addFile(new File(baseDir, "name/seller/rich/hobby/Person.hbm.xml"));
cfg.addFile(new File(baseDir, "name/seller/rich/hobby/Hobby.hbm.xml"));

SchemaExport export = new SchemaExport(cfg);

export.setOutputFile("hobbyDB.txt");
export.execute(false, true, false, false);
return cfg;
}
}

这是 PEOPLE 表中的结果内容。

PEOPLE_ID           |NAME        |HOBBIES              
-------------------------------------------------------
1 |Dave |drinking, walking
2 |Geoff |football, ballet
3 |Anne |walking, karate
4 |Frank |karate, cross-stitch

...这些是空的 HOBBIES 和 PEOPLE_HOBBIES 表:

爱好:

HOBBY
----------------------

0 rows selected

人的爱好:

PEOPLE_ID           |HOBBY
---------------------------------------

0 rows selected

最佳答案

您可能会考虑将您的 xml 预处理为更合适的内容。通常将事物列表表示为元素而不是逗号分隔的属性值更好。

例如:

<People>
<Person Id="1" Name="Dave" Hobbies="drinking, walking"/>
<Person Id="2" Name="Geoff" Hobbies="football, ballet"/>
<Person Id="3" Name="Anne" Hobbies="walking, karate"/>
<Person Id="4" Name="Frank" Hobbies="karate, cross-stitch"/>
</People>

会更好:

<People>
<Person Id="1" Name="Dave">
<Hobbies>
<Hobby>drinking</Hobby>
<Hobby>walking</Hobby>
</Hobbies>
</Person>

...
</People>

您可以使用 XSLT 脚本执行此操作 - 请参阅 XSLT - Best way to split and render comma separated text as HTML举个例子。

这样可以更轻松地以您希望的方式导入 Hibernate。

关于java - 使用 Hibernate 导入和规范化 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1116268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com