首页 > 解决方案 > Hibernate - hbml.xml - Unidirectional set - one-to-many

问题描述

Hello all and thanks for reading,

I have the following problem:

org.hibernate.boot.MappingException: Association [com....core.complex.domain.Complex.outlayTypes] references an unmapped entity [com....core.complex.domain.Complex.outlayTypes

I have been trying to fix this since yesterday and I dont understand what is the problem. Also, I am not sure why there are no xml documentation on the ofitial hibernate page (https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#collections-set).

Because of how I am designing the system, I want to use xml configurations.

So, I have 2 entities:

Complex (1 --- N) OutlayType

public class Complex extends AggregateRoot {

    private ComplexId id;
    ...
    private Set<OutlayType> outlayTypes;

    constructors

    getters and setters

    public Set<OutlayType> getOutlayTypes() { return outlayTypes; }

    public void setOutlayTypes(Set<OutlayType> outlayTypes) { this.outlayTypes = outlayTypes; }

}



public class OutlayType {

    OutlayTypeId id;
    ...

    constructors

    getters and setters

{

So, like is a unidirectional relation, I dont care to have a Complex field on the OutlayType.

In the DB I have the following:

CREATE TABLE `complex` (
  `id` varchar(36) NOT NULL,
   ...
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE `outlay_type` (
  `id` varchar(36) NOT NULL,
  ...
  `complex_id` varchar(36) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

And this is my hbm.xml configurations in the Complex.hbm.xml file.

<hibernate-mapping>
    <class name="com....core.complex.domain.Complex" table="complex">
        <composite-id name="id" class="com....core.complex.domain.valueobject.ComplexId" access="field">
            <key-property column="id" name="value" length="36" access="field" />
        </composite-id>


        <set name="outlayTypes" cascade="all">
            <key>
                <column name="complex_id" not-null="true" />
            </key>
            <one-to-many class="com....core.complex.domain.OutlayType" />
        </set>
    </class>
</hibernate-mapping>

Again, like is a unidirectional relation, I dont have any mappings on the OutlayType.hbm.xml.

I reviewed a lot of tutorials, like: https://www.tutorialspoint.com/hibernate/hibernate_set_mapping.htm

But I dont see why this is not working, and is throwing:

org.hibernate.boot.MappingException: Association [com....core.complex.domain.Complex.outlayTypes] references an unmapped entity [com....core.complex.domain.Complex.outlayTypes

Any ideas? Thanks

标签: hibernatesetone-to-manyhbmxmlmappingexception

解决方案


我的问题是我正在将部分核心作为库导入另一个应用程序中。并且在没有 OutlayType.hbm.xml 的情况下重新加载了 Application.yml 文件。

    mapping-resources:
    - infraestructure/hibernate/Complex.hbm.xml
    - infraestructure/hibernate/OutlayType.hbm.xml

因此,在其中添加丢失的文件可以解决问题。谢谢阅读。


推荐阅读