我想将数据从 xml 文件保存到 db。这是 xml 示例:
<Stock>
<Code>7052</Code>
<Name>PADINI</Name>
<StockDailyRecords>
<PriceOpen>1.2</PriceOpen>
<PriceChange>1.1</PriceChange>
<PriceClose>10.0</PriceClose>
<Volume>3000000L</Volume>
</StockDailyRecords>
<StockDailyRecords>
<PriceOpen>1.3</PriceOpen>
<PriceChange>1.2</PriceChange>
<PriceClose>11.0</PriceClose>
<Volume>5000000L</Volume>
</StockDailyRecords>
</Stock>
我想通过 XML 映射文件(hbm)将数据保存到 Hibernate 中具有一对多表关系的两个表中:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 26 April 2011 11:37:37 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class entity-name="Stock" table="STOCK" node="Stock">
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="sequence" />
</id>
<property name="stockCode" type="string" node="Code">
<column name="STOCK_CODE" length="10" not-null="true" unique="true" />
</property>
<property name="stockName" type="string" node="Name">
<column name="STOCK_NAME" length="20" not-null="true" unique="true" />
</property>
<set name="stockDailyRecords" table="STOCK_DAILY_RECORD" inverse="true" lazy="true" fetch="select">
<key>
<column name="STOCK_ID" not-null="true" />
</key>
<one-to-many entity-name="StockDailyRecord" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 26 April 2011 11:37:37 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class entity-name="StockDailyRecord" table="STOCK_DAILY_RECORD" node="StockDailyRecords">
<id name="recordId" type="java.lang.Integer">
<column name="RECORD_ID" />
<generator class="sequence" />
</id>
<many-to-one name="stock" fetch="select" entity-name="Stock">
<column name="STOCK_ID" not-null="true" />
</many-to-one>
<property name="priceOpen" type="java.lang.Float" node="PriceOpen">
<column name="PRICE_OPEN" precision="6" />
</property>
<property name="priceClose" type="java.lang.Float" node="PriceClose">
<column name="PRICE_CLOSE" precision="6" />
</property>
<property name="priceChange" type="java.lang.Float" node="PriceChange">
<column name="PRICE_CHANGE" precision="6" />
</property>
<property name="volume" type="java.lang.Long" node="Volume">
<column name="VOLUME" />
</property>
</class>
</hibernate-mapping>
这是我的实现代码:
public static void main(String[] args) throws DocumentException {
InputStream input = null;
try {
input = new FileInputStream("c:\\temp\\log\\UserData.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
org.dom4j.Document document = new SAXReader().read(input);
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
dom4jSession.save(document.getRootElement());
System.out.println("Done");
dom4jSession.flush();
transaction.commit();
session.close();
}
但它只将数据保存到“STOCK”表中。表“STOCK_DAILY_RECORD”为空。我怎么解决这个问题?
解决!)
Stock.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 26 April 2011 11:37:37 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class entity-name="Stock" table="STOCK" node="Stock">
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="sequence" />
</id>
<property name="stockCode" type="string" node="Code">
<column name="STOCK_CODE" length="10" not-null="true" unique="true" />
</property>
<property name="stockName" type="string" node="Name">
<column name="STOCK_NAME" length="20" not-null="true" unique="true" />
</property>
<set name="stockDailyRecords" node="." cascade="all">
<key>
<column name="STOCK_IDD" not-null="true" />
</key>
<one-to-many entity-name="StockDailyRecord" />
</set>
</class>
</hibernate-mapping>
StockDailyRecord.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 26 April 2011 11:37:37 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class entity-name="Stock" table="STOCK" node="Stock">
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="sequence" />
</id>
<property name="stockCode" type="string" node="Code">
<column name="STOCK_CODE" length="10" not-null="true" unique="true" />
</property>
<property name="stockName" type="string" node="Name">
<column name="STOCK_NAME" length="20" not-null="true" unique="true" />
</property>
<set name="stockDailyRecords" node="." cascade="all">
<key>
<column name="STOCK_IDD" not-null="true" />
</key>
<one-to-many entity-name="StockDailyRecord" />
</set>
</class>
</hibernate-mapping>
执行:
public static void main(String[] args) throws DocumentException {
InputStream input = null;
try {
input = new FileInputStream("c:\\temp\\log\\UserData.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
org.dom4j.Document document = new SAXReader().read(input);
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
dom4jSession.save(document.getRootElement());
System.out.println("Done");
dom4jSession.flush();
transaction.commit();
session.close();
}