Hibernate语法错误:必须为元素类型“column”声明属性“column”

时间:2013-05-28 19:57:59

标签: java xml hibernate

我是一名经验丰富的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>

这是Algorithm-class的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是一个唯一映射到上表中主键和时间戳的类。该列表应包含映射到主键的所有实例。

这是stackstrace:

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

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

整个映射似乎或多或少地被打破 - 例如:

  • 属性class不是元素key-property
  • 的大声说明
  • 属性column不是元素column
  • 的大声说明
  • 元素many-to-one应使用many-to-one关闭,而不应使用one-to-many关闭。
  • 元素many-to-one不能包含composite-id
  • property不允许many-to-one
  • 的孩子
  • property元素不应包含tablecascade属性。

继续下去是有意义的:

  1. 详细研究Hibernate参考手册 - Chapter 5. Basic O/R Mapping,并查看其他章节。
  2. 使用提供验证和自动完成功能的DTD感知编辑器。