0

我是一位经验丰富的 Java 开发人员,但刚刚开始使用 Hibernate,在我的 class-xml 中做错了一些事情(可能是多件事情)。这是xml:

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping> 
<class name="database.entities.LineItem" table="algorithms_in_production"> 
<composite-id>
    <key-many-to-one name="algorithm" class="database.entities.Algorithm"  column="algorithm_id" />
    <key-many-to-one name="instrument" class="database.entities.Instrument" column="instrument_id" > </key-many-to-one>
    <key-many-to-one name="underlyings" class="database.entities.Underlyings" column="underlyings_id" > </key-many-to-one>
    <key-many-to-one name="setting" class="database.entities.AlgorithmSetting" column="setting_id" > </key-many-to-one>
</composite-id>

<many-to-one name="algorithmStatistic" class="Algorithm" fetch="join" unique="true" />

<property name="dateInProduction" column="date_in_production" type="org.joda.time.contrib.hibernate.PersistentDateTime"/> 
<property name="dateOutProduction" column="date_out_production" type="org.joda.time.contrib.hibernate.PersistentDateTime"/> 

</class> 
</hibernate-mapping>

这是算法类的 xml:

<?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="database.entities.Algorithm" table="algorithms"> 
<id name="algorithmId" type="int" column="algorithm_id"> 
    <generator class="native"/> 
</id> 

<property name="algorithmTypeId" column="algorithm_type_id" type="int"/> 
<property name="name" column="name" type="String"/> 

</class> </hibernate-mapping>

这是课程的相关部分:

public class LineItem {
    private Algorithm algorithm;
    private Instrument instrument;
    private Underlyings underlyings;
    private AlgorithmSetting setting;
    private AlgorithmStatistic algorithmStatistic;
    List<AlgorithmReturn> algorithmReturns;
    private DateTime dateInProduction;
    private DateTime dateOutProduction;
...


public class Algorithm {
private int algorithmId;
@SuppressWarnings("unused")
private int algorithmTypeId;
private String name;
...

这是我将 LineItem 类映射到的表:

`Table "public.algorithms_in_production"
Column              |            Type             | Modifiers
---------------------+-----------------------------+-----------
algorithm_id        | integer                     | not null
instrument_id       | integer                     | not null
underlyings_id      | integer                     | not null
setting_id          | integer                     | not null
date_in_production  | timestamp without time zone | not null
date_out_production | timestamp without time zone |
"pk_algorithms_in_production" PRIMARY KEY, btree (algorithm_id, instrument_id, underlyings_id, setting_id)`
  • 算法是唯一映射到 algorithm_id 的类。
  • Instrument 是一个唯一映射到 instrument_id 的类。
  • Underlyings 是一个唯一映射到underlyings_id 的类。
  • AlgorithmSetting 是唯一映射到 setting_id 的类。
  • AlgorithmStatistic 是唯一映射到上表中的主键的类。
  • AlgorithmReturn 是唯一映射到上表中的主键加上时间戳的类。该列表应包含映射到主键的所有实例。

这是堆栈跟踪:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Initial SessionFactory creation failed.org.hibernate.MappingException: An association from the table algorithms_in_production refers to an unmapped class: Algorithm
Exception in thread "main" java.lang.ExceptionInInitializerError
    at taipan.database.helpers.HibernateUtil.<clinit>(HibernateUtil.java:23)
    at taipan.database.aggregators.LineItemsInProduction.getLineItems(LineItemsInProduction.java:77)
    at taipan.main.BacktestingFictive.main(BacktestingFictive.java:75)
Caused by: org.hibernate.MappingException: An association from the table algorithms_in_production refers to an unmapped class: Algorithm
    at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1824)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1756)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
    at taipan.database.helpers.HibernateUtil.<clinit>(HibernateUtil.java:19)
    ... 2 more

在此先感谢您的帮助!

4

1 回答 1

0

整个映射似乎或多或少被破坏了 - 例如以下:

  • 元素的属性class不大声key-property
  • 元素的属性column不大声column
  • 元素many-to-one应该用 关闭many-to-one,而不是用one-to-many
  • 元素many-to-one不能包含复合 ID
  • property不允许的孩子many-to-one
  • property元素不应该有tablecascade属性。

为了继续,有意义的是:

  1. 学习 Hibernate 参考手册 -第 5 章。详细了解基本 O/R 映射,并查看其他章节。
  2. 使用提供验证和自动完成功能的 DTD 感知编辑器。
于 2013-05-29T04:55:03.437 回答