我是一名经验丰富的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)`
这是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
提前感谢您的帮助!
答案 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
元素不应包含table
和cascade
属性。继续下去是有意义的: